Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Bitwise Operator?
3.
Types of Bitwise Operators in 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 vs Logical Operators in C
11.
Interesting Facts About Bitwise Operators in C/C++
12.
Frequently Asked Questions
12.1.
What is binary operator in C?
12.2.
What is the bitwise operation in C?
12.3.
What are the Bitwise Operators in the C Truth Table?
13.
Conclusion
Last Updated: Jul 5, 2024
Easy

Bitwise Operators in C/C++

Author Rhythm Jain
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 (!).

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 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.

What are the Bitwise Operators in the C Truth Table?

The bitwise operators in C include AND (&), OR (|), XOR (^), and NOT (~). They operate on binary representations of integers, performing bit-by-bit logical operations according to the truth table for each operator.

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
Live masterclass