Bitwise
Operators
C language supports bitwise
operators as &, |, ^, ~, << and >> operators. These operators are used to manipulate data
of the operands at bit level i.e., operations are performed on corresponding
bits of the given operands.
Bitwise operators can operate only on
integer quantities such as int, char, short int, long int etc.,
Bit-Wise AND,
Bit-Wise OR and Bit-Wise Exclusive OR follows the following bit
comparison tables.
Bit1
|
Bit2
|
Bit1 & Bit2
|
Bit1 | Bit2
|
Bit1 ^ Bit2
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
1
|
1
|
0
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
0
|
Here,
Ø Bit-Wise AND
compares the corresponding bits of the operands and produces 1 when both bits
are 1; 0 otherwise.
Ø Bit-Wise OR
compares the corresponding bits of the operands and produces 0 when both bits
are 0; 1 otherwise.
Ø Bit-Wise
Exclusive OR compares the corresponding bits of the operands and produces 0
when both bits are same; 1 otherwise.
For
the above operations consider the following number conversion system for octal
and hexa-decimal numbers.
NUMBER
|
OCTAL
|
HEXADECIMAL
|
0
|
000
|
0000
|
1
|
001
|
0001
|
2
|
010
|
0010
|
3
|
011
|
0011
|
4
|
100
|
0100
|
5
|
101
|
0101
|
6
|
110
|
0110
|
7
|
111
|
0111
|
8
|
|
1000
|
9
|
|
1001
|
A - 10
|
|
1010
|
B – 11
|
|
1011
|
C – 12
|
|
1100
|
D – 13
|
|
1101
|
E – 14
|
|
1110
|
F - 15
|
|
1111
|
Example:
1. X :
011 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 1
Y :
027 0 0 0 0 0 0 0 0 0 0 0
1 0 1 1 1 (Octal)
X&Y : 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 : 1
X|Y : 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 : 37
X^Y : 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 0 : 36
2. X :
0X7B 0 0 0 0 0 0 0
0 0 1 1 1 1 0 1 1
Y :
0X129 0 0 0 0 0 0 0 1 0
0 1 0 1 0 0 1 (Hexadecimal)
X&Y : 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 1 : 29
X|Y : 0 0 0 0 0 0 0 1 0
1 1 1 1 0 1 1 : 17B
X^Y : 0 0 0 0 0 0 0 1 0
1 0 1 0 0 1 0 : 152
One’s Complement
(or) Bit Negation
operator is a unary operator that complements the bits of the given operand.
i.e., Bit 0 converted into Bit 1 and Bit 1 converted into Bit 0.
Example:
X : 0X7B 0
0 0 0 0 0 0 0 0 1 1 1 1 0 1 1
~X : 1
1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 : FF84
Shift Operators: Left shift operator
(<<) and right shift operator (>>) are known as shift
operators. They perform left and right
shifts of their operand bits by the number of bit positions specified by the
argument which must be integer and positive number.
Left Shift
Operator Syntax:
VariableName <<
NoOfBitPositions;
Example: Let X = 36
X << 2;
Ø While performing
left shift operations, there is a loss of data at MSB side.
Ø The vacated
positions at LSB side are filled with zeros.
Ø For each shift
value by the number of bits, the result value is equivalent to multiplication
by 2.
Right Shift
Operator Syntax:
VariableName >>
NoOfBitPositions;
Example: Let X = 36
X >> 2;
Ø While performing
right shift operations, there is a loss of data at LSB side.
Ø The vacated
positions at MSB side are filled with zeros.
Ø For each shift
value by the number of bits, the result value is equivalent to division by 2.
/*
EXAMPLE PROGRAM FOR SHIFT OPERATORS */
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
x=36;
printf("\nLeft Shift
Result:%d",x<<2);
x=36;
printf("\nRight Shift
Result:%d",x>>2);
}
No comments:
Post a Comment