  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Bitwise Operators in C

Last Updated on June 6, 2023 by Mayank Dham C bitwise operators are essential tools for manipulating data at the bit level in the C programming language. These operators allow programmers to perform operations on individual bits, enabling efficient numerical computations and data manipulation. Bitwise operators work on binary numerals, treating each bit as a separate entity, and they are particularly useful for tasks that involve low-level programming and optimization. In C, the bitwise operators include AND (&), OR (|), XOR (^), complement (~), left shift (<<), and right shift (>>). Understanding bitwise operators and their applications is crucial for harnessing the full power of C and optimizing code efficiency.

## What are Bitwise Operators in C?

Bitwise Operators in C are used for data manipulation at the bit level, it is also called bit-level programming. Bitwise operates on one or more binary numerals at the level of their bits. They are used in numerical computations to make the calculation process faster because it mainly consists of two digits – 1 or 0.
In C language, the bitwise operators (work at bit-level) are:-

• The & (bitwise AND) in C takes two numbers as operands and performs logical AND on every bit of two numbers. The result of logical AND is 1 only if both bits are 1.
• The | (bitwise OR) in C takes two numbers as operands and performs logical OR on every bit of two numbers. The result of logical OR is 1 if any of the two bits is 1.
• The ^ (bitwise XOR) in C takes two numbers as operands and performs XOR on every bit of two numbers. The result of XOR is 1 if the two bits are distinct.
• The <> (right shift) in C takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of positions to shift.
• The ~(bitwise NOT) in C takes one number and inverts all its bits.

## Types of Bitwise Operators in C

Now that we know what are the bitwise operators in c, let’s look at each one of them in detail.

### AND (&) Operator in C

The bitwise AND operator is represented using a single ampersand symbol, i.e. &. This is a binary operator, it takes two operands(two integers) to work on. It represents both the left and right operands into their binary expressions and performs the logical AND operation over them on the bit level, i.e. if both the operands have set bit on the specified position then the result will also have set bit in the corresponding position, or else 0. ```#include <stdio.h>

int main(void) {
// Bitwise AND implementation
int result,x=2,y=6;
result = x & y;
printf("2 & 6 = %d" , result);
return 0;
}
``` Working of AND Operator:

• The above code snippet performs the bitwise AND operation on 2 and 6. Let’s see their work in detail.
• The binary value for 2 is 010 and for 6 is 110.
• Now move from left to right, perform logical AND operations on the bits and store the result in the corresponding position.
• The first bit of 2 is 0 and the first bit of 6 is 1, the logical AND will consider 0 as False and 1 as True, so the result will be false and 0 will be the first bit of the result.
• The same process repeats itself throughout the length of the binary values. The second bit of 2 and 6 are 1 and 1 respectively, so 1 will be stored as the second bit of the result.
• The third and last bit of both 2 and 6 are 0 and 0, so again 0 will be the third and final bit of our result.
• So the final binary value of our result will be 010, which when converted to integer decimal results in 2.

### OR ( | ) Operator

The bitwise OR is similar to the bitwise AND, the only difference is that the bitwise OR performs logical OR instead of logical AND on the bit level, i.e. if at least any one of the operands has a set bit, then the result will also have set bit in the corresponding position, or 0 if they both have 0 in the corresponding position.Bitwise OR is denoted using the symbol, i.e. |. ```#include <stdio.h>

int main(void) {
// Bitwise OR Implementation
int result, x = 2, y = 6;
result = x | y;
printf("2 | 6 = %d", result);
return 0;
}
``` Working of OR Operator in C:

• The above code snippet performs the bitwise OR operation on 2 and 6. Let’s see their work in detail.
• The binary value for 2 is 010 and for 6 is 110.
• Now move from left to right, perform logical OR operations on the bits and store the result in the corresponding position.
• The first bit of 2 is 0 and the first bit of 6 is 1, the logical OR will consider 0 as False and 1 as True, so the result will be true and 1 will be the first bit of the result.
• The process repeats throughout the length of the binary values. The second bit of 2 and 6 are 1 and 1 respectively, so 1 will be stored as the second bit of the result.
• The third and last bit of both 2 and 6 are 0 and 0, so again 0 will be the third and final bit of our result.
• So the final binary value of our result will be 110, which when converted to integer decimal results in 6.

### XOR ( ^ ) Operator in C

This is similar to the other two, but the only difference is that they perform logical XOR on the bit level, i.e., if exactly one of the operands has a set bit and the other has 0 then the result will be a set bit in the corresponding position, and 0 if they have the same bits such as both 0s or both 1s. ```#include <stdio.h>

int main(void) {
// code goes here
int ans, num1 = 2, num2 = 6;
ans = num1 ^ num2;
printf("2 ^ 6 = %d", ans);
return 0;
}
```

