Bitwise Operators

Bitwise Operators

Published by: Nuru

Published date: 21 Jun 2021

Bitwise Operators Photo

Bitwise Operators

  • C has special operators for bit-oriented programming known as bitwise operators.
  • Bitwise operators in C are used for manipulating data in a bit level. These are used to testing the bits or shifting the bits left or right.
  • Bitwise operators are not applied to float and double.
  • The following table shows the bitwise operators and their meanings.
Operator Meaning
& Bitwise AND
| Bitwise OR
& Bitwise AND
^ Bitwise X-OR
<< Shift left
>> Shift right

Bitwise Logical operators

  • The three operators &, |, ^ act on integral expressions only.
  • The two operands of these operators are operated bit by bit.
  • The following table shows the bitwise operator acting on 1-bit fields.
a b a & b a| b a ^ b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Bitwise AND (& )The & operator is surrounded by its two integral operands. The result of AND operations is 1 if both the bit is 1 otherwise 0.Example: For 4-byte word machine:a = 00000000 00000000 00000000 00001101 ( 13 DEC )
b= 00000000 00000000 00000000 00011001 ( 25 DEC.)

  • if we execute statement: c=a&b; then

c= 00000000 00000000 00000000 00001001 ( DEC 9)Bitwise OR ( | )The | operator is also surrounded by its two integral operands. The result of ORing operations is 1 if at least one of the bit of its operand is 1
otherwise 0.Example:a = 00000000 00000000 00000000 00001101 (13 D )
b= 00000000 00000000 00000000 00011001  (25 D)

  • if we execute statement: c=a| b; then

c= 00000000 00000000 00000000 00011101 ( 29 D)Bitwise Exclusive OR ( ^)The ^ operator is also surrounded by its two integral operands. The result of X-OR operations is 1 if only one of the bit of its operand is 1
otherwise 0.
Example below:
a = 00000000 00000000 00000000 00001101 (13 D )
b= 00000000 00000000 00000000 00011001 (25 D)

  • if we execute statement: c=a^b; then

c= 00000000 00000000 00000000 00010100 (20 D)
/*Program to demo bitwise logical operators*/
#include
main()
{
int a=13,b=25,c;
c=a&b;
printf("%d&%d=%d",a,b,c);
c=a|b;
printf("\n%d|%d=%d",a,b,c);
c=a^b;
printf("\n%d^%d=%d",a,b,c);
return 0;}Bitwise Shift operatorsThe shift operators are used to shift the bit of any integral expression to the left or right as indicated by the number of bits.

  • The left shift operators( <<): The left-shift operator takes the form: expr<< n
  •  Which causes the bits representation of expr to be shifted to the left by the number of places specified by n.
  • The leftmost n bits in the original bit pattern will be lost and rightmost n bit positions that are vacated will be filled with 0s
  • e.g. 0000 1111<< 2 produces 0011 1100
  • The decimal value of 0000 1111 is 15 and the decimal value of 0011 1100 is 60.
  • So left shift by 1, multiply the number by 2. left shift by 2, multiply the number by 4, and so on...

The shift right operator (>> )

  • The left shift operator takes the form: expr1 >> n which causes the bits representation of expr1 to be shifted to the right by the number of places specified by n.
  • The rightmost n bits in the original bit pattern will be lost and leftmost n bit positions that are vacated will be filled with 0s
    e.g.
    0000 1111>> 2 produces 0000 0011
  • The decimal value of 0000 1111 is 15 and the decimal value of 0000 0011 is 3
  • So right shift by 1, divides the number by 2, (integer division), right shift by 2 bits divides the number by 4.