Operator (&, |, ^ ~, < <, > >)
Commonly used operators have &, |, ^
The operator | describe |
---|---|
& | with |
\ | or |
^ | Exclusive or |
The truth table is as follows
p | q | p&q | p \ q | p^q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Note: 0 means false and 1 means true
That is
0, 0 = 0
0 and 1 = 0
1 and 1 = 1
1 & 0 = 0
0 | 0 = 0
0 | = 1
1 | = 1
1 | 0 = 1
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 1 = 0
1 ^ 0 = 1
Now we have two numbers A=10 and B=6
Their binary format is
A = 0110 = 1010, B
A&B=0010
A|B=1110
A^B=1100
Bitwise operators also include ~, <<, >>
The operator | describe |
---|---|
~ | The bitwise inverse operator, that is, 0 becomes 1,1 becomes 0 |
<< | Binary left shift operator. The value of the left-hand operand moves to the left by the number of digits specified by the right-hand operand |
>> | Binary right shift operator. The value of the left operand moves to the right by the number of digits specified by the right operand |
~A = 0101
A<<2=1000
A>>2=0010