The Bit-wise Operators are used for manipulation of data at bit level.
These are classified into two types, Namely,
1. Bit-wise Logical Operators.
2. Bit-wise Shift Operators.
1.Bit-Wise Logical Operators:
These are used for bit-wise Logical decision making.
i) Bit-wise Logical AND ( & )
ii) Bit-wise Logical OR ( | )
iii) Bit-wise Logical XOR ( ^ )
A1 | A2 | A1&A2 | A1|A2 | A1^A2 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
0 | 1 | 0 | 1 | 1 |
0 | 0 | 0 | 0 | 0 |
Eg1: int A=5, B=6;
then A&B is shown in below table
Values | Binary Form |
A=5 | 1 0 1 |
B=6 | 1 1 0 |
A&B = 4 | 1 0 0 |
Eg2: int A=5, B=6;
then A|B is shown in below table
Values | Binary Form |
A=5 | 1 0 1 |
B=6 | 1 1 0 |
A|B = 7 | 1 1 1 |
Eg3: int A=5, B=6;
then A^B is shown in below table
Values | Binary Form |
A=5 | 1 0 1 |
B=6 | 1 1 0 |
A^B = 3 | 0 1 1 |
2. Bit-wise Shift Operators:
The Bit-wise Shift Operators is used to take binary patterns and shift the bits to the left to right or right to left.
These are Two types i.e. i) Left shift (<<)
ii) Right Shift (>>)
Eg i): int A=4, B;
A=4 => 1 0 0 (Binary Form)
B=A<<2; i.e. A is to be left shifted by 2 bits and store the value in B.
1 | 0 | 0 |
Hence it became,
1 | 0 | 0 | 0 | 0 |
Then the Value of B is :
1 0 0 0 0 = 24 = 16
Eg ii): int B=4, C;
B=4 => 1 0 0 (Binary Form)
C=B>>1; i.e. B is to be Right shifted by 1 bit and store the value in C.
1 | 0 | 0 |
Hence it became,
1 | 0 |
Then the value of C is :
1 0 = 21 = 2
-----------------------------------------------------------------------------------------------------------------------------
Next Topic : Bit-wise Logical Operators
Previous Topic : Increment and Decrement Operator
No comments:
Post a Comment