Bitwise AND :
Syntax :
Values for bit combinations
a b a & b
------------------------
0 0 0
0 1 0
1 0 0
1 1 1
In other words, a & b = 0
unless a = 1 and b = 1
.
What does A & B mean :
A & B implies a & b for all corresponding bits of A and B.
So, lets say,
A = 21 ( 10101 ) and B = 6 ( 110 )
A & B =
1 0 1 0 1
& 0 0 1 1 0
------------------
0 0 1 0 0 = 4.
Bitwise OR :
Syntax :
Values for bit combinations
a b a | b
------------------------
0 0 0
0 1 1
1 0 1
1 1 1
In other words, a | b = 1
unless a = 0 and b = 0
What does A | B mean :
A | B
implies a | b
for all corresponding bits of A and B.
So, lets say
A = 21 ( 10101 ) and B = 6 ( 110 )
A | B =
1 0 1 0 1
| 0 0 1 1 0
------------------
1 0 1 1 1 = 23.
Bitwise XOR :
Syntax :
Values for bit a, b :
a b a ^ b
------------------------
0 0 0
0 1 1
1 0 1
1 1 0
In other words, a ^ b = 1
when a and b are different.
What does A ^ B mean :
A ^ B implies a ^ b for all corresponding bits of A and B.
So, lets say
A = 21 ( 10101 ) and B = 6 ( 110 )
A ^ B =
1 0 1 0 1
^ 0 0 1 1 0
------------------
1 0 0 1 1 = 19.
Bitwise NOT :
Syntax :
Values for bit a :
a | ~a
-------|-------
0 | 1
1 | 0
Its the inverse of the bit.
What does ~A mean
~A implies inverting every single bit in A.
So, lets say
A = 21 ( 10101 ) and A is a char ( 1 byte )
~A =
0 0 0 1 0 1 0 1
--------------------------
1 1 1 0 1 0 1 0 = -22 ( Sign bit is 1 ).