Bitwise Operators in C

Welcome to Geekstocode, In this post we are going to discuss the Bitwise Operators in C/C++.

This post is going to be a long post because we are going to discuss Bitwise operators completely and try to cover all the aspect.

So, without wasting your time let’s start the discussion

Before starting the Bitwise I think we should first discuss what the Operator is, Let’s know what is operator

Operator

Operator is a symbol and works on operand. Learn by following example

2+3;
a-b;
5*8;

In above example 2 and 3 are known as operand and + is operator.

You can also define operator as “which perform some calculations on operand”.

There are many types of operator present in c.

You are also using many different different types of operators in your daily life but you are not aware. Let’s summarize all the operator’s type

Unary Operator

Unary Operator are those which perform calculations on only one operand.

Example

Increment and Decrement operator are the examples of unary operator

++i;
i--;

Binary Operator

Binary Operator are those which perform calculation on two operand

Example

All the arithmetic operations are the example of Binary operators

a+b;
a%g;

Ternary Operator

Operator that works on three operands are known as Ternary Operator

Example

Below is the simple example of Ternary Operator

condition?:true:false;

I think it’s enough for operator discussion,

Before discussing bitwise, I think we should also discuss little bit about Bit.

What is a Bit

Basic unit of information is known as Bit. Bit is a short form of binary digit.

Bit only have value as 0 or 1. You also heard “Movie is of 2 GB”, “song size of 4 MB” all of these term has bit hidden in it.

Size of computer memory are increase as

  1. Bit
  2. Kilobyte
  3. Megabyte
  4. Gigabyte
  5. Terabyte

and so on.

How to find Binary of any number

Finding binary of any number is very easy. All you need is to divide the number by 2 till the number is greater than 2 and note down all remainder as stated in the below example

  1. Let’s take a number 13 and find it’s binary
  2. Divide 13 by 2 and note the remainder which is 1 also note down quotient which is 6
  3. Again divide new quotient by 2 and note down the remainder which is 0 and also note down new quotient which is 3
  4. Same, divide new quotient by 2 and note down the remainder which is 1 and also note down new quotient which is 1
  5. Now quotient is less than 2 note it down directly or you can perform step one more time but the quotient and remainder will same hence nothing change happens
binary of 13

Make sure you know about decimal to bit conversion because from now it is going to very very important.

Now, we are heading to our most important topic which is Bitwise operators

Bitwise Operators In C or C++

According to us, “Operators that perform some calculation on a bit of the operand are known as Bitwise Operator”.

There are mainly 6 types of Bitwise Operators in C –

  1. AND Operator represented by &
  2. OR Operator represented by |
  3. XOR Operator represented by ^
  4. Left Shift Operator represented by <<
  5. Right Shift Operator represented by >>
  6. Not Operator represented by ~

All the above operators are part of bitwise and all of them perform operations on a bit. Now, we know how to convert any decimal to binary, we are good to go learning all these

Bitwise And Operator

This is a binary operator which means it performs operations on two operands and does the AND of every bit of two numbers. It is denoted by &.

: AND of two bit gives 1 if both bit are 1 else 0

1 & 1 = 1

1 & 0 = 0

0 & 0 = 0

0 & 1 = 0

Let’s understand this by a example –

Find the bitwise AND of 7 and 5

For finding this first we need to convert the 7 and 5 in binary form –

  1. Binary of 7 is 111
  2. Binary of 5 is 101

Now, we need to perform bitwise And, write down their bit representation as

1 1 1
1 0 1
_____
1 0 1
_____

Now, convert 101 to decimal which is 5.

Hence, bitwise and of 7 and 5 is 5.

Try some of these questions to get your hand dirty

  1. 10 and 43
  2. 34 and 41

Program for Bitwise AND Operator in C

Below is the Program for Bitwise AND in c you can convert it in any language.

You can access this code from ideone

#include <stdio.h>

int main(void) {

int a , b;
printf("Ënter the number");
scanf("%d %d",&a,&b);
printf("Bitwise AND is %d ",a&b);
	return 0;
}

Output: Bitwise AND is 5 (if input is 7 and 5)

Bitwise OR Operator

This is a binary operator which means it performs operations on two operands and does the OR of every bit of two numbers. It is denoted by |.

OR of two bit is 1 if any of the bit is 1 otherwise 0

1 | 1 = 1

1 | 0 = 1

0 | 1 = 1

0 | 0 = 0

Find the bitwise OR of 7 and 5

  1. Binary of 7 is 111
  2. Binary of 5 is 101

Given is the bitwise OR Calculation

1 1 1
1 0 1
-----
1 1 1
-----

Now convert 111 to decimal which is 7, hence 7 is bitwise OR of 7 and 5

Practice these question and Find Bitwise OR

  1. 12 and 32
  2. 22 and 43

Program for Bitwise OR Operator in C

Check the Program in C language and also you can access the code from ideone.

