Bitwise operator in c with suitable examples

Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long. In this article, we will see the basics of bitwise operators, and some useful tips for manipulating the bits to achieve a task. This article assumes that you know the basics of Truth Table for various operators.

C language supports the following bitwise operators.
  • | – Bitwise OR
  • & – Bitwise AND
  • ~ – One’s complement
  • ^ – Bitwise XOR
  • << – left shift
  • >> – right shift
Though we are calling it as a bitwise operators, it always operate on one or more bytes i.e, it will consider the whole representation of the number when applying bitwise operators. By using some techniques, we can manipulate a single bit on the whole representation of the number as we will see in later sections

Bitwise OR – |

Bitwise OR operator | takes 2 bit patterns, and perform OR operations on each pair of corresponding bits. The following example will explain it.
       1010
       1100
      --------
OR     1110
      --------
The Bitwise OR, will take pair of bits from each position, and if any one of the bit is 1, the result on that position will be 1. Bitwise OR is used to Turn-On bits as we will see in later sections.

Bitwise AND – &

Bitwise AND operator &, takes 2 bit patterns, and perform AND operations with it.
       1010
       1100
      -------
AND    1000
      -------
The Bitwise AND will take pair of bits from each position, and if only both the bit is 1, the result on that position will be 1. Bitwise AND is used to Turn-Off bits.

One’s Complement operator – ~

One’s complement operator (Bitwise NOT) is used to convert each “1-bit to 0-bit” and “0-bit to 1-bit”, in the given binary pattern. It is a unary operator i.e. it takes only one operand.
       1001
NOT
      -------
       0110
      -------

Bitwise XOR – ^

Bitwise XOR ^, takes 2 bit patterns and perform XOR operation with it.
       0101
       0110
      ------
XOR    0011
      ------
The Bitwise XOR will take pair of bits from each position, and if both the bits are different, the result on that position will be 1. If both bits are same, then the result on that position is 0.

Left shift Operator – <<

The left shift operator will shift the bits towards left for the given number of times.
int a=2<<1;
Let’s take the binary representation of 2 assuming int is 1 byte for simplicity.
Position 7    6    5    4    3    2    1    0
Bits 0    0    0    0    0    0    1    0
Now shifting the bits towards left for 1 time, will give the following result
Position 7    6    5    4    3    2    1    0
Bits 0    0    0    0    0    1    0    0
Now the result in decimal is 4. You can also note that, 0 is added as padding the in the position 0.
If you left shift like 2<<2, then it will give the result as 8. Therefore left shifting 1 time, is equal to multiplying the value by 2.

Right shift Operator – >>

The right shift operator will shift the bits towards right for the given number of times.
int a=8>>1;
Let’s take the binary representation of 8 assuming int is 1 byte for simplicity.
Position 7    6    5    4    3    2    1    0
Bits        0    0    0    0    1    0    0    0
Now shifting the bits towards right for 1 time, will give the following result
Position 7    6    5    4    3    2    1    0
Bits 0    0    0    0    0    1    0    0
Now the result in decimal is 4. Right shifting 1 time, is equivalent to dividing the value by 2.

Note on shifting signed and unsigned numbers

While performing shifting, if the operand is a signed value, then arithmetic shift will be used. If the type is unsigned, then logical shift will be used.
In case of arithmetic shift, the sign-bit ( MSB ) is preserved. Logical shift will not preserve the signed bit. Let’s see this via an example.
#include<stdio.h>

int main() {
    signed char a=-8;
    signed char b= a >> 1;
    printf("%d\n",b);
}
In the above code, we are right shifting -8 by 1. The result will be “-4″. Here arithmetic shift is applied since the operand is a signed value.
#include<stdio.h>

int main() {
    unsigned char a=-8;
    unsigned char b= a >> 1;
    printf("%d\n",b);
}
Note: Negative number are represented using 2′s complement of its positive equivalent.
2's compliment of +8 is

1111 1000

Right shifting by 1 yields,

0111 1100 ( 124 in decimal )
The above code will result in 124 ( Positive value ). Here logical shift is applied since the operand is unsigned, and it won’t preserve the MSB of the operand.

Comments

Popular posts from this blog

Tips 01) self introduction in an interview

Computer Science Terminology

Why Failure Is Good for Success