The & Operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Albert

    #1

    The & Operator

    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

    #2
    Re: The & Operator

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

    Comment

    • Keith Thompson

      #3
      Re: The & Operator

      "Albert" <albert.xtheunk nown0@gmail.com > writes:[color=blue]
      > 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;[/color]

      You mean "n = n & 0177";
      [color=blue]
      > 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?[/color]

      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 000101001110011 110001011010011 00
      (32 bits). Then and-ing that value with 0177 yields:

      000101001110011 110001011010011 00
      & 000000000000000 000000000011111 11
      --------------------------------
      000000000000000 000000000010011 00

      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_Keit h) kst-u@mib.org <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.

      Comment

      • Markus Becker

        #4
        Re: The &amp; Operator

        Albert <albert.xtheunk nown0@gmail.com > schrieb:
        [color=blue]
        > And could someone give me an example of when the & operator is useful
        > or needed?[/color]

        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|0x0 1) // '|' means OR where '&' means AND
        #define ERR_FILEEXISTS (CAT_FILEOP|0x0 2)

        // ...

        #define ERR_OUTOFMEM (CAT_WHATEVER|0 x01)

        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

        Comment

        Working...