Background
The Hamming weight of an integer is the number of ones in its binary representation. For this challenge, integers are represented with 32 bits, and they are unsigned.
Challenge
Given an integer between 0 and 2^32-1 (non-inclusive), output a different integer within the same range, and also with the same Hamming weight.
Examples
Input (Decimal) | Input (Binary) | Hamming weight | Possible output (Decimal)
46 | 0b0010 1110 | 4 | 15
12 | 0b0000 1100 | 2 | 3
1 | 0b0000 0001 | 1 | 2
3 | 0b0000 0011 | 2 | 6
2^31 | 0b1000....0 | 1 | 1
2^31+2 | 0b1000...10 | 2 | 3
2^32-5 | 0b1111..011 | 31 | 2^31-1
2^32-2 | 0b1111....0 | 31 | 2^31-1
0 | 0b0000 0000 | 0 | None (This case need not be handled)
2^32-1 | 0b1111....1 | 32 | None (This case need not be handled)
Scoring
This is code-golf, so the solution in the fewest bytes in each language wins.
2^31+2
, I'll repeat that I said an odd number. The answers in question only failed when both the highest and the lowest bit are1
. \$\endgroup\$