Bitwise Operators in C

One of C’s powerful features is a set of bit manipulation operators. These permit the programmer to access and manipulate individual bits within a piece of data

The various Bitwise Operators in C

OperatorMeaning
~One’s complement
>>Right shift
<<Left shift
&Bitwise AND
!Bitwise OR
^Bitwise XOR(Exclusive OR)
The various Bitwise Operators in C

Bitwise AND Operator

This operator is represented as &. Remember it is different than &&, the logical AND operator. The & operator operates on two operands. While operating upon these two operands they are compared on a bit-by-bit basis. Hence both the operands must be of the same type (either char or int). The second operand is often called an AND mask. The & operator operates on a pair of bits to yield a resultant bit

Table of AND Operator

First bitSecond bitFirst bit & Second bit
000
010
100
111
Bitwise AND Operator

Truth Table of AND operator

&01
000
101

Programme To test whether a bit in a number is ON or OFF

main( ) 
{ 
int i = 65, j ; 
 printf ( "\nvalue of i = %d", i ) ; 
 j = i & 32 ; 
 if ( j == 0 ) 
 printf ( "\nand its fifth bit is off" ) ; 
 else 
 printf ( "\nand its fifth bit is on" ) ; 
 j = i & 64 ; 
 if ( j == 0 ) 
 printf ( "\nwhereas its sixth bit is off" ) ; 
 else 
 printf ( "\nwhereas its sixth bit is on" ) ; 
} 

Output of this programme

Value of i = 65
and its fifth bit is off
whereas its sixth bit is on

Bitwise OR Operator in C

Bitwise OR Operator in C is that operator which is represented as |. The rules that govern the value of the resulting bit obtained after ORing of two bits is shown in the truth table below

!01
001
111
Bitwise OR Operator in C

Example of OR operator in C

main( ) 
{ 
 int b = 50 ; 
 b = b ^ 12 ; 
 printf ( "\n%d", b ) ; /* this will print 62 */ 
 b = b ^ 12 ; 
 printf ( "\n%d", b ) ; /* this will print 50 */ 
} 

Bitwise XOR Operator

The XOR operator is represented as ^ and is also called an Exclusive OR Operator. The OR operator returns 1, when any one of the two bits or both the bits are 1, whereas XOR returns 1 only if one of the two bits is 1

The truth table for the XOR operator

^01
001
110
Bitwise XOR Operator

The showbits( ) Function

showbits ( int n ) 
{ 
 int i, k, andmask ; 
 
 for ( i = 15 ; i >= 0 ; i-- ) 
 { 
 andmask = 1 << i ; 
 k = n & andmask ; 
 k == 0 ? printf ( "0" ) : printf ( "1" ) ; 
 } 
}

All that is being done in this function is using an AND operator and a variable andmask we are checking the status of individual bits. If the bit is OFF we print a 0 otherwise we print a 1.

These operators can operate upon ints and chars but not on floats and doubles.

One’s Complement Operator in C

On taking one’s complement of a number, all 1’s present in the number are changed to 0’s and all 0’s are changed to 1’s

Example

one’s complement of 1010 is 0101

one’s complement operator in action

main( ) 
{ 
 int j, k ; 
 for ( j = 0 ; j <= 3 ; j++ ) 
 { 
 printf ( "\nDecimal %d is same as binary ", j ) ; 
 showbits ( j ) ; 
 k = ~j ; 
 printf ( "\nOne’s complement of %d is ", j ) ;
showbits ( k ) ; 
 } 
} 

Output of this programme

Decimal 0 is same as binary 0000000000000000
One’s complement of 0 is 1111111111111111
Decimal 1 is same as binary 0000000000000001
One’s complement of 1 is 1111111111111110
Decimal 2 is same as binary 0000000000000010
One’s complement of 2 is 1111111111111101
Decimal 3 is same as binary 0000000000000011
One’s complement of 3 is 1111111111111100

Right Shift Operator

The right shift operator is represented by >>. It needs two operands. It shifts each bit in its left operand to the right. The number of places the bits are shifted depends on the number following the operator (i.e. its right operand)

Thus, ch >> 3 would shift all bits in ch three places to the right. 
Similarly, ch >> 5 would shift all bits 5 places to the right.

Programme of Right Shift Operator

main( ) 
{
int i = 5225, j, k ; 
 printf ( "\nDecimal %d is same as binary ", i ) ; 
 showbits ( i ) ; 
 for ( j = 0 ; j <= 5 ; j++ ) 
 { 
 k = i >>j ; 
 printf ( "\n%d right shift %d gives ", i, j ) ; 
 showbits ( k ) ; 
 } 
}

Output of this programme

Decimal 5225 is same as binary 0001010001101001
5225 right shift 0 gives 0001010001101001
5225 right shift 1 gives 0000101000110100
5225 right shift 2 gives 0000010100011010
5225 right shift 3 gives 0000001010001101
5225 right shift 4 gives 0000000101000110
5225 right shift 5 gives 0000000010100011

Left Shift Operator

This is similar to the right shift operator, the only difference being that the bits are shifted to the left, and for each bit shifted, a 0 is added to the right of the number

Example of Left Shift operator

main( ) 
{ 
 int i = 5225, j, k ; 
 printf ( "\nDecimal %d is same as ", i ) ; 
 showbits ( i ) ; 
 for ( j = 0 ; j <= 4 ; j++ ) 
 { 
 k = i <<j ; 
 printf ( "\n%d left shift %d gives ", i, j ) ; 
 showbits ( k ) ; 
 } 
} 

Output of this programme

Decimal 5225 is same as binary 0001010001101001
5225 left shift 0 gives 0001010001101001
5225 left shift 1 gives 0010100011010010
5225 left shift 2 gives 0101000110100100
5225 left shift 3 gives 1010001101001000
5225 left shift 4 gives 0100011010010000

Leave a Comment