[1+1=2]

OneAndOneIs2

« Back into binaryThat was remarkably painless »

Fri, Jan 19, 2007

[Icon][Icon]Coming to a grinding halt

• Post categories: Omni, Programming

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 [Smiley]

1 comment

Andrew
Comment from: Andrew [Visitor] · http://andrews.co.nr
Just learn Pascal!!! Lol. Not that it is going to make things all that easier in logical sequential problem solving areas like this, but in general, it's a much easier, more extensible, awesome language to use. Of course, you're going to NEED to use an OOP based IDE to start with. I don't know if Linux even has one of these for Pascal, but a good Windows alternative is Delphi. Any other way, you'll just be annoyed. (Does Linux even SUPPORT Pascal?)

Of course, most of what I just said is invalidated, because you use Linux (I would too, if I could get printing working with Samba and CUPS).

Overall I see your rationale behind learning C (because Linux can utilize that language in various ways without problems), but there are much easier to use languages out there that will speed up your development time and enjoyment. Unfortunately, the really easy, awesome languages and IDE's are bottled up in Windows.
20/01/07 @ 04:47

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12

[Icon][Icon] Facebook Syndication Error
09/02/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
Submersible houseboat

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution free blog software