Operator | Description | Symbol |
---|---|---|
AND | Sets each bit to 1 if both bits are 1 | & |
OR | Sets each bit to 1 if at least one bit is 1 | ` |
XOR | Sets each bit to 1 if the bits are different | ^ |
NOT | Inverts all the bits | ~ |
Left Shift | Shifts bits to the left | << |
Right Shift | Shifts bits to the right | >> |
&
)A | B | A & B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
|
)^
)A | B | A ^ B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
~
)0100
).1011
).-5
.>>
)<<
)~
&
^
|
<<
and >>
~
) is also known as the complement operator, as it complements all bits of a number.&
) operates on binary values and returns an integer.and
) operates on Boolean values and returns True
or False
.