Question about the book "The C Programming Language"

• 05-15-2005
Question about the book "The C Programming Language"
Hi. I'm reading The C Programming Language and I am having a hard time understanding this. On page 48, when the authors present the bitwise operators, they have this as an example for the & operator:
n = n & 0177;

"sets to zero all but the low-order bits of n."

I know 0177 is an octal number but that's all I understand. Could someone explain this to me in details and then suggest me a good reading on number bases, binary numbers and bitwise operations?
• 05-15-2005
dwks
Quote:

n = n & 0177;
Binary numbers first. Binary is a numbering system that has only two digits: 1 and 0. Decimal, with which you are probably familiar, has 10 digits: 0 - 9 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). In decimal, you can count up to 9 without requiring 2 digits. But as soon as you count to ten, you need two digits, 10. The same goes for binary. In binary, you can count to one (1). If you count to two, you need two digits, 10. Three in binary is 11. Here's a small list:

Decimal Binary Octal Hexadecimal
0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
8 1000 8 8
9 1001 10 9
10 1010 11 A
11 1011 12 B
12 1100 13 C
13 1101 14 D
14 1110 15 E
15 1111 16 F
16 10000 17 10

(Octal is similar, except octal is base 8 (0, 1, 2, 3, 4, 5, 6, 7, 8). Hexadecimal is base 16 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F).)

AND is simple.

1001
& 1100
----
1000

Both of the bits have to be 1 (true) for the result to be 1.

Anyway, bytes in computer memory are stored with 8 bits, and can represent 256 numbers (2 exponent 8 = 256). The low-order bits are the first four, the ones you can represent 8 with. In your equation, it's like you're doing this:

n
& 00001111
--------

No matter what the left bits in n are, the result can't be 1, since one of the involved bits is zero. The four right bits are left untouched; that is, if they are 1 in n, they're 1 in the result. In effect, the equation "clears" the four left-hand bits.

I hope this helps. :)
• 05-15-2005
Stoned_Coder
0177 octal is 7f hex or 127 decimal

lets look at the bit pattern of the last byte.

01111111

anything anded with a 0 is 0
a 1 anded with a 0 is 0
a 1 anded with a 1 is 1

hence this does not set to zero all the lower order bits on n. it strips the lower order bits of n from the most significant bit.
If you wish to set all the lower order bits in n to 0 then and it with 128 decimal. bit pattern...
10000000

This will preserve the most significant bit but set the other 7 to zero as anything anded with 0 is 0.
• 05-15-2005
dwks
The Windows calculator can convert between decimal, binary, hex, and octal. (Just switch to scientific mode.) Most other calculators can, too, like the Mac OS 10.4 one and my Linux one.
• 05-15-2005
dwks
Some other bitwise operators are OR, NOT, and XOR. Following are some "truth tables":

OR
in1 in2 out
0 0 0
1 0 1
0 1 1
1 1 1

XOR
in1 in2 out
0 0 0
1 0 1
0 1 1
1 1 0

NOT
in out
0 1
1 0

I like NOT myself. :)