Bitwise Operators in C allow direct manipulation of individual bits in the integer variables. These operators are fast and efficient, but also prone to errors. These operators are used in various fields, such as low-level programming and embedded systems. In this article, we will discuss what a bitwise operator in C is, its types, applications, advantages, and disadvantages in C.
Table of Contents:
What are Bitwise Operators in C?
Bitwise operators in C are the operators that are used to perform bit-level operations on the integers, allowing the direct manipulation of individual bits in a variable. These operators perform the operations by considering the integers as a sequence of binary digits. They are efficient for low-level programming, memory optimization, and hardware interactions. Since these operators work only on a bit at a time, it is different from the arithmetic and logical operators, which work only with entire values.
Types of Bitwise Operator in C
There are six types of bitwise operators in C:
Operator Name |
Symbol |
Description |
Bitwise AND |
& |
Sets a bit to 1 if both corresponding bits are 1 |
Bitwise OR |
| |
Sets a bit to 1 if at least one corresponding bit is 1 |
Bitwise XOR |
^ |
Sets a bit to 1 if the corresponding bits are different |
Bitwise NOT |
~ |
Inverts all bits (1 becomes 0, and 0 becomes 1) |
Left Shift |
<< |
Shifts bits to the left, filling with 0s |
Right Shift |
>> |
Shifts bits to the right, discarding bits |
Let’s discuss each of the types in brief with the example in C.
1. Bitwise AND (&) Operator
The bitwise AND (&) operator in C compares corresponding bits between two integers. It sets a bit to 1 as a result if both corresponding bits are 1, otherwise, it sets the bit to 0.
Syntax:
result = a & b;
Bitwise AND Truth Table:
A |
B |
A & B |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
Example:
Output:
The code shows how the bitwise AND operator performs a bitwise AND operation between the integer 5(00000101) and 3(00000001), which results in 1(00000001) and then printed to the console.
2. Bitwise OR (|) Operator
The bitwise OR (|) operator in C performs a bitwise comparison between two integers and sets each bit to 1 if at least one of the corresponding bits is 1, otherwise, it sets the bit to 0.
Syntax:
result = a | b;
Bitwise OR Truth Table:
A |
B |
A | B |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
Example:
Output:
The code shows how the bitwise OR operator performs a bitwise OR operation on the integers 5(00000101) and 3(00000001), which gives the result 7(00000111) and is then printed to the console.
3. Bitwise XOR (^) Operator
The bitwise XOR (^) operator in C compares corresponding bits of two integers and sets each bit to 1 if the corresponding bits are different, otherwise, it sets the bit to 0.
Syntax:
result = a ^ b;
Bitwise XOR Truth Table:
A |
B |
A ^ B |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
Example:
Output:
The code shows how the bitwise XOR operator performs a bitwise XOR operation between the integer 5(00000101) and 3(00000001), which results in 6(00000110) and then printed to the console.
4. Bitwise NOT (~) Operator
The bitwise NOT (~) operator in C inverts all the bits of the integer, which means that it changes 1s to 0s and 0s to 1s. This operation is also known as the one’s complement.
Syntax:
result = ~a;
Example:
Output:
The code shows how the bitwise NOT operator performs a bitwise NOT operation on the integer 5(00000101), which results in -6(11111010) and then printed to the console.
5. Left Shift (<<) Operator
The left shift (<<) operator in C is used to shift the bits of a number to the left by a particular number of positions and fill the vacant rightmost bits with 0s. It multiplies the number by 2^n, where n is the shift count.
Syntax:
result = a << n;
Example:
Output:
The code shows that the left shift operator shifts the bits of 5(00000101) to the left by 2 positions, which results in 20(00010100) by multiplying 5 by 2^2 and then printing to the console.
6. Right Shift (>>) Operator
The right shift (>>) operator in C shifts the bits of an integer to the right by a particular number of positions and discards the rightmost bits. Also, the behavior of the leftmost bits depends on whether the integer is signed or unsigned.
- For signed numbers, the behavior of the leftmost bits depends on the system.
- For unsigned numbers, the leftmost bits are filled with 0.
Syntax:
result = a >> n;
Example:
Output:
The code shows that the right shift operator shifts the bits of 20(00010100) to the right by 2 positions, which results in 5(00000101) by multiplying 20 by 2^2 and then printing to the console.
Precedence and Associativity of Bitwise Operators in C
Bitwise operators have lower precedence than arithmetic operators but have higher precedence than logical operators.
Symbol |
Precedence Level |
Associativity |
~ |
Highest |
Right to Left |
<< |
High |
Left to Right |
>> |
High |
Left to Right |
& |
Medium |
Left to Right |
^ |
Low |
Left to Right |
| |
Lowest |
Left to Right |
Advantages of Bitwise Operators in C
- Bitwise operators give higher performance than arithmetic and logical operators.
- These operators are ideal for individual bit manipulation.
- They provide efficient memory usage by storing multiple Boolean flags in a single variable.
- Bitwise operators make it easier to access and modify particular bits in registers and ports.
- Shift operators help to perform fast and efficient multiplication and division.
- Bitwise operators are useful in encryption and compression.
Disadvantages of Bitwise Operators in C
- Bitwise operators are error-prone, as small mistakes in bit manipulation can lead to bugs that are difficult to find.
- Using these operators reduces readability as they are hard to read and understand.
- Bitwise operators are used only with the integer types and cannot be used with the floating-point type.
- The behavior of these operators may vary depending on data type sizes or signed shifts across systems.
- Debugging the bitwise operations is very difficult as they are harder to find.
Applications of Bitwise Operators in C
- Bitwise operators can be used for setting, clearing, and toggling the individual bits by using bitmasks.
- The shift operators are used for fast multiplication and division.
- These operators efficiently manipulate flags and status registers in a single variable.
- Bitwise XOR is used for simple encryption and checksum operations.
- They optimize performance in low-level and system-level programming.
- Bitwise operators are also used in embedded systems.
Conclusion
Bitwise operators in C can be used for low-level programming and direct manipulation of bits. These operators provide performance and memory optimization. Also, these are fast and efficient, yet they are error-prone, can only be used with integers, and reduce readability. So, by understanding how each bitwise operator works, their applications, advantages, and disadvantages, you can easily perform any bitwise operation without any error in C programming.
Check out other related Software-Engineering blogs:
FAQs on Bitwise Operators in C
Q1. Can bitwise operators be used with float and double?
No, bitwise operators only work with integer types.
Q2. What’s the difference between & and &&?
& is bitwise AND, and && is logical AND.
Q3. What does a << n do?
It shifts bits to the left by n positions by multiplying by 2^n.
Q4. Is a >> n the same as a / 2^n?
Yes, for unsigned integers, it is the same
Q5. Are bitwise operators faster than arithmetic operators?
Yes, they are generally more efficient.