Reverse Bits: Code explanation: Why some code in the book don't have comment?

#1

Can someone explain this code to me. I know it’s accessing precomputed reverse from the cache but I don’t get the way it’s doing it in this code. I have a hard time understanding the code especially after the return statement
def reverse_bits():
MASK_SIZE= 16
BIT_MASK=0xFFFF
return (PRECOMPUTED_REVERSE[x & BIT_MASK] <<(3* MASK_SIZE) |
PRECOMPUTED_REVERSE[(x >> MASK_SIZE) & BIT_MASK] << (2
* MASK_SIZE) | PRECOMPUTED_REVERSE[(x>> (2 *
MASK_SIZE))&BIT_MASK] << MASK_SIZE |
PRECOMPUTED_REVERSE[(x >> (3 * MASK_SIZE)) & BIT_MASK])

0 Likes

#2

Hi @johny,

You might want to use http://elementsofprogramminginterviews.com/solutions/python/parity3.py to see what is going on in the code. It is a very succinct code that does exactly what the book describes, we computes the parity for four disjointed part of a 64-bit integer and then compute the parity of those four together.

0 Likes

Is there an older solutions repo available?
#3

@johny check out the previous answer to this question from the author, Question about BitMask in 5.1- Compute Parity

0 Likes