1.
Introduction
2.
Bitwise AND (&) operator
2.1.
Example
2.2.
C
2.3.
Output
3.
Bitwise OR (|) operator
3.1.
Example
3.2.
C
3.3.
Output
3.4.
Bitwise XOR (^) operator
3.5.
Example
3.6.
C
3.7.
Output
3.8.
Bitwise NOT (~) Operator
3.9.
Example
3.10.
C
3.11.
Output
3.12.
Bitwise Left Shift (<<) Operator
3.13.
Example
3.14.
C
3.15.
Output
3.16.
Bitwise Right Shift (<<) Operator
3.17.
Example
3.18.
C
3.19.
Output
4.
4.1.
Why don't we use Bitwise operators for floating point numbers?
4.2.
Why are Bitwise operators preferred over other operators?
4.3.
What languages other than C support Bitwise operators?
5.
Conclusion
Last Updated: Mar 31, 2024
Easy

# Bitwise Operators in C Programming

Aayush Sharma
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

Bitwise operators are the type of operators that perform operations on individual bits of data instead of the whole data. These operators are used to manipulate individual bits in a number.

Bitwise operators include AND(&), OR(|), XOR(^), NOT(~), Left Shift(<<) etc. These operators are important to understand when working with particular bits instead of the whole number. In the next section, we will see about these operators in detail.

## Bitwise AND (&) operator

The bitwise AND operator is a binary operator denoted by the ampersand symbol (&). The AND operator takes two operands as input and operates on the corresponding pair of bits in each number. The result of the AND operation on a pair of bits is '1' if both are set to one and '0' otherwise.

The truth table of the AND operator is shown below.

Now we will discuss an example to help us understand it more clearly.

Let us suppose that we need to find bitwise AND of two integers, say 13 and 7

``````13 = 00001101 (Binary representation)
7 = 00000111 (Binary representation)``````

The result of these operations should be '1' where both the corresponding bits are set to one and '0' otherwise. Therefore, the result will be 000001015 in decimal notation. The complete C implementation to perform bitwise AND operation is shown below.

• C

### C

``#include <stdio.h>int main() {	int a=13;	int b=7;	int c=a&b;	printf("Bitwise AND of 13 and 7 is %d \n",c);}``

### Output

``Bitwise AND of 13 and 7 is 5``
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

## Bitwise OR (|) operator

The bitwise OR operator is a binary operator denoted by the pipe symbol (|). The OR operator takes two operands as input and operates on the corresponding pair of bits in each number. The result of the OR operation on a pair of bits is '1' if at least one of the bits is set to one and '0' if both bits are set to zero.

The truth table of the OR operator is shown below.

We will retake the same example and find the bitwise OR of 13 and 7.

``````13 = 00001101 (Binary representation)
7 = 00000111 (Binary representation)``````

The result of these operations should be '1' where at least one of the bits is set to one, and '0' if both the bits are set to zero. Therefore the result will be 00001111, which is 15 in decimal notation.

The complete C implementation to perform bitwise OR operation is shown below.

• C

### C

``#include <stdio.h>int main() {	int a=13;	int b=7;	int c=a|b;	printf("Bitwise OR of 13 and 7 is %d \n",c);}``

### Output

``Bitwise OR of 13 and 7 is 15``

### Bitwise XOR (^) operator

Like bitwise AND and bitwise OR operators, the XOR operator is also a binary operator denoted by the caret symbol (^). The XOR operator takes two operands as input and operates on the corresponding pair of bits in each number. The result of the XOR operation on a pair of bits is '1' if the number of set bits is odd and '0' if the number of set bits is even.

The truth table of the XOR operator is shown below.

We will retake the same example and find the bitwise OR of 13 and 7.

``````13 = 00001101 (Binary representation)
7 = 00000111 (Binary representation)``````

The result of these operations should be '1', where the number of set bits is odd, and '0' when the number of set bits is even. Therefore the result will be 00001010, which is 10 in decimal notation. The complete C implementation to perform bitwise XOR operation is shown below.

• C

### C

``#include <stdio.h>int main() {	int a=13;	int b=7;	int c=a^b;	printf("Bitwise XOR of 13 and 7 is %d \n",c);}``

### Output

``Bitwise XOR of 13 and 7 is 10``

