Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Explain this to me in English please.

This topic is 6052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here is a problem that I came across in the K&R C Programming Language book. Somebody please explain to me what they are asking for in laymen''s terms. Don''t solve it, just explain what is being asked. Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

##### Share on other sites
As far as I understand it it means:
x contains a series of bits
p is a position into x
replace n bits of x, starting at p, with the n rightmost bits
of y.

For instance:
x = 01010101
p = 4
n = 4
y = 00001111

The n(4) rightmost bits of y is 1111
Replacing the 4 bits in x starting at position 4 with the 4 rightmost bits of y (1111) gives:
01011111
So x should be 0101111, and returned.

-Neophyte

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GED d- s:+ a- C++\$ UL++ P++ L++ E W+ N+ o K? w !O M--(+) V-- PS+
PE Y+ PGP t-- 5++ X+ R(+) rv+(++) b+++ DI+ D(+) G e+>++ h r--> y+
----- END GEEK CODE BLOCK-----
geekcode.com

##### Share on other sites
Hibble, Hobble, Hubble, Hool.
Don''t mind me, I''m just a fool.

##### Share on other sites
On another note, is there a way to print and assign values to binary numbers. For example, is something like this, possible?

x = 0100101;
printf("%d", x);

I''ve tried something similar to this and i only get the integer value of x output to the screen. Any way to do this without making x an array where the elements each equal 0 or 1?

printf("%b", x);

##### Share on other sites
Thanks.
Just what I was looking for.

##### Share on other sites
Except that it doesn''t work.

##### Share on other sites
%b is not ANSI C and doesn''t work in some implementations (MSVC for one I believe)...

##### Share on other sites
Because the assignment was done in octal. The only literal integers supported in C and C-like languages are decimal, octal and hexadecimal. Converting from binary to hex is fairly straight forward: 4 binary digits correspond to one hex digit, so you can probably figure it out in seconds.

Decimal assignment
x = 14;

Octal assignment
x = 016; // preceding zero, as in your case, indicates octal

x = 0x0e;

To assign the binary value 0100101 to x, convert it to hex (0x25) and assign:
x = 0x25;
printf( "%b", x );

Have fun!

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
Thanks to Kylotan for the idea!

##### Share on other sites
Just in case you haven't figured it out, however, you're supposed to solve the original question using bitshifts (operator <<, operator >>) and masking (operator &, operator |).

[Edit: Typo.]

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
Thanks to Kylotan for the idea!

[edited by - Oluseyi on April 19, 2002 1:02:57 PM]

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011153
• ### Who's Online (See full list)

There are no registered users currently online

×