Question about the book "The C Programming Language"

This is a discussion on Question about the book "The C Programming Language" within the C Programming forums, part of the General Programming Boards category; Hi. I'm reading The C Programming Language and I am having a hard time understanding this. On page 48, when ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    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?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    Last edited by dwks; 05-15-2005 at 01:17 PM.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input/output question
    By vice in forum C Programming
    Replies: 8
    Last Post: 04-27-2005, 08:17 AM
  2. basic recursion function question. typo in book?
    By navyzack in forum C Programming
    Replies: 6
    Last Post: 04-18-2005, 11:07 AM
  3. Can't display book and borrower's details.
    By grscot in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2003, 10:18 AM
  4. Template question
    By grscot in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2003, 03:12 PM
  5. Replies: 3
    Last Post: 03-26-2003, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21