Table of contents
1.
Introduction
2.
What is a Bitwise Operator?
3.
Types of Bitwise Operators in C/C++
4.
Bitwise AND Operator
4.1.
Example of Bitwise AND Operator
4.2.
C++
5.
Bitwise OR Operator
5.1.
Example of Bitwise OR Operator
5.2.
C++
6.
Bitwise NOT Operator
6.1.
Example of Bitwise NOT Operator
6.2.
C++
7.
Bitwise XOR Operator
7.1.
Example of Bitwise XOR Operator
7.2.
C++
8.
Left shift Operator
8.1.
Example of Left shift Operator
8.2.
C++
9.
Right shift Operator
9.1.
Example of Right shift Operator
9.2.
C++
10.
Bitwise Operators in C/C++ vs Logical Operators in C/C++
11.
Interesting Facts About Bitwise Operators in C/C++
12.
Frequently Asked Questions
12.1.
What is the NOT bit operator in C++?
12.2.
How to write XOR in C++?
12.3.
What is bitwise logical operations?
13.
Conclusion
Last Updated: Sep 5, 2024
Easy

Bitwise Operators in C/C++

Author Rhythm Jain
0 upvote

Introduction

Bitwise operators in C/C++ are tools that allow manipulation of individual bits within data. These operators perform operations at the binary level, enabling efficient processing of data, especially in tasks like setting, clearing, or toggling specific bits. Common bitwise operators include AND, OR, XOR, NOT, and bit shifts, each serving distinct purposes in low-level programming, optimizing performance, and controlling hardware.

Bitwise Operators in C

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.

Here's your problem of the day

Solving this problem will increase your chance to get selected in this company

Skill covered: Programming

How do you create a function in JavaScript?

Choose another skill to practice

Types of Bitwise Operators in C/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;
}
You can also try this code with Online C++ Compiler
Run Code


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;
}
You can also try this code with Online C++ Compiler
Run Code


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;
}
You can also try this code with Online C++ Compiler
Run Code


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;
}
You can also try this code with Online C++ Compiler
Run Code

 

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;
}
You can also try this code with Online C++ Compiler
Run Code

 

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;
}
You can also try this code with Online C++ Compiler
Run Code

 

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/C++ vs Logical Operators in C/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 (!).

Interesting Facts About Bitwise Operators in C/C++

  • Low-Level Manipulation: Bitwise operators allow direct manipulation of individual bits in an integer, providing fine-grained control over data.
  • Common Operators: The main bitwise operators are AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>).
  • Performance: Bitwise operations are generally faster than arithmetic operations because they are directly supported by the processor's instruction set.
  • Bit Masks: Bitwise operators are often used to create and manipulate bit masks, which are used to extract or set specific bits in a byte or word.
  • Boolean Algebra: Bitwise operators follow the principles of Boolean algebra, making them useful for logical operations at the bit level.
  • Swapping Values: You can swap two integers without using a temporary variable by using the XOR operator.
  • Power of Two: Checking if a number is a power of two can be efficiently done using bitwise AND.
  • Bitwise NOT: The bitwise NOT operator (~) inverts all the bits in a number, effectively performing a one's complement.
  • Clearing Bits: Specific bits can be cleared (set to 0) using the AND operator with a bit mask.
  • Setting Bits: Specific bits can be set (set to 1) using the OR operator with a bit mask.
  • Toggling Bits: Specific bits can be toggled (flipped) using the XOR operator with a bit mask.
  • Circular Shifts: Bitwise shift operators can be used to perform circular shifts (rotations) by combining left and right shifts with bitwise OR.
  • Checking Parity: You can check the parity (odd or even number of 1-bits) of a number using XOR.
  • Compression: Bitwise operators are used in data compression algorithms to efficiently pack and unpack data into smaller bit-sized units.

Frequently Asked Questions

What is the NOT bit operator in C++?

The NOT bitwise operator in C++ is ~. It inverts all bits in a number, converting 0s to 1s and vice versa.

How to write XOR in C++?

In C++, the XOR bitwise operator is ^. It performs an exclusive OR operation between corresponding bits of two numbers.

What is bitwise logical operations?

Bitwise logical operations manipulate individual bits of binary numbers using operators like AND, OR, XOR, and NOT to perform low-level data processing.

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 Code360 to learn more about DSA, JavaScript, System Design, DBMS, Java, etc.

Live masterclass