login
A373038
In the binary expansion of any nonnegative number n, say with Hamming weight w, reverse the order of the w rightmost bits.
1
0, 1, 2, 3, 4, 6, 5, 7, 8, 10, 9, 14, 12, 13, 11, 15, 16, 18, 17, 22, 20, 21, 19, 30, 24, 28, 26, 29, 25, 27, 23, 31, 32, 34, 33, 38, 36, 37, 35, 46, 40, 44, 42, 45, 41, 43, 39, 62, 48, 52, 50, 60, 49, 58, 54, 61, 56, 57, 53, 59, 51, 55, 47, 63, 64, 66, 65, 70
OFFSET
0,3
COMMENTS
This sequence is a self-inverse permutation of the nonnegative integers.
FORMULA
A000120(a(n)) = A000120(n).
a(2^k - 1) = 2^k - 1 for any k >= 0.
a(2^k) = 2^k for any k >= 0.
EXAMPLE
For n = 43: the binary expansion of 43 is "101011", and has Hamming weight 4, reversing the 4 rightmost bits yields "101101", so a(43) = 45.
PROG
(PARI) a(n) = { my (b = binary(n), w = hammingweight(n)); fromdigits(concat( b[1..#b-w], Vecrev(b[#b-w+1..#b])), 2); }
(Python)
def a(n): w, b = n.bit_count(), bin(n)[2:]; return int(b[:-w]+b[-w:][::-1], 2)
print([a(n) for n in range(68)]) # Michael S. Branicky, May 24 2024
CROSSREFS
Sequence in context: A361926 A332805 A332807 * A267106 A275119 A218252
KEYWORD
nonn,base,easy
AUTHOR
Rémy Sigrist, May 20 2024
STATUS
approved