Find file History
Latest commit 9335588 Nov 10, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information. Add more exercises Nov 10, 2018 Add more exercises Nov 10, 2018

Bit operations

Here are a few exercises on bit operations.

Running the solutions

Edit Then load the code in Python interpreter:

python3 -i

>>> hex(replace_lowest_byte(0xabcd, 12))

Running the tests

I recommend pytest. You can install it using:

pip3 install --user pytest

Then just run:

pytest --doctest-modules -v

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

Octal (Oo666) is also supported but you shouldn't need it.

Printing the numbers to hexadecimal and binary:

>>> hex(127)

>>> bin(42)

You can also insert _ for readability:


Useful links