Update appNew update is available. Click here to update.
Last Updated: Dec 8, 2023
Easy

Bitwise Operators in C/C++

Author Rhythm Jain
0 upvote

Introduction

Bitwise operations in C/C++ are asked frequently in programming interviews as well as competitive programming. Therefore it's essential to practice problems that use a variety of approaches and algorithms.

Bitwise Operators in C

Bitwise operators are the operators that manipulate the bits of a number. Bitwise operators involve operations on integers at the binary level and can be used to set the bits and shift or remove the bits.

What is a Bitwise Operator?

The operators that operate on data at the bit level are known as bitwise operators. It is known as bit-level programming when we perform bitwise operations. It consists of two numbers, either 0 or 1. To speed up calculations, it is mostly used in numerical computations.

Bitwise operators are used in Communication stacks where the individual bits in the header attached to the data signify important information. Apart from that they are used in low-level programming for applications such as device drivers, cryptographic software, video decoding software, memory allocators,  and graphics.

Types of Bitwise Operators in C

Types of Bitwise Operators

BITWISE OPERATORS
 

In C and C++, we have the following six Bitwise Operators.

  1. &: bitwise AND
     
  2. |: bitwise OR
     
  3. ^: bitwise XOR
     
  4. ~: bitwise NOT
     
  5. <<: left shift
     
  6. >>: right shift
     

Let us learn about each one of them in detail.

Bitwise AND Operator

It takes two numbers as input operands and does Bitwise AND on every corresponding bit of two numbers. If both operands are 1, the bitwise AND operator returns 1. Otherwise, it produces a value of 0.

Bitwise AND

Let’s take an example:

Example of Bitwise AND Operator

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
   int a=9;
   int b=2;
  
   //And
   int c=a&b;
  
   //Output
   cout<<c<<endl;
   return 0;
}


Output

0

 

Explanation

The binary representation of 9 is 1001.

The binary representation of 2 is 0010.

So now, we do bitwise AND of corresponding bits.

Bitwise AND of 9 and 2

Bitwise OR Operator

It takes two numbers as input operands and does Bitwise OR on every corresponding bit of two numbers. If at least one bit is 1, the bitwise OR operator returns 1. Otherwise, it produces a value of 0.

Bitwise OR

Let’s take an example:

Example of Bitwise OR Operator

  • C++

C++

#include <iostream>
using namespace std;
int main()
{
   int a=9;
   int b=2;
  
   //OR
   int c=a|b;
  
   //Output
   cout<<c<<endl;
   return 0;
}


Output

11

 

Explanation

The binary representation of 9 is 1001.

The binary representation of 2 is 0010.

So now, we do bitwise OR of corresponding .bits

 

Bitwise OR of 9 and 2

1011 is the binary representation of 11. So the output is 11.

Bitwise NOT Operator

It takes one number as an input operand and inverts all the number bits. It means 0 will replace with 1 and vice versa.

Bitwise NOT

Let’s take an example:

Example of Bitwise NOT Operator

  • C++

C++

#include <iostream>
using namespace std;
int main()
{
   int a = 10;
  
   //Not
   int c = ~ a;
  
   //Output
   cout<<c<<endl;
   return 0;
}


Output

-11

 

Explanation

The binary representation of 10 is 1010.

The 32-bit binary representation of 10 is 00000000000000000000000000001010.

So now, we do bitwise NOT of corresponding bits.

Bitwise NOT of 10

11111111111111111111111111110101 is the 32-bit binary representation of -11. So the output is -11.

Bitwise XOR Operator

Also called Exclusive OR, it takes two numbers as input operands and does Bitwise XOR on every corresponding bit of two numbers. If both bits are different, the bitwise OR operator returns 1. Otherwise, it produces a value of 0.

Bitwise XOR

Let’s take an example:

Example of Bitwise XOR Operator

  • C++

C++

#include <iostream>
using namespace std;

int main()
{
   int a=9;
   int b=3;
  
   //Xor
   int c = a ^ b;
  
   //Output
   cout<<c<<endl;
   return 0;
}

 

Output

10

 

Explanation

The binary representation of 9 is 1001.

The binary representation of 3 is 0011.

So now, we do bitwise OR of corresponding bits.