Working of XOR Operator in C:

• The above code performs the bitwise XOR operation on 2 and 6. Let’s see this in detail.
• The binary value for 2 is 010 and for 6 is 110.
• Now move from left to right, perform logical XOR operations on the bits and store the result in the corresponding position.
• The first bit of 2 is 0 and the first bit of 6 is 1, the logical XOR will consider 0 as False and 1 as True, so the result will be true and 1 will be the first bit of the result.
• The same process goes on throughout the length of the binary values. The second bit of 2 and 6 are 1 and 1 respectively, so 0 will be stored as the second bit of the result.
• The third and last bit of both 2 and 6 are 0 and 0, so again 0 will be the third and final bit of our result.
• So the final binary value of our result will be 100, which when converted to integer decimal results in 4.

### Complement ( ~ ) Operator in C

If you have noticed, all of the three bitwise operators we have seen till now were binary operators, i.e. all three of them require two operands to execute their functions. But complement operator is different, this is the only bitwise operator that needs only one operand. All other bitwise operators require two operators.
The complement operator takes a single value and give its one’s complement. The one’s complement of a number is obtained by inverting all the bits.
It is represented by tilde symbol, i.e. ‘~’. ```#include <stdio.h>

int main(void) {
// code goes here
int ans, num1 = 6;
ans = ~num1;
printf("~6 = %d", ans);
return 0;
}
``` Working of Complement Operator in C:

• Logically ~6 = 1 but,compiler returns the 2’s complement of the value.
• Negative numbers are stored as 2’s complement
• The binary equivalent of 6 is 0000 0110.
• One‘s complement of 6 is 1111 1001.
• 7 is represented in binary as 0000 0111.
• One‘s complement of 7 is 1111 1000.
• Add one to its one’s complement to get two’s complement of 7 = 1111 1001.
• This equates to ~6.
• 7 is the bitwise complement of 6.

### Left Shift ( << ) Operator in C

Syntax:

``x << n;``

The left shift operator shifts the bits of an integer by a specified number of places to the left.

The Left shift operator takes two operands,one on which the shift operation is to be performed, say ‘x’, and another that specifies the number of bit positions that have to be shifted, say ‘n’.

The value of ‘x’ can be negative, but ‘n’ can not be negative, if the value of ‘n’ is negative then the compiler will throw an error, saying ‘negative shift count’

When the value of 'x' is negative, the Left Shift operation is performed on the two’s complement of the number. So there is a probability that the sign of the number may or may not be the same. The Shift Left operator is denoted using two consecutive greater than operators, i.e. <> ) Operator in C**

Syntax:

``x >> n``

The right shift operator is very similar to the left shift operator, the only difference is that it shifts the bits to right instead of left. This removes the last n bits from the given value and converts the leftover bits from binary to an integer.

The same rule that we saw in the Left shift operator also applies for the Right shift operator. ‘x’ can be negative, but not ‘n’, if ‘n’ is negative then the compiler will throw an error, giving warning ‘negative shift count’.

Just as in the left shift operator, When performing right shift operation on a negative number, the right shift operation is performed on the two’s complement of the number. So when we do right shift operation on a negative number, output will be a positive number, cause when you perform right shift operation you replace the sign bit with 0, after shifting the previously present 1 to the next bit position.

It is represented by two consecutive less than symbols, i.e. >>.

```#include <stdio.h>

int main(void) {
int ans, num1 = 12;
ans = num1 >> 2;
printf("12 >> 2 = %d", ans);
return 0;
}
```

Working of Right Shift Operator in C:

• The above code snippet performs the right shift operation on the decimal value 12.
• It shifts the bit patterns of 12 by 2
• The binary value for 12 is 1100.
• When you shift it to the right by 2 positions and add 2 0s to the leftmost end of the binary sequence, i.e. the most significant bit, the result that you get is 011.
• The result when converted from binary to decimal will be 3.

Conclusion
In conclusion, C bitwise operators provide programmers with powerful tools to manipulate data at the bit level, enabling efficient computations and data manipulation. These operators work on binary numerals, performing logical operations on individual bits. The bitwise AND (&), OR (|), and XOR (^) operators allow for combining, comparing, and manipulating bits based on specific conditions. The complement (~) operator inverts all the bits of a given value, while the left shift (<<) and right shift (>>) operators shift the bits to the left or right by a specified number of positions. By leveraging these operators effectively, programmers can optimize code performance and implement low-level operations efficiently. Understanding and utilizing C bitwise operators are vital skills for mastering the intricacies of the language and achieving efficient and optimized programming.