Here are a few exercises on bit operations.
Running the solutions
exercises.py. Then load the code in Python interpreter:
python3 -i exercises.py >>> hex(replace_lowest_byte(0xabcd, 12)) '0x0'
Running the tests
pytest. You can install it using:
pip3 install --user pytest
Then just run:
pytest --doctest-modules -v exercises.py
This will run all the examples in the file.
and: a & b or: a | b xor: a ^ b not: ~a shift left: a << b shift right: a >> b
You can write the numbers as hexadecimal and binary:
0x12EF, 0xabcd, 0b10101010
Oo666) is also supported but you shouldn't need it.
Printing the numbers to hexadecimal and binary:
>>> hex(127) 0x7f >>> bin(42) 0b101010
You can also insert _ for readability: