The Runtime Theory
easyleetcode#bit-manipulation#masking

Reverse the Bits of a Machine Word

Practice masks, shifts, unsigned representation, and fixed-width reasoning by reversing a 32-bit integer.

The Runtime Theory Team1 min read
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inn=00000010100101000001111010011100 (32-bit)

out964176192

inn=00000000000000000000000000000001 (32-bit)

out2147483648

Reverse all 32 bits of the input word, including leading zeroes. Do not treat the displayed binary string as a variable-length number: the width is part of the problem.

Build the result one bit at a time. Extract the next low bit from the input, shift the result to make room, append that bit, and shift the input. State whether your language uses signed or unsigned right shift and how you keep the computation within 32 bits.

After the implementation works, write the intermediate result after the first four iterations. Explain why a leading zero in the input can become a high-order one in the output and why the decimal result may appear negative in a signed 32-bit type.

One dispatch a week

The trace behind each problem, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.

Not started

Sign in to save your learning progress.

Sign in to save