| « Back into binary | That was remarkably painless » |
Fri, Jan 19, 2007
![[Icon]](rsc/img/chain_link.gif)
I started reading K&R section 2.9 - bitwise operators. And fell at the first hurdle.
Apparently, x = n & 0177 sets to zero all but the low order bits of n.
Of course it does. Obviously. What else would it do?
Err. . .
Okay: Let's think about this. Bitwise operations must mean getting at the actual binary bits that make up an integer, rather than dealing with bytes. But why does one hundred and seventy seven wipe out the high-order bits?
Thank God for C programming notes where I worked out what I was missing: There's a leading zero on that "one hundred and seventy seven"
And leading zeros in programming means what? Yep, it means it's not a decimal value, it's an octal.
Funnily enough, although I can do binary, decimal, or hexadecimal pretty well in my head, octal is a base that just passed me by. Possibly because you can't spell out interesting words on a scientific calculator with it like you can in Hex. . .
So, unable to rely on knowledge, I fell back on the pen & paper I like to keep near me when learning anything, and did a quick working-out of how octal digits work in translation to binary.
0 = 000 1 = 001 2 = 010 3 = 011 4 = 100 5 = 101 6 = 110 7 = 111
And suddenly, everything becomes clear!
177 as an octal translated to binary therefore makes perfect sense:
1 | 7 | 7 001|111|111
Since a byte is eight bits, and 177 equates to seven ones, using the AND operator means that 177 will set the first bit of a byte to zero and leave the rest alone.
So I'll keep my little table of octal-binary conversions at hand, and continue reading this chapter, secure in the knowledge that I do actually understand WTF it's talking about ![]()
![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12
Facebook Syndication Error
09/02/12
![]()
I last listened to:
Johann Pachelbel - Canon in D major
Most recent photo:
Submersible houseboat