A Neat Bitwise Trick for Swapping Even and Odd Bits (dailycodingproblem.com) 1 points by lawrencewu 8y ago ↗ HN
[–] joezydeco 8y ago ↗ 8 bit input? Use a pre-generated lookup table. Store it in ROM.The table deference will take 1 or 2 operations instead of the AND/SHIFT/PUSH/AND/SHIFT/POP/OR described in the answer. [–] lawrencewu 8y ago ↗ You're right, 8 bits is small enough to just store in a table.This trick will work for any number of bits though. You might not want to use 2^64 entries in your table. [–] joezydeco 8y ago ↗ The question was an unsigned 8-bit input, not an arbitrary number.And the "trick" given in the answer still depends on a fixed number of input bits, no matter if it's 8, 32, or 10^100.
[–] lawrencewu 8y ago ↗ You're right, 8 bits is small enough to just store in a table.This trick will work for any number of bits though. You might not want to use 2^64 entries in your table. [–] joezydeco 8y ago ↗ The question was an unsigned 8-bit input, not an arbitrary number.And the "trick" given in the answer still depends on a fixed number of input bits, no matter if it's 8, 32, or 10^100.
[–] joezydeco 8y ago ↗ The question was an unsigned 8-bit input, not an arbitrary number.And the "trick" given in the answer still depends on a fixed number of input bits, no matter if it's 8, 32, or 10^100.
3 comments
[ 2.6 ms ] story [ 18.4 ms ] threadThe table deference will take 1 or 2 operations instead of the AND/SHIFT/PUSH/AND/SHIFT/POP/OR described in the answer.
This trick will work for any number of bits though. You might not want to use 2^64 entries in your table.
And the "trick" given in the answer still depends on a fixed number of input bits, no matter if it's 8, 32, or 10^100.