Chapter 02-1 – Bitwise Operators
in C programming
Bit-level operations
In processor, mathematical operations like:
addition, subtraction, addition and division
are done in bit-level to process faster and
save power. To perform bit-level operations
in C programming, bitwise operators are
used.
Department of Engineering 2
Bitwise operators
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Department of Engineering 3
Bitwise AND operator &
The output of bitwise AND is 1 if the corresponding bits of
all operands is 1. If either bit of an operand is 0, the
result of corresponding bit is evaluated to 0.
Let us suppose the bitwise AND operation of two integers 12
and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
00001000 = 8 (In decima
l)
Department of Engineering 4
Example #1: Bitwise AND
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf(“Output = %d”, a&b);
return 0;
}
Output
Output = 8
Department of Engineering 5
Bitwise OR operator |
The output of bitwise OR is 1 if at least one corresponding bit of two
operands is 1. In C Programming, bitwise OR operator is denoted by
|.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
00011101 = 29 (In decimal)
Department of Engineering 6
Example #2: Bitwise OR
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf(“Output = %d”, a|b);
return 0;
}
Output
Output = 29
Department of Engineering 7
Bitwise XOR (exclusive OR)
operator ^
The result of bitwise XOR operator is 1 if the corresponding
bits of two operands are opposite. It is denoted by ^.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
00010101 = 21 (In decimal)
Department of Engineering 8
Example #3: Bitwise XOR
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf(“Output = %d”, a^b);
return 0;
}
Output
Output = 21
Department of Engineering 9
~ 00100011 ________ 11011100 = 220 (In decimal)
Bitwise complement operator ~
Bitwise compliment operator is an unary operator (works on
one operand only). It changes the 1 to 0 and 0 to 1. It is
denoted by ~.
35= 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
11011100 = 220 (In decimal)
Department of Engineering 10
Shift Operators in C programming
There are two shift operators in C programming:
Right shift operator >>
Right shift operator shifts all bits towards right by
certain number of specified bits. It is denoted by >>.
Left shift operator <<
Left shift operator shifts all bits towards left by certain
number of specified bits. It is denoted by <<.
Department of Engineering 11
Right Shift Operator >>
212 = 11010100 (In binary)
212>>2 00110101 (In binary) [Right shift by tw
o bits]
212>>7 00000001 (In binary)
212>>8 00000000
212>>0 11010100 (No Shift)
Department of Engineering 12
Left Shift Operator <<
212 = 11010100 (In binary)
212<<1 = 110101000 (In binary)[Left shift
by one bit]
212<<0 = 11010100 (Shift by 0)
212<<4 = 110101000000 (In binary)
=3392 (In decimal)
Department of Engineering 13
Example #4: Shift Operators
#include <stdio.h>
int main()
{
int num = 212, i;
for (i = 0; i <= 2; ++i)
printf(“Right shift by %d: %d\n”, i, num>>i);
printf(“\n”);
for (i=0; i <= 2; ++i)
printf(“Left shift by %d: %d\n”, i, num<<i);
return 0;
}
Department of Engineering 14
Shift operations Outputs
Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53
Left Shift by 0: 212
Left Shift by 1: 424
Left Shift by 2: 848
Department of Engineering 15
#include <stdio.h>
void displayBin(int);
int main(void)
{
int num = 212, i;
// Get the Lowest bit:
//result = value & 1;
// Get the Highest bit:
//result = value >> (sizeof(unsigned int) - 1);
// Get the nth bit (LSB is 0):
//result = (value >> n) & 1
Department of Engineering 16
for (i=0; i <= 3; ++i)
{
printf("Right shift by %d: %d\n", i, num>>=i);
displayBin(num);
}
printf("\n");
num = 212;
for (i=0; i <= 3; ++i)
{
printf("Left shift by %d: %d\n", i, num<<=i);
displayBin(num);
}
return 0;
}
Department of Engineering 17
void displayBin(int num)
{//display the integer in binary format
int i;
for (i=8*sizeof(int)-1; i>=0; i--)
{
if (i/8 <= 1) printf("%d",(num>>i)&1);
}
printf("\n");
return;
}
Department of Engineering 18
End
Department of Engineering 19