Bitwise Operators in C Programming

 Welcome Back Programmers, Till now in every tutorial we have worked with different types of values, like integers,floats,Strings and much more. As we know Strings are nothing but sequence of characters, but ever thought how are computer may understand all these characters? How the computer so ghastly performs operations on numbers and other type of Data? In this tutorial we are going to learn about the system of Bits and the different operators that are used to perform operations on them.

What are Bits?

Since we know that computer cannot understand the human-readable language. So it converts the data into binary form, i.e in 1's and 0's. Each single digit in the binary number is known as Bit. The number 7 when converted to binary number gives 1010110, here every single digit is a bit. 1 bit is equal to 1/8th of a byte. Or we can say a Byte is equal to 8 bits. In-fact bit is the smallest unit of  digital information.

Concluding about bits, Bits are the basic unit of information in computing and digital communication. It represents a logical state with one of two possible values, i.e either 1 or 0. 

* A bit actually consists of 8 digits, like 10100011 or 00001111, but here in some examples we'll consider only first 4 bits from every number, like as 5 = 0000 0101 but here in some cases we'll take 5 = 0101. 

Need of Operating with Bits?

Operations on Bits are used in various Programming tasks like low-level device detection, error detection and correction algorithms, data compression, encryption algorithms and Optimisation, low-level programming on embedded Systems.

What are Bit-wise Operators?

C provides us with a special set of operators (other than Unary, Binary and Ternary Operators), known as Bit-wise Operators, These are used in Bit level programming. We can manipulate the bits of an Integer Expression using Bit-wise Operators.
Different Types of Operators are: 
At Bit level, all the arithmetic operations like Addition, Subtraction, Division etc are done using these Operators.

Now, lets study each of them one by one...

Bit-wise AND Operator ( & )

This operator take 2 binary numbers and perform AND operation on them, this is like the multiplication operation performed in maths. The output of the operation on 2 bits is as follows:
Here the result(R) is 1 if both the corresponding bits of the operands are 1, otherwise if either bit of any operand is 0, then the result/output would be 0.

Example :
10 in binary is 1010 and 9 is 1001, if we perform & operation on them, we'll get 1000
which is the decimal equivalent of 8. Hence, 10 & 9 = 8

Bit-wise OR Operator ( | )

It also take 2 operands, and do a operation similar to the Addition operation in Mathematics, just with a little difference.
Here the result is 1, when any of corresponding bits of either operands is 1, but if both the bits are 0, then the output is zero.

Example :
Let us again consider the above two numbers, 10 and 9, i.e 1010 and 1001 in binary representation, now the output would be 1011
which is the decimal equivalent of 11. Hence 10 | 9 = 11

Bit-wise XOR Operator ( ^ )

It is also known as the Exclusive OR operator. Its output is 1 when the corresponding bits are opposite in nature like 0-1 or 1-0, and if the corresponding bits are same, then the output is 0.
Example :
10 ^ 9 = 1010 ^ 1001 = 0011
which is the decimal equivalent of 3

Bit-wise NOT Operator ( ~ )

It is a unary operator, i.e it works on only 1 operand, and flip each bit of the number. Meaning if the bit is 1 it flips it to 0, and 0 to 1. This is known as the Logical negation of bits.
Example :
~ ( 9 ) = 6 , This negation compliment obtained is known as 1's Complement. There is an another type of compliment known as 2's Compliment, where negation is done 1 added to the 1's compliment found.

Hence 2's compliment of X = - ( n + 1 ).

2's complement of 7 (0111) is 9 (1001)

Bit-wise Right Shift Operator ( >> )

The Bit-wise Right Shift Operator, right shifts all the bits of the operand placed at the left side, in accordance to the value on the right side, which tells us the number of positions to shift. 

Syntax: x >> y, which is similar to dividing x with 2 raise to power y.

Example 1 : 
5 >> 1, hence we have to shift the bits of 0101(binary representation of 5), 1 position to the right.
On shifting the bits to right, the empty positions on the left side will be occupied by 0s.
So 5 >> 1 = 2, which is decimal representation of 0010.

Example 2 :
5 >> 2 
0101 >> 2 = 0001
Hence, 5 >> 2 = 1

Bit-wise Left Shift Operator ( << )

The Bit-wise Left Shift Operator, left shifts all the bits of the operand placed on the left side, in accordance to the value on the right side.

Syntax : x << y, this expression shifts the bits of x, y places to the left. And the empty positions are occupied by 0s.

x << y is equivalent to multiplying x with 2 raise to power y.

Example 1 :
5 << 1, will shift the bits of 5(0101) to the left by 1 place.
which is equivalent to 10 in decimal format.

Example 2 :
5 << 2 
0101 << 2 = 0100
Hence, 5 << 2 = 20        // 0000 0101 <<  2 = 0001 0100

Full Program on Bit-wise operators

#include <stdio.h>
int main()
{
  short int a = 5, b = 6;            // Assume short int is of 8-bits, then
  printf("a & b=%d\n", a & b);
      // 0000 0101 = 5
      // 0000 0110 = 6
  //AND= 0000 0100 = 4

  printf("a | b=%d\n", a | b);
      // 0000 0101 = 5
      // 0000 0110 = 6
  //OR = 0000 0111 = 7

  printf("a ^ b=%d\n", a ^ b);
      // 0000 0101 = 5
      // 0000 0110 = 6
  //XOR= 0000 0011 = 3

  printf("~a=%d\n", ~a);
      // 0000 0101 = 5
  //NOT= 1111 1010 = -6
  
  int right_shift = a >> 1;
  printf("a >> 1=%d\n", right_shift);
  //      0000 0101 >> 1 = 0000 0010
  //      0000 0010 = 2
  
  int left_shift = a << 1;
  printf("a << 1=%d\n", left_shift);
  //      0000 0101 >> 1 = 0000 1010
  //      0000 1010 = 10

  return 0;
}

OUTPUT 

a & b=4
a | b=7
a ^ b=3
~a=-6
a >> 1=2
a << 1=10

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post