C

Asked • 05/19/19

Most Efficient Algorithm for Bit Reversal ( from MSB->LSB to LSB->MSB) in C?

What is the best algorithm to achieve the following:`0010 0000 => 0000 0100`The conversion is from MSB->LSB to LSB->MSB. All bits must be reversed; that is, this is *not* endianness-swapping.

1 Expert Answer

By:

Anthony B. answered • 05/26/19

Tutor
5.0 (28)

Ph.D. Particle Physicist

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);
Report

06/21/22

Sivachandran M.

This works for me
Report

06/21/22

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.