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 , so the solution in the fewest bytes in each language wins.

• I'd suggest adding an odd number between 2^31+1 and 2^32-3, as some answers are failing at that. Commented Jun 2, 2017 at 8:06
• Related. Commented Jun 2, 2017 at 8:49
• Since you just added 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 are 1. Commented Jun 3, 2017 at 4:16
• I'm a fool. Thank you. Will fix that Commented Jun 3, 2017 at 12:10
• @musicman523 I just happened to be browsing active questions and saw this one. And noticed that you still haven't added the requested test cases. Commented Apr 29, 2019 at 13:47

x86-64 assembly, 5 4 bytes

0:   97                      xchg   %eax,%edi
1:   d1 c0                   rol    %eax
3:   c3                      retq

A function using the C calling convention that bitwise rotates its argument left by 1 bit.

• Dammit - I was about to post exactly this - well done :) Commented Jun 1, 2017 at 23:06
• assembly beats Jelly :o Commented Jun 1, 2017 at 23:44
• Isn't this multiplying by 2? If so, then my 2 byte Pyth answer probably wins Commented Jun 2, 2017 at 17:08
• @NoOneIsHere No, this is not multiplication by 2. Multiplication by 2 sends half of the inputs outside of the required range, and if you ignore the overflow bit on the left, you’ve decreased the Hamming weight by 1. This is a bitwise rotation, which brings the overflow bit back in from the right. Commented Jun 2, 2017 at 17:51
• @DigitalTrauma GCC 4.9.0 and later are smart enough to compile n << 1 | n >> 31 into rol instead of ror (saving a byte). Commented Jun 3, 2017 at 7:08

Python, 20 bytes

lambda x:x*2%~-2**32

Bitwise rotation left by 1 bit.

MATL, 9 bytes

32&B1YSXB

Circularly shifts the 32-digit binary representation one step to the right.

Try it online!

Jelly, 10 8 bytes

‘&~^^N&$Swaps the least significant set and unset bit. Try it online! How it works ‘&~^^N&$  Main link. Argument: n

‘         Increment; yield n+1, toggling all trailing set bits and the rightmost
unset bit.
~       Bitwise NOT; yield ~n, toggling ALL bits of n.
&        Bitwise AND; yield (n+1)&~n, keeping the only bit that differs in n+1 and
~n, i.e., the rightmost unset bit.
^      Perform bitwise XOR with n, toggling the rightmost unset bit.

Try it online!

Python 3, 45 bytes

lambda i:int(f'{i:32b}'[1:]+f'{i:32b}'[:1],2)

Try it online!

C++ (gcc), 45 39 bytes

-6 bytes thanx to ceilingcat

auto h(unsigned x){return x%2<<31|x/2;}

Try it online!

• -6 bytes using x= instead of return. Commented Feb 10, 2020 at 20:39
• That's not valid in C++. Commented Feb 11, 2020 at 22:07
• It doesn't have to be. This is code golf. Commented Feb 11, 2020 at 22:14