Consider the following statement given in C book by Dennis ritchie
The bitwise AND operator & is often used to mask off some set of bits,
for example
n = n & 0177;
sets to zero all but the low-order 7 bits of
n
.
Regarding bit-wise AND operator, my understanding is as follows
11010010 & 01101010 = 01000010
i.e., sets to 0 if the corresponding bit in any operand is 0
But in the above quoted statement, it is told that the the bits are 0 except the last 7.
If we expand 0177 in binary, it will be 10110001 (8-bits), so we cannot say about lower order 8 bits. Where i went wrong?
Quoting C11
, chapter §6.4.4.1, (emphasis mine)
[...] An octal constant consists of the prefix
0
optionally followed by a sequence of the digits0
through7
only. [...]
So, an integer constant like 0177
is octal, which is the same as decimal 127
or hex 7F
. The binary representation gives you 01111111
, which is the true in
[...] sets to zero all but the low-order 7 bits of
n
.