Bit Operations
Ø Bitwise one's compliment operator will invert the binary bits.
Ø Bitwise compliment operator is an unary operator. It is denoted by ~.
Ø It changes 1 to 0 and 0 to 1.
i.e. If a bit is 1, it will change it to 0. and If the bit is 0, it will change
it to 1.
5 = (00000101)2
~5 = (11111010)2. It
will convert all 1 to 0 and 0 to 1.
#include <conio.h>
void
main()
{
int a = 9;
int c;
c = ~(a);
printf("The Ones Complement is
%d", c);
getch();
}
Right shift Operator: The right shift operator is
represented by >>. It needs two operands. It shifts each bit in its left
operand to the right.
The >>
(right shift) in C takes two numbers, right shifts the bits of the first
operand, the second operand decides the number of places to shift. In other words,
right shifting an integer “a” with an integer “b” can be denoted as a>>b.
#include
<conio.h>
void
main()
{
int a = 5;
int c;
c = a>>1;
printf("After Right Shift result is %d",
c);
getch();
}
The <<
(left shift) in C takes two numbers, left shifts the bits of the first operand,
the second operand decides the number of places to shift. In other words, left
shifting an integer “a” with an integer “b” can be denoted as a<<b.
#include
<conio.h>
void
main()
{
int a = 5;
int c;
c = a<<1;
printf("After Left Shift result is %d",
c);
getch();
}
Bit wise operator- AND
This operator is represented as &. It is different than &&, the logical AND operator. The bit
wise &
operator operates on
two operands. While operating upon these two operands they are compared on a
bit-by-bit basis so both the operands must be of the same data type.
The rules for bit wise & operations are as follows:
For example:
A=5;
B=4;
C = A & B
It can be represented as
00000101 Original Bit Pattern
00000100 Mask with 4
----------------
00000000 Result
Program:
#include
<conio.h>
void
main()
{
int a = 5, b = 4;
int c;
c = a & b;
printf("After AND operation result is %d",
c);
getch();
}
Bit wise operator- OR
This operator is represented as |. It is different than ||, the logical OR operator. The bit
wise |
operator operates on
two operands. While operating upon these two operands they are compared on a
bit-by-bit basis so both the operands must be of the same data type.
For example:
A=5;
B=4;
C = A | B
It can be represented as
00000101 Original Bit Pattern
00000100 Mask with 4
----------------
00000101 Result
#include
<conio.h>
void
main()
{
int a = 5, b = 4;
int c;
c = a | b;
printf("After OR operation result is %d",
c);
getch();
}
Bit wise operator- XOR
This operator is represented as ^. The bit wise ^ operator operates on two operands.
While operating upon these two operands they are compared on a bit-by-bit basis
so both the operands must be of the same data type.
A=5;
B=4;
C = A ^ B
It can be represented as
00000101 Original Bit Pattern
00000100 Mask with 4
----------------
00000001 Result
Program:
#include
<conio.h>
void
main()
{
int a = 5, b = 4;
int c;
c = a ^ b;
printf("After XOR operation result is %d",
c);
getch();
}
No comments:
Post a Comment