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

README.md

Bit operations

Here are a few exercises on bit operations.

Running the solutions

Edit exercises.py. Then load the code in Python interpreter:

python3 -i exercises.py

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

Running the tests

I recommend 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.

Cheatsheet

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)
0x7f

>>> bin(42)
0b101010

You can also insert _ for readability:

0b1010_1111_0000_0001

Useful links