Bitwise XOR of 9 and 3

1010 is the binary representation of 10. So the output is 10.

Left shift Operator

It takes two numbers as input operands. The value of the left operand is shifted to the left by the number of bits given by the right operand and returned.

If numbers are positive, the left-shift operators are identical to multiplication by two.

Let us take an example:

Example of Left shift Operator

  • C++

C++

#include <iostream>
using namespace std;
int main()
{
   int a = 9;
  
   //Left Shift
   int c = a << 2;
  
   //Output
   cout<<c<<endl;
   return 0;
}

 

Output

36

 

Explanation

The binary representation of 9 is 1001.

9 << 2 means we need to shift 9 to the left by two bits.

So now, we do left shift of the corresponding bits.

Left Shift

100100 is the binary representation of 36. So the output is 36.

Right shift Operator

It takes two numbers as input operands. The value of the left operand is shifted to the right by the number of bits given by the right operand. ​​The least significant bits are lost when we shift a number to the right, while the most significant bits are replaced with zeroes.

Only if numbers are positive the right-shift operators are identical to the division by two.

Let’s take an example:

Example of Right shift Operator

  • C++

C++

#include <iostream>
using namespace std;
int main()
{
   int a = 9;
  
   //Left Shift
   int c = a >> 2;
  
   //Output
   cout<<c<<endl;
   return 0;
}

 

Output

2


You can also compile this code with the help of the Online C++ Compiler.

Explanation

The binary representation of 9 is 1001.

9 >> 2 means we need to shift 9 to the right by two bits.

So now, we do a right shift of the corresponding bits.

Right Shift

Here we will discard the rightmost 2 bits ( 0 1 ) because, after the right shift, the least significant bits are lost.

0010 is the binary representation of 2. So the output is 2.

Bitwise Operators in C vs Logical Operators in C

Bitwise OperatorsLogical Operators
Bitwise operators operate on the integer operands.Logical Operators operate on the expressions that result in a boolean value
These are used for the bit manipulation purposes such as checking a bit, setting a bit, and clearing a bit.These are used for checking if the expression is following specific criteria.
Bitwise Operators evaluate the whole expression. Logical Operators evaluate the expression as soon as the result is determined. That's why they are useful for short-circuiting.
The result of Bitwise Operators is an integer value.The result of Logical Operators is a boolean value which can be either true or false.
Major Bitwise Operators in C include Bitwise AND (&), Bitwise OR (|), and XOR (^). Major Logical Operators in C include Logical AND (&&), Logical OR (||) and Logical NOT (!).

Frequently Asked Questions

What is the Bitwise operator in C?

Bitwise Operators in C perform bit-level operations such as checking a bit, setting a bit, and clearing a bit. Major Bitwise Operators in C are Bitwise AND (&), Bitwise OR (|), and Bitwise XOR (^).

What is binary operator in C?

In C programming, binary operators are pivotal for various operations. Consider arithmetic operators, where '+' performs addition, '-' handles subtraction, '*' handles multiplication, '/' manages division, and '%' calculates the modulus (remainder). For relational operations, '==' checks for equality, '!=' verifies non-equality, '>' evaluates greater than, '<' assesses less than, '>=' checks greater than or equal to, and '<=' examines less than or equal to. Logical operations involve '&&' for logical AND and '||' for logical OR. These operators are fundamental for manipulating data, executing comparisons, and making decisions within C programs.

What is the bitwise operation in C?

Bitwise operators are characters that represent operations that should be carried out on single bits. They function at the binary level and carry out operations on bit patterns that involve bit manipulation.

Conclusion

In this article, we learned that Bitwise operations are easy to understand. Because they are quicker and use less memory, they are frequently preferred over standard arithmetic processes.
We also learned that bitwise operators should be used carefully when dealing with negative numbers and large numbers since incorrect usage could lead to overflowing numbers and wrong answers.
Refer to our Guided Paths on Coding Ninjas Studio to learn more about DSA, JavaScript, System Design, DBMS, Java, etc.
Happy Coding!

Previous article
Minimum Count of Inversion Pairs Possible by Concatenating N Binary Strings in any Order
Next article
Bit Manipulation for Competitive Programming