Bitwise Operators
Bitwise
operators manipulates data at bit level. A bitwise operator operates on each
bit of data. Those operators are used for testing, complementing or shifting
bits to the right or left. Bitwise operators may not be applied to a float or
double.
Operator
|
Meaning
|
&
|
Bitwise AND
|
|
|
Bitwise OR
|
^
|
Bitwise Ex-OR
|
~
|
Bitwise NOT
|
<<
|
Left shift
|
>>
|
Right shift
|
A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0.
Bitwise AND operator is "
&
" (ampersand). Again, this operator must not be confused with its Boolean "logical and" counterpart, which treats its operands as Boolean values, and is written "&&
" (two ampersands).
15 = 00001111 (In Binary)
20 = 00010100 (In Binary)
Bit Operation of 15 and 20
00001111
& 00010100
________
00000100 = 6 (In decimal)
A bitwise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits (the first of each; the second of each; and so on) and performing the logical inclusive OR operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 OR the second bit is 1 OR both bits are 1, and otherwise the result is 0.
Bitwise OR operator is "
|
" (pipe). Again, this operator must not be confused with its Boolean "logical or" counterpart, which treats its operands as Boolean values, and is written "||
" (two pipes).
15 = 00001111 (In Binary)
20 = 00010100 (In Binary)
Bit Operation of 15 and 20
00001111
& 00010100
________
000011111 = 31 (In decimal)
The bitwise
NOT, or complement, is a unary operation that performs logical
negation on each bit, forming the ones' complement of the given binary value.
Digits which were 0 become 1, and vice versa.
Bitwise NOT
operator is "
~
" (Tilde), this operator must not be confused with the
"logical not" operator, "!
" (Exclamation
point).15 = 00001111 (In Binary)
Bitwise complement Operation of 35
! 00001111
________
11110000
A bitwise
exclusive or takes two bit patterns of equal length and performs the
logical XOR operation on each pair of corresponding bits. The result in each
position is 1 if the two bits are different, and 0 if they are the same.
0101
XOR 0011
= 0110
Bitwise XOR operator is "
^
"
(caret).12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)
Left Shift Operator:
Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.
Syntax: var<< n
Here var is the variable to be shifted and n specifies the number of times to shift.
Ex:
22<2
binary of 22 = 1 0 1 1 0 <<1 = 1 0 1 1 0 0 =44
1 0 1 1 0 0 <<1= 1 0 1 1 0 0 0=88
For every left shift operation the value becomes double.
Right Shift Operator:
Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.
Syntax: var>> n
Here var is the variable to be shifted and n specifies the number of times to shift.
Ex:
22> 2
binary of 22 = 1 0 1 1 0 <<1
= 0 1 0 1 1 = 11
0 0 1 0 1<<1= 5
For every left shift operation the value becomes half.
No comments:
Post a Comment