Using the '&' operator to show a value is odd ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Using the '&' operator to show a value is odd ?

    How does this work? Also - what about showing a value is even?

    eg:
    Code:
    int val = 3;
    if ( val & 1 ) print("I am odd");
    thank you
    ps this is not homework
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dissectcode
    How does this work? Also - what about showing a value is even?

    eg:
    Code:
    int val = 3;
    if ( val & 1 ) print("I am odd");
    thank you
    ps this is not homework
    Write down the binary representation of a couple of integers and pay special attention to the rightmost bit for odd and even numbers. The '&' operator is the bitwise-and operator. Does that ring a bell?

    kind regards,

    Jos

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Consider the binary representation of a non-negative integer. It can be shown that the integer is odd if and only if the low-order bit is set.

      What about negative integers? It depends on how your target hardware represents negative numbers. Two's-complement is by far the most common way to represent negative numbers. It can also be shown that a negative integer expressed in two's-complement notation is odd if and only if the low-order bit is set. There is some very small risk that your target uses some odd ball representation for negative numbers where this property doesn't hold.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        ........and
        Code:
        int val=22;
        if(val | 1)cout<<"I am even"<<endl;

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by whodgson
          ........and
          Code:
          int val=22;
          if(val | 1)cout<<"I am even"<<endl;
          Make that & instead of | and change 'even' to 'odd'. Your approach doesn't work.

          kind regards,

          Jos

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            Yes, pardon my thread 4 post.......I jumped to a rediculous conclusion. It was quite wrong.
            So, is the following an acceptable way (or even the only way) to identify an even integer using bitwise methods?
            Code:
            int n=31;
            int m=4;
            cout<<"m = 4 and n = 31"<<endl;
            if(n & 1)cout<<n<<" is an odd number"<<endl; 
            else cout<<n<<" is an even number"<<endl;
            if(m & 1)cout<<m<<" is an odd number"<<endl;   
            else cout<<m<<" is an even number"<<endl;
            It produced the following output:
            m = 4 and n = 31
            31 is an odd number
            4 is an even number

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              It is certainly an acceptable way, probably the best way, but it is certainly not the only way, for instance

              Code:
              unsigned m = 5;
              
              if (m << (sizeof m * 8 -1))
              {
                  // M is odd
              }
              else
              {
                  // M is even
              }
              Anything that isolates and allows you to test bit 0 of your integer should work.

              Using the | operator

              Code:
              #include "limits.h"
              
              unsigned m = 5;
              
              if (m | (UINT_MAX-1) == UINT_MAX)
              {
                  // M is odd
              }
              else
              {
                  // M is even
              }
              But what you have done the the universally recognised way to do it not least because it is simple and unlike my methods if performed on signed integers it doesn't rely on platform defined behaviour.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                Thanks Banfa (and JosAH)
                I`m trying to learn about 'bitwise' and 'bits' and bit manipulation but at the moment it`s pretty slow going.
                rgds
                bill

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  In the case of 22, the bits are 10110.

                  To see if a bit is on, like the right-hand bit, you AND your number with a mask that has the right-hand bit set. If the result is true the bit set, otherwise the bit is not set.

                  10110
                  00001
                  ---------
                  00000 <-- result

                  The result is false, the bit is OFF.

                  The C code is: 22 & 1

                  In this case, the bit is off.

                  Next, take the next bit to the left:

                  10110
                  00010
                  --------
                  00010 <-- result

                  Here the result is true so the bit is ON.

                  The C code is 22 & 2.

                  Comment

                  • whodgson
                    Contributor
                    • Jan 2007
                    • 542

                    #10
                    Yes, thanks wfc, i get the idea. A mask is quite a simple concept (not really as complicated as i thought).Will have a play round with that.
                    rgds.

                    Comment

                    Working...