[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.)
 

[icon] Blogroll

[icon] Creative Hedgehog
This week has been learning about what a Systems Analyst is, how to think about systems, and to be very aware of gorillas. Gorillas? There is a problem with simply asking how the company wants you to solve their problem, or even what they think the problem is. It's called situational blindness, and it [...][Link to post]04/03/10 - FIT2001 week 1: beware gorillas!

[icon] Hari's corner
How do you awaken a sleepy lawyer in the mornings?[Link to post]07/03/10 - The Legalease - Episode 10

[icon] Place of Stuff
Genesis 19 is a fun little tale, full of reversals and surprises.  It begins with two angels going to visit Lot in Sodom.  Lot welcomes them in and offers a good foot washing and feeding session.  As is polite and in custom with receiving visitors.  Especially angels.  Shortly after they arrive, the local men turn [...]

[Link to post]
27/02/10 - The Bible ? Sod ?Em

[icon] Advice From a Single Girl
So I figure I'm like some kind of man whisperer or something.Well, no, that's not quite right, it's more like I'm the guy maturer. See, I just heard that Bird's in a relationship. Bird. And Smith, of course, got married.And the most irresponsible of my exes now has a legitimate grown up career and another has left his job and gone into finance.It's like they break up with me and then go ahead [Link to post]31/12/69 - They Should Make A Movie About Me

[icon] Nation
&#160; This was possibly the most ridiculous show I have seen in a long time and I can get Sky 1 I know ridiculous. It could be summed up in three sentences Do you know what's in your cereal? Want to? Read the label. Instead it went on for a hour about how evil the cereal [...][Link to post]27/10/09 - Dispatches ? do you know what?s in your breakfast? (warning...

Blogroll generated by MagpieRSS

[Links][icon] My links

[Icon][Icon] Dominic can drive for another year! My little Fiesta survived its ordeal by MOT :).
05/03/10

[Icon][Icon] I last listened to:
The Offspring - She's Got Issues

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


March 2010
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 30 31        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution free blog software