BlogsDope image BlogsDope

Bitwise operators in C and C++

Aug. 8, 2017 C C++ OPERATOR 18591

This post is about explaining the bitwise operators of C and C++. Bitwise operators are used to perform bit-level operations in C and C++. It means that all the operations of bitwise operators will be performed on the binary values of the digits.

We are provided with following bitwise operators:

Operator Description
& Binary AND Operator
Binary OR Operator
^ Binary XOR
~ Binary One's Complement
<< Binary Left Shift Operator
>> Binary Right Shift Operator

 

Bitwise AND operator &


If both the bits of the two operands are 1 then the result is 1. If either of them is 0 then the result is 0.

0 & 0 is 0
0 & 1 is 0
1 & 0 is 0
1 & 1 is 1

Let's take an example of using "bitwise AND" on 7 and 11 to understand and see how it works.

The binary value of 11 is 00001011 and the binary value of 7 is 00000111.

The bitwise AND operator just performs the bitwise operation on each bit.

   00001011
& 00000111
----------------
   00000011 →  3 in decimal system

The first four bits of both the numbers are 0 and thus the first four bits of the result are also 0 (since 0 & 0 is 0). The next two bits are 1 and 0. Since both of them are not 1, therefore the resulting bits are also 0. The last two bits of both numbers are 1, and thus the last two digits of the resulting number are 1 (1 & 1 is 1).

#include <stdio.h>
int main()
{
    int a = 7;
    int b = 11;

    printf("%d\n",a&b);

    return 0;
}

Bitwise OR operator |


If at least any one of the two operands of the bitwise OR operator is 1 then the result is 1. The result is 0 only if both the operands are 0.

0 | 0 is 0
0 | 1 is 1
1 | 0 is 1
1 | 1 is 1

Let's use bitwise OR operator on 7 and 11.

   00001011
|  00000111
----------------
   00001111 → 15 in decimal

The first four bits of both the number are 0, so the first four bits of the result are also 0 (0 & 0 is 0). In the next four bits, at least one of the operand is 1, and thus the next four bits of the result are also 1.

#include <stdio.h>
int main()
{
    int a = 7;
    int b = 11;

    printf("%d\n",a|b);

    return 0;
}

Bitwise XOR (exclusive OR) operator ^


The bitwise XOR operator gives the result as 1 if the corresponding bits of two operands are opposite, and 0 if they are same.

0 ^ 0 is 0
0 ^ 1 is 1
1 ^ 0 is 1
1 ^ 1 is 0

Let's use bitwise XOR operator on 7 and 11.

   00001011
^  00000111
----------------
   00001100 → 12 in decimal

#include <stdio.h>
int main()
{
    int a = 7;
    int b = 11;

    printf("%d\n",a^b);

    return 0;
}

Bitwise complement operator ~


Bitwise complement operator changes all 0 to 1 and all 1 to 0 of its operand. It is a unary operator, i.e., it works on one operand.

~0 is 1
~1 is 0

~  00001011
-----------------
    11110100 → 244 in decimal

But when you try the execute this in C, the result will be -12 instead of 244. Now, let's learn why it is so.

#include <stdio.h>
int main()
{
    int a = 11;

    printf("%d\n",~a);

    return 0;
}

2's complement of a number


The above-discussed compliment is the 1's complement. So, 1's complement of a binary number is an another binary number obtained by switching its 0 to 1 and 1 to 0.

The 2's complement of a binary number is 1 added to its 1's complement. Let's take a number is 0000 0010 (which is 2 in decimal). It's 1's complement is 1111 1101 which we got by switching 0 to 1 and 1 to 0. Now, adding 1 to it will give the 2's complement.

    11111101
+ 00000001
-----------------
    11111110

Additions in binary:
0+0 = 0
0+1 = 1
1+0 = 1
1+1 = 10 (which is 0 and carry is 1)

Adding 1 to 1 in binary gives the digit 0 and a carry of 1. So, the right most bit is 0 (adding 1 and 1) and 1 is carried over to the next bit. So that bit becomes 1 (adding 0 to 0 is 0 and then adding the carry of 1 to it is 1).

Now, the important thing is that the negative numbers are stored as the two's complement of the positive counterpart. So, the -12 will be stored as the 2's complement of 12 i.e., 2's complement of 00001100. It's 1's complement is 11110011 and adding 1 to it gives us its 2's complement.

    11110011
+ 00000001
----------------
    11110100

So, the 2's complement of 12 is 11110100 which is same as the binary representation of 224 (11110100).

The complement operator (~) just flips the bits. The final value printed by the machine after this depends on the machine itself.

Right Shift Operator >>


The right shift operator shifts all the bits right by the number of bits specified by the right operand. For example, 00001011 >> 2 will shift the bits of 00001011 towards the right by 2 and will result in 00000010.

#include <stdio.h>
int main()
{
    int a = 11;

    printf("%d\n",a>>2);

    return 0;
}

Left Shift Ophifterator <<


The left shift operator shifts all the bits left by the number of bits specified by the right operand. For example, 00001011 << 2 will shift the bits of 00001011 towards left by 2 and will result in 00101100.

#include <stdio.h>
int main()
{
    int a = 11;

    printf("%d\n",a<<2);

    return 0;
}

Liked the post?
Developer and founder of CodesDope.
Editor's Picks
4 COMMENTS

Please login to view or add comment(s).