Bitwise Operators in C
What is Bitwise Operators in C Programming?
Operators which are used to perform bit-level operations or bitwise manipulations in C programming are called bitwise operators.
Types of Bitwise Operators
There are six types of bitwise operators:- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR or Bitwise exclusive OR (^)
- Bitwise complement or Bitwise NOT (~)
- Bitwise Left Shift (<<)
- Bitwise Right Shift (>>)
Bitwise Operators
OPERATOR | DESCRIPTION |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR or Bitwise exclusive OR |
~ |
Bitwise complement or Bitwise NOT |
<< | Bitwise Left Shift |
>> | Bitwise Right Shift |
Let's discuss bitwise operators :
Bitwise AND(&) operator
Bitwise AND operator is denoted by the single ampersand sign (&). It takes two bits at a time and performs AND operation.It is a binary operator takes two operands.The result of bitwise AND is 1 if both bits are 1; otherwise, the output would be 0.
Let us suppose the bitwise AND operation of two integers num1=6 and num2=4.
The binary representation of above two variables is:
num1=00000110
num2=00000100
00000110
& 00000100
00000100 = 4 (In decimal)
Code for Bitwise AND(&)
#include <stdio.h>void main()
{
int num1 = 6, num2 = 4;
printf("Bitwise AND result is %d", num1&num2);
}
Output:
Bitwise AND result is 4
Bitwise OR(|) operator
Bitwise OR operator is denoted by the single verticle sign (|). It takes two bits at a time and performs OR operation.It is a binary operator takes two operands.The result of bitwise OR is 0 if both bits are 0; otherwise, the output would be 1.
Let us suppose the bitwise OR operation of two integers num1=6 and num2=4.
The binary representation of above two variables is:
num1=00000110
num2=00000100
00000110
| 00000100
00000110 = 6 (In decimal)
Code for Bitwise OR(|)
#include <stdio.h>
void main()
{
int num1 = 6, num2 = 4;
printf("Bitwise OR result is %d", num1|num2);
}
Output:
Bitwise OR result is 6
Bitwise XOR(^) or exclusive OR operator
Bitwise OR operator is denoted by (^) symbol. It is a binary operator takes two operands.The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite.
Let us suppose the bitwise XOR operation of two integers num1=6 and num2=4.
The binary representation of above two variables is:
num1=00000110
num2=00000100
00000110
^ 00000100
00000010 = 2 (In decimal)
Code for Bitwise XOR(|)
#include <stdio.h>void main()
{
int num1 = 6, num2 = 4;
printf("Bitwise XOR result is %d", num1^num2);
}
Output:
Bitwise XOR result is 2
Bitwise complement or Bitwise NOT(~)
Bitwise complement operator is an unary operator (works on only one operand).It is also known as one's complement operator. It changes 1 to 0 and 0 to 1. It is denoted by tilde(~) symbol.
Let us suppose the bitwise complement operation of one integer num1=6 .
The binary representation of above two variables is:
num1=00000110
Bit Operation of 6
~00000110
11111001 = 249 (In decimal)
The bitwise complement of 6 (~6) is -7 instead of 249,why?
For any integer number n, bitwise complement of n will be -(n+1). To understand this, you should have the knowledge of 2's complement.
2's Complement
Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1. For example:Decimal | Binary | 2's Complement |
0 | 00000000 | -(11111111+1) = -00000000 = -0(decimal) |
1 | 00000001 | -(11111110+1) = -11111111 = -256(decimal) |
12 | 00001100 | -(11110011+1) = -11110100 = -244(decimal) |
220 | 11011100 | -(00100011+1) = -00100100 = -36(decimal) |
Code for Bitwise NOT(~)
#include <stdio.h>
void main()
{
int num1= 6;
printf("Bitwise NOT result is %d", ~num1);
}
Output:
Bitwise NOT result is -7
Bitwise Left Shift(<<) operator
The left shift (<<) operator takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
Let us suppose the bitwise left shift (<<) operation of num1=6.
The binary representation of num1 variable is:
num1=00000110
Bit Operation of 6
If we want to left-shift the above representation by 2, then the statement would be:
num1 << 2;
00000110<<2 = 00001100
Code for Bitwise Left Shift(<<)
#include <stdio.h>void main()
{
int num1 = 6;
printf("Bitwise left shift << result is %d", num1<<2);
}
Output:
Bitwise left shift << result is 24
right shifts the bits of the first operand, the second operand decides the number of places to shift.
Bitwise Right Shift(>>) operator
The right shift (>>) operator takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
Let us suppose the bitwise right shift (>>) operation of num1=6.
The binary representation of num1 variable is:
num1=00000110
Bit Operation of 6
If we want to right-shift the above representation by 2, then the statement would be:
num1 >> 2;
00000110>>2 = 00000001
Code for Bitwise Right Shift(>>)
#include <stdio.h>
void main()
{
int num1 = 6;
printf("Bitwise right shift >> result is %d", num1>>2);
}
Output:
Bitwise right shift >> result is 1