Explain this to me in English please.

Started by
8 comments, last by rhino2876 22 years ago
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.
Rhino2876
Advertisement
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
Hibble, Hobble, Hubble, Hool.
Don''t mind me, I''m just a fool.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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?
Rhino2876
printf("%b", x);
Thanks.
Just what I was looking for.
Rhino2876
Except that it doesn''t work.
Rhino2876
%b is not ANSI C and doesn''t work in some implementations (MSVC for one I believe)...


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

Hexadecimal assignment:
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 ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
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 ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!


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

This topic is closed to new replies.

Advertisement