FunctionRacerProblemsRace

Swap Even and Odd Bits

EasyBitsPython, JavaScript, TypeScript, Java, C++

Number the bits of n from the right starting at 0. Swap bit 0 with bit 1, bit 2 with bit 3, bit 4 with bit 5, and so on, then return the new number.

Example 1

n 23
returns 43

10111 is 010111 padded, swapped pairs give 101011 = 43.

Example 2

n 2
returns 1

10 becomes 01 = 1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Bits problems