Simple link
See http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious - this is a good start for you to see how it really should be done. There are some pretty interesting methods here. Especially take a look at
http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64Bits
to understand how to think about these issues.
TIP A
For an arbitrary word size, use the obvious method after using the mask to mask the bit you want to keep:
typeof(word) preserve_mask = \ ((1 << 8*sizeof(word)) - 1) & ~(typeof(word))((1 << K) - 1);
Where K is the number of bits you want to flip. preserve_mask will give you a mask to save part of the word that you do not want to flip. Please note that the above is not C code, but a concept that you must implement. I suggest first doing this within your processor; and then process arbitrary precision later (and only if necessary).
Tip B
Can you see how this can be done for an arbitrary length using the ReverseByteWith64Bits generalization?
Is it possible to execute a piece of food on N bits, where N ≢ 0 (mod 8)? can you use the result from hint A?
Let me know if you need more help.
source share