The & Operator

Discussion in 'C Programming' started by Albert, Dec 31, 2005.

  1. Albert

    Albert Guest

    Hello,

    I'm having trouble understanding what K&R are saying: 'The bitwise AND
    operator & is often used to mark off some set of bits, for example,

    n = n % 0177;

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

    So in the end, what actually happens? What does the value of n become?
    Albert, Dec 31, 2005
    #1
    1. Advertising

  2. Albert

    Albert Guest

    And could someone give me an example of when the & operator is useful
    or needed?
    Albert, Dec 31, 2005
    #2
    1. Advertising

  3. "Albert" <> writes:
    > I'm having trouble understanding what K&R are saying: 'The bitwise AND
    > operator & is often used to mark off some set of bits, for example,
    >
    > n = n % 0177;


    You mean "n = n & 0177";

    > sets to zero all but the low-order 7 bits of n.'
    >
    > So in the end, what actually happens? What does the value of n become?


    Think of the value of n as binary. 0177 is an octal constant; in
    binary, it's 01111111. Ideally, n should be unsigned (bitwise operators
    normally shouldn't be used with signed types).

    For single-bit operands, the "&" operator is defined as:
    0 & 0 == 0
    0 & 1 == 0
    1 & 0 == 0
    1 & 1 == 1

    Suppose the value of n (in binary) is 00010100111001111000101101001100
    (32 bits). Then and-ing that value with 0177 yields:

    00010100111001111000101101001100
    & 00000000000000000000000001111111
    --------------------------------
    00000000000000000000000001001100

    As you can see, this does just what K&R say it does: it zeros all but
    the low-order 7 bits of n.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 31, 2005
    #3
  4. Albert <> schrieb:

    > And could someone give me an example of when the & operator is useful
    > or needed?


    I guess you mean a real-world example ...

    Think of some code-base that uses for example the following definition
    for error values to set or return:

    // Error-categories
    #define CAT_FILEOP 0x0100
    #define CAT_WHATEVER 0x0200

    #define ERR_NOSUCHFILE (CAT_FILEOP|0x01) // '|' means OR where '&' means AND
    #define ERR_FILEEXISTS (CAT_FILEOP|0x02)

    // ...

    #define ERR_OUTOFMEM (CAT_WHATEVER|0x01)

    Then you can use:

    void check_error(int _err)
    {
    int cat,err;

    cat = _err&0xf00; // mask out the specific error bits and get the category
    err = _err&0x0ff; // mask out the category bits and get the error
    hope("this helped"); // ;-)
    }

    Markus
    Markus Becker, Dec 31, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    550
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    408
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,029
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    4,992
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,656
    Kanenas
    May 30, 2005
Loading...

Share This Page