Used in algorithms where instead of performing operations like add, subtract etc. on the decimal numbers, those are performed at bit level.
Bitwise operators are used in a class of algorithms where instead of performing common operations like addition, comparisons, etc on the decimal numbers, those are performed on the individual bits that comprise those numbers. The bitwise operations are considered very efficient as compared to their decimal counterparts.
Following is the list of bitwise operators:
Bitwise AND (&): Performs bitwise AND on the individual bits of the two operands. The result of this operation results in a set bit if the two operands were also set (1)and unset (0) otherwise.
Bitwise OR (|): Used to perform OR operation on individual bits of the two operands. The resultant bit is set if either of the two operand bits was set and zero otherwise.
Bitwise XOR (^): Sets the bit only when the two operand bits were different.
Left Shift («): Takes two numbers (like a « b) and left shifts the bits of the first operand by the number denoted by the second operand.
Right Shift (»): Takes two numbers (like a » b) and right shifts the bits of the first operand by the number denoted by the second operand.
Bitwise NOT (~): A unary operator, bitwise NOT is used to flip all the bits of the operand number.
The number of bits used to represent a number in binary notation is given by:
To generate a number with only
To set the
To clear the
To toggle the
All power of two numbers have only one bit set. Example: 2(010), 4(100), 8(1000) and so-on.
The & operator can be used to check if a number x is even or odd. For all even x, x&1=0 and 1 otherwise. Example: 2(010) & 1(001)= 0; 6(110) & 1(001) = 0, 3(011) & 1(001) = 1
« and » are equivalent to multiplication and division by 2 respectively.
2’s complement, used to represent negative numbers = 1’s complement + 1. Another method of calculating 2’s complement is to subtract the number from
2’s complement of a number N, has all the bits toggled except for the rightmost set bit.
Here are some useful applications of bitwise operations:
Using Left Shift: As we know that only 1 bit is set for number 1, and shifting it left by k-1 units will give a number having only
A non-zero result will denote that the bit is set: return (n & (1« (k - 1)) >= 1) ? true : false;
Using RightShift: As right shifting a number drops the lower order bits, to see if
As mentioned above, any power of 2 will have only 1 bit set in its binary representation like 2(010), 4(100), 8(1000), and so on. Also, it can be observed that subtracting 1 from any number (which is a power of 2) will lead to:
For Example:
From the examples mentioned above, we can observe that:
As seen in the above examples, subtracting 1 from a number (say x) will toggle all bits from right to left until the first set bit, and thus performing an & between x&(x-1) will unset the rightmost set bit.
Example:
As clear from the above examples, we can see that repeatedly doing an x & x-1 will eventually lead to x being zero in exactly that many iterations as is the number of set bits. This is due to the fact that in every iteration, the rightmost set bit is being unset.
Take 2’s complement of the given number (say N) as it will have all bits as reverted except the first set bit from right to left.
Do a bit-wise & of 2’s complement with the original number. The result will have only 1-bit set which will be the rightmost set bit in the original number i.e. calculate N & -N.
Calculate
Add 1 to the output of step 3.
Be notified of new posts. Subscribe to the RSS feed.