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.
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?
def myFunction() {}create function myFunction()function myFunction() {}
Choose another skill to practice
Types of Bitwise Operators in C/C++
BITWISE OPERATORS
In C and C++, we have the following six Bitwise Operators.
&: bitwise AND
|: bitwise OR
^: bitwise XOR
~: bitwise NOT
<<: left shift
>>: 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.
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
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.
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
The 32-bit binary representation of 10 is 00000000000000000000000000001010.
So now, we do bitwise NOT of corresponding bits.
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.
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
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
9 << 2 means we need to shift 9 to the left by two bits.
So now, we do left shift of the corresponding bits.
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
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.
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 Operators
Logical 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.