Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if an integer is even or odd using bitwise operators

How do I check if an integer is even or odd using bitwise operators

like image 829
brisk man Avatar asked Apr 18 '11 09:04

brisk man


People also ask

How do you check if an integer is even or odd?

If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd. You can check for this using num % 2 == 1 .

Which Bitwise operators can be used to check whether a number is even or odd Quickly question Bitwise AND (&) Bitwise OR (|) Bitwise XOR Bitwise not (~)?

Correct answer: 1Bitwise AND (&) Operator can be used to check whether a number if EVEN or ODD, consider the statement (num & 1), this statement will return 1 if first bit of the number is High (1) else it will return 0. All ODD numbers have their firs bit 1 and ODD numbers have 0.

Which operator can be used to check if a number is odd or even?

Using Modulo Operator ( % ) This is the most used method to check whether the given number is even or odd in practice. Modulo operator is used to get the remainder of a division. For example, 5 % 2 returns 1, i.e., the remainder when divided 5 by 2.

How do you know if a number is Bitwise?

Now, all we need to do is, just check the LSB of a number. If it is 1, it is Odd number. If it is 0, the number is even. Using Bit-wise AND operator, we can check whether the LSB is 0 or 1.


1 Answers

Consider what being "even" and "odd" means in "bit" terms. Since binary integer data is stored with bits indicating multiples of 2, the lowest-order bit will correspond to 20, which is of course 1, while all of the other bits will correspond to multiples of 2 (21 = 2, 22 = 4, etc.). Gratuituous ASCII art:

NNNNNNNN |||||||| |||||||+−− bit 0, value =   1 (20) ||||||+−−− bit 1, value =   2 (21) |||||+−−−− bit 2, value =   4 (22) ||||+−−−−− bit 3, value =   8 (23) |||+−−−−−− bit 4, value =  16 (24) ||+−−−−−−− bit 5, value =  32 (25) |+−−−−−−−− bit 6, value =  64 (26) +−−−−−−−−− bit 7 (highest order bit), value = 128 (27) for unsigned numbers,                  value = -128 (-27) for signed numbers (2's complement) 

I've only shown 8 bits there, but you get the idea.

So you can tell whether an integer is even or odd by looking only at the lowest-order bit: If it's set, the number is odd. If not, it's even. You don't care about the other bits because they all denote multiples of 2, and so they can't make the value odd.

The way you look at that bit is by using the AND operator of your language. In C and many other languages syntactically derived from B (yes, B), that operator is &. In BASICs, it's usually And. You take your integer, AND it with 1 (which is a number with only the lowest-order bit set), and if the result is not equal to 0, the bit was set.

I'm intentionally not actually giving the code here, not only because I don't know what language you're using, but because you marked the question "homework." :-)

like image 150
T.J. Crowder Avatar answered Nov 03 '22 11:11

T.J. Crowder