Monday, November 28, 2011

Bitwise operation

A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits.
NOT:-The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value.
NOT 0111 (decimal 7)
= 1000 (decimal 8)
The ~ (tilde) operator performs a bitwise complement on its single integer operand.
OR:-A bitwise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits .
0101 (decimal 5)
OR 0011 (decimal 3)
= 0111 (decimal 7)
The | (vertical bar) operator performs a bitwise OR on two integers.
The bitwise OR may be used in situations where a set of bits are used as flags;

XOR:-A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. For example:
0101 (decimal 5)
XOR 0011 (decimal 3)
= 0110 (decimal 6)
The ^ (caret) operator performs a bitwise exclusive-OR on two integers.
0 1 0 1 0 1 1 0
^ 0 0 1 1 0 0 1 0
---------------
0 1 1 0 0 1 0 0
Bit shift:-
The << operator shifts its first operand left by a number of bits given by its second operand, filling in new 0 bits at the right.
The >> operator shifts its first operand right. If the first operand is unsigned, >> fills in 0 bits from the left, but if the first operand is signed, >> might fill in 1 bits if the high-order bit was already 1.
For example, 0x56 << 2 is 0x158:
0 1 0 1 0 1 1 0 << 2-------------------
0 1 0 1 0 1 1 0 0 0
And 0x56 >> 1 is 0x2b:
0 1 0 1 0 1 1 0 >> 1
---------------
0 1 0 1 0 1 1
AND:-A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits.
the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. For example:
0101 (decimal 5)
AND 0011 (decimal 3)
= 0001 (decimal 1)
The & operator performs a bitwise AND on two integers.
0 1 0 1 0 1 1 0
& 0 0 1 1 0 0 1 0 --------------
0 0 0 1 0 0 1 0

No comments:

Post a Comment