what does this mean, bitwise operator

This is a discussion on what does this mean, bitwise operator within the C++ Programming forums, part of the General Programming Boards category; im kinda confused on what exactly this means/does. Code: if (n & 1) //action n is a parameter to a ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    what does this mean, bitwise operator

    im kinda confused on what exactly this means/does.
    Code:
    if (n & 1)
    //action
    n is a parameter to a recursive function and the function works, but what does the & (bitwise operator) does. From what i can understand is that when n is eventually 1 then the recursive function is called for the last time and then it returns the result. Am i right?
    When no one helps you out. Call google();

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    Lets say that (in binary) the variable n has the following contents:

    01010101010101010101010101010101

    When we perform the bitwise AND of that value with 1 which is:

    00000000000000000000000000000001

    ... we get the resulting value:

    00000000000000000000000000000001

    ...which will evaluate to 'true' for the purposes of the if test. Basically the test is determining if the low order bit is set.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    n is a parameter to a recursive function and the function works, but what does the & (bitwise operator) does. From what i can understand is that when n is eventually 1 then the recursive function is called for the last time and then it returns the result. Am i right?
    Yes, but it really only means that when n = 0 it will end

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    n & 1 evaluates to 1 if the last bit in the number is set, and it evaluates to 0 if it is not. Since 1 means true and 0 means false, the action is performed if the last bit is set in the number. If n is unsigned, then (n & 1) will be true whenever n is odd, so whether n is eventually 1 or 3 or some other odd number doesn't matter. It will end when n is even.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    so that means that 0 is considered an even number?
    When no one helps you out. Call google();

  6. #6
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeå, Sweden
    Posts
    555
    n&1 is also the same as n%2

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by InvariantLoop
    so that means that 0 is considered an even number?
    What is:

    0000 0000
    0000 0001 &
    ------------


    ?

    What will happen to the loop when n=0?
    Last edited by 7stud; 03-09-2006 at 05:07 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> so that means that 0 is considered an even number?
    Yes, 0 is even because of the definitions of even and odd. http://en.wikipedia.org/wiki/Even_and_odd_numbers

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Daved
    >> so that means that 0 is considered an even number?
    Yes, 0 is even because of the definitions of even and odd. http://en.wikipedia.org/wiki/Even_and_odd_numbers
    So, if wikipedia had defined 0 as an odd number, would that affect what the loop does when n=0?
    Last edited by 7stud; 03-09-2006 at 05:48 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    No. Your question doesn't really make sense given the context of my statement. I think you may have misunderstood my point.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    4,831
    0 is defined mathematically as an even number. A formal mathematical definition is that an integer i is even if it is exactly equal to 2*j where j is also an integer. All integers that are not even are odd. This definition can be expressed in a number of equivalent forms (eg a number i is even if the remainder one dividing it by 2 is zero).

    Daved's link to wikipedia was for information, not a claim that life is defined by wikipedia.

  12. #12
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeå, Sweden
    Posts
    555
    An even number is a number that can be divided by 2 e.g. it doesn't leave any decimals/no remainder. Simple as that.

    0/2 = 0
    1/2 = 0.5
    2/2 = 1
    3/2 = 1.5
    4/2 = 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C bitwise operator exercise - more like newb killer
    By shdwsclan in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 06:02 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Replies: 5
    Last Post: 10-30-2002, 09:23 PM

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