### Bitwise NOT (~) Operator

Unlike the AND, OR, and XOR operators discussed above, the NOT Operator is unary. This means that the NOT Operator only takes a single operand. The NOT Operator is denoted by the tilde symbol (~). The NOT Operator is used to flip the bits of a number in its binary representation, i.e., it changes the '0' bit in binary to '1' and vice-versa. The truth table of the NOT Operator is shown below.

Let us take an example of the number 137 and find its bitwise NOT. The number 137 is represented as 10001001 in binary. The bitwise NOT operation will flip the bits in it and change the '1' bit to '0' and vice-versa. Therefore the result should be 01110110, which is 118 in decimal. The complete C implementation of the bitwise NOT operation is shown below.

• C

### C

``#include <stdio.h>int main() {	unsigned int a=137;	unsigned int c= ~a;	printf("Bitwise NOT of 137 is %u \n",c);}``

### Output

``Bitwise NOT of 137 is 4294967158``

There are a few things that we should understand here. We have used unsigned integers here because if we apply it to signed integers, the outermost bit gets flipped and is counted as a negative number. In our example of 137, the output will come out to be -138, which is not correct. Hence we need to use unsigned integers to calculate the bitwise NOT.

Another thing that we should note here is that our output comes out to be a very large number instead of 118, which we calculated manually. This is happening because of the number of bits in an int variable. When we manually calculated, we only considered 8 bits. But as we know that it is a 32-bit data type. The compiler will consider all of these bits and flip all the bits, which will cause our output to be a very large number.

Also read,  Short int in C Programming And Floyd's Triangle in C

### Bitwise Left Shift (<<) Operator

The bitwise left shift operator is a binary operator that shifts the bit of the operand by a specific number of positions to the left. The bitwise left shift operator consists of two operands: the number to be left-shifted and the number of shifts.

Let us take an example to understand the left shift in a better way. Suppose we need to shift 29 by three positions to the left. 29 in binary is written as 00011101. Left shifting 29 by three places will result in 11101000, 232 in decimal. The implementation of using the left shift operator in C is shown below.

• C

### C

``#include <stdio.h>int main() {	int a=29;	int b=3;	int c= 29 << 3;	printf("The result after left shifting 29 by 3 positions is %u \n",c);}``

### Output

``The result after left shifting 29 by 3 positions is 232``

### Bitwise Right Shift (<<) Operator

Similar to the bitwise left shift operator, the right shift is also a binary operator that shifts the bit of the operand by a specific number of positions but to the right. Like the bitwise left shift operator, the right shift also consists of two operands: the number to be left-shifted and the number of shifts.

We will take the same example to understand the right shift. We need to shift 29 by three positions to the right. 29 in binary is written as 00011101. Right shifting 29 by three places will result in 00000011, which is in decimal. The implementation of using the left shift operator in C is shown below.

• C

### C

``#include <stdio.h>int main() {	int a=29;	int b=3;	int c= 29 >> 3;	printf("The result after right shifting 29 by 3 positions is %u \n",c);}``

### Output

``The result after right shifting 29 by 3 positions is 3``

Must Read Passing Arrays to Function in C

### Why don't we use Bitwise operators for floating point numbers?

Bitwise operators work by manipulating the individual bits of an integer. Floating point numbers are not represented in binary like integers. Using bitwise operators with floating point numbers will result in unexpected behavior. Hence we should not use bitwise operators on floating point numbers.

### Why are Bitwise operators preferred over other operators?

In some situations, bitwise operators are preferred over others as they operate on individual bits rather than the whole number, resulting in efficient code. Moreover, bitwise operators reduce memory usage and improve the program's performance.

### What languages other than C support Bitwise operators?

Many other programming languages, like C++, Java, Swift, Python, Javascript, Ruby, PHP, etc., support bitwise operators. Programming languages like Assembly also provide extra operators like rotate, which allows bits to be shifted circularly rather than just in one direction.

## Conclusion

In this article, we discussed various bitwise operators in C like AND, OR, XOR, NOT, Left shift and Right shift. We discussed their working and use cases with their C implementations. Now that you have learnt about bitwise operators in C, you can also refer to other similar articles.

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSA, Competitive Programming, System Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!

Live masterclass