C Programming

Bitwise Operators

Posted on 02nd May 2013

Bitwise operators are used to perform operations on individual bits of the operand or operands. Each bit in the operand will be either 0 or 1. Bitwise operators in C are:

OperatorMeaning
&Bitwise AND; If both bits are 1 then result is 1 otherwise 0
|Bitwise OR; If either one of the bit is 1 then result is 1 otherwise 0
^Bitwise Exclusive OR (XOR); If only one bit (NOT BOTH) is 1 then result is 1 otherwise 0
<<Left Shift; Moves the bit pattern to the left
>>Right Shift; Moves the bit pattern to the right
˜Complement; All 1 bits are changed to 0 and 0 bits are changed to 1

Example

Consider two bit patterns

a = 0000 1100
b = 0000 0110
OperationMeaningResult
a & ba AND b0000 0100
a | ba OR b0000 1110
a ^ ba XOR b0000 1010
a << 2a Left Shift 20011 0000
a >> 2a Right Shift 20000 0011

Post a comment

Comments

Nothing yet..be the first to share wisdom.