An Bitwise operator is used for the manipulation of data at the bit level. These operators are not applied for the float and double datatype.
Bitwise operator first converts the integer into its binary representation then performs its operation. Bitwise operators subsist of two digits, either 0 or 1. Some of the bitwise operators are (&, | , ^, ~)
Note: Shift Bitwise operators are used to shift the bits right to left. Some of the shift bitwise operators are(<<, >>)
|
Operator |
Description |
Syntax |
& |
Bitwise
AND |
Performs
bit-by-bit AND operation and returns the result. |
a
&& b |
| |
Bitwise
OR |
Performs
bit-by-bit OR operation and returns the result. |
a ||
b |
^ |
Bitwise
XOR |
Performs
bit-by-bit XOR operation and returns the result. |
a ^
b |
~ |
Bitwise
First Complement |
Flips
all the set and unset bits on the number. |
~a |
<< |
Bitwise
Leftshift |
Shifts
the number in binary form by one place in the operation and returns the
result. |
a
<< b |
>> |
Bitwise
Rightshilft |
Shifts
the number in binary form by one place in the operation and returns the
result. |
a
>> b |
We use the following truth table for the Bitwise Operators:
A |
B |
A &
B (Bitwise AND) |
A | B
(Bitwise OR) |
A ^ B
(Bitwise XOR) |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
SIMPLE C PROGRAMMING USING BITWISE OPERATORS
// C program
to illustrate the bitwise operators
#include
<stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing
results
printf("a & b : %d\n", a
& b);
printf("a | b : %d\n", a |
b);
printf("a ^ b : %d\n", a ^
b);
printf("~a : %d\n", ~a);
printf("a >> b : %d\n",
a >> b);
printf("a << b : %d\n",
a << b);
return 0;
}
Output
a & b : 1
a | b : 29
a ^ b : 28
~a : -26
a >>
b : 0
a <<
b : 800
QUESTION FOR YOU
How shift operators work ? comment your answer.
0 Comments
Post a Comment