>>
is a right bitwise shift. It takes the bits and shifts them right n places1. For example, let's examine 35 >> 2
:
35 = 100011 shift two places
001000 = 8
And indeed, 35 >> 2 == 8
.
|
is a bitwise OR. It takes each bit in each operand and ORs them together. You can envision it as a sort of binary addition, but you don't carry when both top and bottom are 1
. For example, here's 5 | 3
:
5 = 101
3 = 011
| -----
111 = 7
And indeed, 5 | 3 == 7
.
Finally, &
is a bitwise AND. It takes each bit in each operand, except instead of giving 1 if either one bit OR the other is one, it gives 1 if one bit AND the other are both one. For example, here's 5 & 3
:
5 = 101
3 = 011
& -----
001 = 1
Try it out; 5 & 3 == 1
.
Some other ones you might want to be aware of are <<
, which is a left bitwise shift, and ^
, which is an XOR (0 when both bits are the same, 1 if they're different).
1 Actually, it's n modulo 32. 1 >> 32
is 1
. Not sure why.