bitwise_operators_in_c_gate_notes_72
bitwise_operators_in_c_gate_notes_72
To perform operations on available data at the bit level, we use the bitwise
operators in C. As a result, carrying out a bitwise operation is also referred to as
bit-level programming. Because it consists of only two digits - 1 or 0 - it is
primarily used in numerical computations for faster calculation.
The bitwise operators in C work with integers, that is, they take integer inputs,
manipulate with their bits, and return an integer value. The bitwise AND and
OR operators are '&' and '|', while the logical AND and OR operators are '&&'
and '||'.
Some of these operator's symbols and names may appear similar to logical
operators, but they are different from them. There are various types of bitwise
operators in all programming languages. There are basically six different types
of bitwise operators in C that are as follows:
• Bitwise OR Operator
• Bitwise AND Operator
• Unary Operator (Binary One’s complement operator)
• Bitwise XOR Operator
• Binary Right Shift Operator
• Binary Left Shift Operator
Bitwise OR Operator
The bitwise OR operator is represented by the vertical bar or pipe symbol, |. The
bitwise OR operator is a binary operator which requires two operands (two
integers) to operate on. It takes the binary values of both the left and right
operands and performs the logical OR operation on the bit level over them, i.e.
if both operands have a 0 in the specified position, the result will also have a 0
in the corresponding position, otherwise it will be 1.
0 0 0
1 0 1
0 1 1
1 1 1
The bitwise AND operator is represented by a single ampersand symbol, &. The
bitwise AND operator is a binary operator which requires two operands (two
integers) to operate on. It takes the binary values of both the left and right
operands and performs the logical AND operation on the bit level over them,
i.e. if both operands have a 1 in the specified position, the result will also have a
1 in the corresponding position, otherwise it will be 0.
0 0 0
1 0 0
0 1 0
1 1 1
We have above seen two bitwise operators which require two operands. But this
is different as it requires only one operand, whereas all the others require two
operands. The one's complement of a single value is returned by the bitwise
complement operator. A number's one's complement is obtained by changing all
of its 0's to 1's and all of its 1's to 0's.
This is denoted by the tilde symbol ‘~’. The truth table of the unary operator is
as follows:
0 1
1 0
0 0 0
1 0 1
0 1 1
1 1 0
Binary Shift right is represented by two consecutive greater than operators, i.e.
>>. This is equivalent to dividing the number by 2 power n, where n is the
operand to the operator's right.
The value of 'x' can be negative, but not that of 'n'; if 'n' is negative, the compiler
will throw an error stating 'negative shift count.' When the value of 'x' is
negative, the right Shift operation is applied to the number's two's complements.
As a result, the sign of the number may or may not be the same as the right shift
operation.
Binary Shift Left is represented by two consecutive greater than operators, i.e.
<<. This is equivalent to multiplying the number by 2 power n, where n is the
operand to the operator's right.
The value of 'x' can be negative, but not that of 'n'; if 'n' is negative, the compiler
will throw an error stating 'negative shift count.' When the value of 'x' is
negative, the Left Shift operation is applied to the number's two's complements.
As a result, the sign of the number may or may not be the same as the left shift
operation.
#include <stdio.h>
int main()
return 0;
Output:
Explanation:
The Left Shift Operator (<<) is a bitwise operator that operates on bits. It shifts
a given number of bytes to the left and inserts 0's to the right.
0000 0000 1111 1111 is the binary of 0xFF (in 4 bytes format).
After a two-byte left shift (in four-byte format), the result is 0000 0011 1111
1100, which is equivalent to 0x03FC.
#include <stdio.h>
int main()
{
return 0;
Output:
Explanation:
The Right Shift Operator (>>) is a bitwise operator that operates on bits. It shifts
a given number of bytes to the right and inserts 0's to the left.
0000 0000 1111 1111 is the binary of 0xFF (in 4 bytes format).
After a two-byte left shift (in four-byte format), the result is 0000 0000 0011
1111, which is equivalent to 0x003F.
#include <stdio.h>
int main()
int num2 = 15
Output:
Explanation:
In binary 20 = 00010100
In binary 15 = 00001111
00010100
& 00001111
#include <stdio.h>
int main()
int num2 = 15
return 0;
Output:
Explanation:
In binary 20 = 00010100
In binary 15 = 00001111
00010100
| 00001111
#include <stdio.h>
int main()
int num2 = 15
return 0;
Output:
Explanation:
In binary 20 = 00010100
In binary 15 = 00001111
00010100
^ 00001111