how to get last n bits by bit-op?

Viewed 12918

I want to get last n bit of number, for example:

num = 0b111111111
# if I want to get last 8 bits, it will be 0b11111111
# if I want to get last 2 bits, it will be 0b11

I thought this can be ok:

bits = 1 << n
little = num & (~bits)

but this is wrong, if n = 8, it get 0b110111111

2 Answers
Related