#include <stdio.h>
 
int main(void) {
 
int a , b;
printf("Ënter the number");
scanf("%d %d",&a,&b);
printf("Bitwise OR is %d ",a|b);
	return 0;
}

Bitwise OR id 7 if input are 7 and 5

Bitwise XOR Operator

This is a binary operator which means it performs operations on two operands and does the XOR of every bit of two numbers. It is denoted by ^.

XOR of two bit is 1 if both bit are different and 0 in other cases

1 ^ 1 = 0

1 ^ 0 = 1

0 ^ 1 = 1

0 ^ 0 = 0

Find the bitwise XOR of 7 and 5

  1. Binary of 7 is 111
  2. Binary of 5 is 101

Given is the bitwise XOR Calculation

1 1 1
1 0 1 
-----
0 1 0
-----

Now, convert the 010 to decimal which is 2 hence, 2 is xor of 7 and 5

Try to find XOR of following

  1. 32 and 2
  2. 12 and 44

Program for Bitwise XOR in C

Here is the program for bitwise xor, you can run the code here

#include <stdio.h>
 
int main(void) {
 
int a , b;
printf("Ënter the number");
scanf("%d %d",&a,&b);
printf("Bitwise XOR is %d ",a^b);
	return 0;
}

Bitwise Left Shift Operator

This operator is a binary operator used to shift the binary of a number to the left. This is denoted by <<.

Suppose this is written.

Example: 8 << 2

Above example means is, shift binary of 8 by 2 places to left and add two zeros to right.

Binary of 8 is 00001000 (number of bit is 8 as we are using 8 bit to represent binary)

Shifting 2 places left : 00100000

Note: Left shift operator does not work on negative integers

8 << 2 means is multiply 8 with 2^2;

8 << 2 equals 00100000 whose decimal is 32 (same as 8 multiply with 2^2)

8 << 1 means is multiply 8 with 2^1;

8 << 1 equals 00010000 whose decimal is 16 (same as 8 multiply with 2^1)

Program for Bitwise Left Shift Operator

Here is the program for bitwise left shift operator.

/* C++ Program to demonstrate use of left shift
operator */
#include<stdio.h>
int main()
{
	
	int a = 8,b=2;

	
	printf("left shift is %d",a<<b);
	return 0;
}

Bitwise Right Shift Operator

This operator is a binary operator used to shift the binary of a number to the right. This is denoted by >>.

Suppose this is written

Example : 8 >> 2

This means we need to shift the binary of 8 by 2 places to the right.

Binary of 8 is 00001000

shifting two places right 00000010

Note: Right shift operator does not work on negative integers

8 << 2 means is divide 8 with 2^2;

8 << 2 equals 00000010 which is equals to 2(same as 8 divide by 2^2)

8 << 1 equals 000000100 which is equals to 4 ( same as 4 divide by 2^1)

Program for Bitwise Right Shift Operator

Here is the program for bitwise right shift operator.

/* C++ Program to demonstrate use of left shift
operator */
#include<stdio.h>
int main()
{
	
	int a = 8,b=2;

	
	printf("left shift is %d",a>>b);
	return 0;
}

Bitwise NOT Operator (Bitwise compliment Operator)

This is a unary operator which means it perform operations on only one operand. This operator is represented by ~.

This operator inverts all the bits of number or you can say reverse the bit pattern for each individual bit.

Let’s understand by the example:

Find bitwise NOT of 8

Binary of 8 = 1000 [4 Bit representation]

~8 (Invert all bits) = 0111

Program for Bitwise NOT

You can check the code for bitwise not

#include<stdio.h>
int main(void) {



unsigned int a = 8;
printf("Bitwise NOT Of 8 is %u",~a);
	return 0;
}

You might be confuse in the ideone code editor, why the NOT of 8 comes to be negative if you use %d in place of %u, but there is a logic,

NOT of small numbers leads to greatest number and since the value does not fit in %d it shows the negative value

Bitwise Operator Interview/Coding Questions

  1. Interesting XOR – Cakewalk
  2. XOR Engine – Simple
  3. XOR Subset – Simple
  4. Chef and Medium problem – Medium
  5. Prefix Sub array – Medium

Important Notes

  1. XOR Operator is widely used in competitive coding and also in interview questions.
  2. Bitwise Operator and Logical operators are different
  3. Left and Right shift are equivalent to Multiplication and division by 2
  4. NOT of small numbers give a very large number.

Conclusion

Hope you love this article on Bitwise Operators in C. We spend many hours to write and present it to you. Feel free to share and comment.

Which are Bitwise Operator

Operators that perform operations on the bit of operands are known as bitwise operator

Are Bitwise Operator Faster

Yes, Bitwise Operator are considerd as faster and it give you advantage in competitive coding

What are the three logical operators

AND , OR , NOT are three logical operators

Leave a Comment

Your email address will not be published. Required fields are marked *