Bitwise Operators in C Programming

Bitwise operators in C programming can only be applied to integral operands: char, short, int and long, signed or unsigned

& bitwise AND
| bitwise inclusive OR
^ bitwise exclusive OR
<< left shift
>> left shift
~ one’s complement(unary)

Examples:

 1&1 = 1  1&0 = 0  0&1 = 0  0 &0 = 0
 1|1 = 1  1&0 = 1  0|1 = 1  0|0 = 0
 1^1 = 0  1^0 = 1  0^1 = 1  0^0 = 0
 15<<1 = 30  30<<1 = 60  60<<1=120  0<<1=0
 6>>1 = 3  3>>1 = 1  1>>1 = 0  0>>1=0

x = x & 0xf;  //sets to zero but the last 4 bits
x = x & ~0xf;  //sets the last 4 bits of x to zero

 

Leave a Reply

Your email address will not be published. Required fields are marked *