"Albert" <al*****************@gmail.com> writes:
I'm having trouble understanding what K&R are saying: 'The bitwise AND
operator & is often used to mark off some set of bits, for example,
n = n % 0177;
You mean "n = n & 0177";
sets to zero all but the low-order 7 bits of n.'
So in the end, what actually happens? What does the value of n become?
Think of the value of n as binary. 0177 is an octal constant; in
binary, it's 01111111. Ideally, n should be unsigned (bitwise operators
normally shouldn't be used with signed types).
For single-bit operands, the "&" operator is defined as:
0 & 0 == 0
0 & 1 == 0
1 & 0 == 0
1 & 1 == 1
Suppose the value of n (in binary) is 00010100111001111000101101001100
(32 bits). Then and-ing that value with 0177 yields:
00010100111001111000101101001100
& 00000000000000000000000001111111
--------------------------------
00000000000000000000000001001100
As you can see, this does just what K&R say it does: it zeros all but
the low-order 7 bits of n.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.