# what does this mean, bitwise operator

• 03-09-2006
InvariantLoop
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?
• 03-09-2006
hk_mp5kpdw
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.
• 03-09-2006
Quote:

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
• 03-09-2006
Daved
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.
• 03-09-2006
InvariantLoop
so that means that 0 is considered an even number?
• 03-09-2006
OnionKnight
n&1 is also the same as n%2
• 03-09-2006
7stud
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?
• 03-09-2006
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
• 03-09-2006
7stud
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?
• 03-09-2006
Daved
No. Your question doesn't really make sense given the context of my statement. I think you may have misunderstood my point.
• 03-09-2006
grumpy
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.
• 03-09-2006
OnionKnight
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