
Anthony B. answered 05/26/19
Ph.D. Particle Physicist
Swap bits in a binary tree using bit-wise and (&), bit wise or (|), and bit shifts. This procedure is destructive to the input, so make sure to make a copy. I haven't tested this code and have not proven that it's the most efficient possible, but it should be very efficient and scale logarithmically with more bits.
Sivachandran M.
This works for me06/21/22
Sivachandran M.
//Swap the first 4 bits with the last 4 bits a = ((a & 0xffff0000) >> 16) | ((a & 0x0000ffff) << 16); //Next swap pairs of 2 bits a = ((a & 0xff00ff00) >> 8) | ((a & 0x00ff00ff) << 8); //Finally, swap every other bit. a = ((a & 0xf0f0f0f0) >> 4) | ((a & 0x0f0f0f0f) << 4);06/21/22