Bitwise Operators
The bitwise operators are the operators used to
perform the operations on the data at the bit-level. In the arithmetic-logic unit, mathematical operations like addition,
subtraction, multiplication and division are done in bit-level.
Operator |
Meaning of operator |
& |
Bitwise AND operator |
| |
Bitwise OR operator |
^ |
Bitwise exclusive OR
operator |
~ |
One's complement
operator (unary operator) |
<< |
Left shift operator |
>> |
Right shift operator |
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal)
1. The <<
(left shift) left shifts the bits of the first
operand, the second operand decides the number of places to shift.
Example1
Left shift operator shifts all bits towards left by a certain
number of specified bits. The bit positions that have been vacated by the left
shift operator are filled with 0. The symbol of the left
shift operator is <<
.
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)
Example 2
A=60
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 = 240 i.e., 1111 0000 |
Right Shift Operator
2. The >>
(right shift) in right shifts the bits of the
first operand, the second operand decides the number of places to shift.
212 = 11010100 (In binary)
212 >> 2 = 00110101 (In binary) [Right shift by two bits]
212 >> 7 = 00000001 (In binary)
212 >> 8 = 00000000
212 >> 0 = 11010100 (No Shift)
>>
.
Example2
A=60
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 = 15 i.e., 0000 1111 |
3. The ~
(bitwise NOT) in C takes one number and inverts all bits of it.
~
.
35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)
#include <stdio.h>
main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b;
/* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
}
Output-:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
Application of Bitwise Operator
A bitwise operator works with the binary
representation of a number rather than that number's value.
·
Encryption,
·
Compression,
·
Graphics,
·
Communications
over ports/sockets
·
Embedded
systems programming
·
Finite
state machine.
0 comments:
Post a Comment