Bitwise Operators
Bitwise operators let you apply logical operations to binary numbers, by applying them per binary bit. They are not often used very often, but every now and then they come in very handy. For example, sometimes you might want to store multiple variables as a single binary number (e.g. instead of 8 different booleans) in order to minimise your memory footprint.
Watch the video and then answer the questions below.
Twelve-minute video
You can also view this video on YouTube
Key Points
Bitwise operators are like logical operators that are applied per bit. While logical operators take a pair of truth values and return a truth value, bitwise operators take a pair of binary values and return a binary value.
Bitwise AND takes two operands a
and b
, and return a value c
that is determined by applying logical conjunction to each bit in turn.
For example, imagine we had the 4-bit binary numbers 0011
and 1010
. Bitwise AND would apply logical conjunction on each bit, and return either 1 or 0 for that bit. Similarly for bitwise OR and XOR.
a | 0 | 0 | 1 | 1 |
---|---|---|---|---|
b | 1 | 0 | 1 | 0 |
AND | 0 | 0 | 1 | 0 |
OR | 1 | 0 | 1 | 1 |
XOR | 1 | 0 | 0 | 1 |
Bit shifts just shift all the bits left and right. There are multiple ways in which shifts can work: sometimes they shift in 0. Sometimes right shifts shift in the sign bit (i.e. -1 for negative). You can also get circular shifts (where the numbers wrap around) - it depends on your platform.
a | 0 | 0 | 1 | 1 |
---|---|---|---|---|
LEFT SHIFT | 0 | 1 | 1 | 0 |
RIGHT SHIFT | 0 | 0 | 0 | 1 |
Bitwise Operators in Java
In Java, you can do bitwise operations using the following operators:
&
bitwise AND|
bitwise OR^
bitwise XOR~
bitwise NOT<< n
left shift by n>> n
right shift by n
In the example below, we set two numerical values using the binary prefix 0b
. Then we print out the binary for each of the bitwise operations applied to these values.
Bitwise Operators in Python
In Python, you can do bitwise operations using the following operators:
&
bitwise AND|
bitwise OR^
bitwise XOR~
bitwise NOT<< n
left shift by n>> n
right shift by n
In the example below, we set two numerical values using the binary prefix 0b
. Then we print out the binary for each of the bitwise operations applied to these values.
Questions
1. Check your understanding
1. Logical Operators
Solve the following, where bitwise operators are applied to a pair of bytes. Give your answers without using the binary prefix 0b
.
Convert the following values from decimal into binary (you will want to pad the front with 0
s), and then perform the bitwise operator shown. Give your answers as 8-bit binary values without using a prefix.
For any value a
between 0-255
, give the result of the following bitwise operations in decimal:
2. Bit Shifts
Assume << n
and >> n
shift left and right by n, and that replacment values are always 0.
Without converting to binary, work out the answers to these. Give your answers in decimal.
Summary
In this section we learned how to use bitwise operators.
- You should be able to apply bitwise AND, OR and XOR, and left and right bit shifts both by hand and using Python.
- You should know that left shift on a binary number does multiplication by 2, and right shift on a binary number does integer division by 2.
One you’ve completed the questions above, you’re ready to move onto the propositional logic challenges.
- Previous
- Next