Jump to content
  • Advertisement

Archived

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

rhino2876

Explain this to me in English please.

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
printf("%b", x);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
%b is not ANSI C and doesn''t work in some implementations (MSVC for one I believe)...


Share this post


Link to post
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

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!

Share this post


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


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!