Bitwise operators are used for manipulation of data at
bit level. These operators are used for testing the bits, or shifting them
right or left. Bitwise operators may not be applied to float or double data
type. It is applicable to integer data types data only. Following are some
bitwise operators.
S.No.
|
Operator
|
Meaning
|
1
|
&
|
Bitwise
Logical AND
|
2
|
¦
|
Bitwise
Logical OR
|
3
|
^
|
Bitwise
Logical XOR
|
4
|
<<
|
Left
shift
|
5
|
>>
|
Right
shift
|
6
|
~
|
One's
complement
|
1. & (Bit-wise AND) : Binary
operator takes two operands of int type and performs bit-wise AND operation.
With same assumption on int size as above:
int a = 5; [binary : 0000 0101]
int b = 9; [binary : 0000 1001]
a & b yields [binary
: 0000 0001]
2. ¦ (Bit-wise OR) : Binary operator
takes two operands of int type and performs bit-wise OR operation. With
assumption that int size is 8-bits:
int a = 5; [binary : 0000 0101]
int b = 9; [binary : 0000 1001]
a | b yields [binary
: 0000 1101]
3. ^
(Bit-wise Logical XOR) : XOR gives 1 if only one of the operand is 1 else
0. With same assumption on int size as above:
int a = 5; [binary : 0000 0101]
int b = 9; [binary
: 0000 1001]
a ^ b yields [binary : 0000 1100]
4. <<
(Shift left) : This operator shifts the bits towards left padding the space
with 0 by given integer times.
int a = 5; [binary : 0000 0101]
a << 3 yeilds [binary : 0010 1000]
5. >>
(Shift right) : This operator shifts
the bits towards right padding the space with 0.
int a = 5; [binary
: 0000 0101]
a >> 3 yeilds [binary : 0000 0000]
6. ~ (one’s complement operator) : It
is a uniary operator that causes the bits of its operand to be inverted so that
1 becomes 0 and vice-versa. The opearator must always precede the operand and
must be integer type of all sizes. Assuming that int type is of 1 byte size:
int a = 5; [binary
: 0000 0101]
~a; [binary : 1111 1010]
Next Topic: Special Operators
Previous Topic: Conditional Operators
No comments:
Post a Comment