login
A381852
In the binary expansion of n (without leading zeros): complement the bits strictly to the right of the leftmost zero digit, if any.
2
0, 1, 2, 3, 5, 4, 6, 7, 11, 10, 9, 8, 13, 12, 14, 15, 23, 22, 21, 20, 19, 18, 17, 16, 27, 26, 25, 24, 29, 28, 30, 31, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 55, 54, 53, 52, 51, 50, 49, 48, 59, 58, 57, 56, 61, 60, 62, 63, 95, 94, 93, 92
OFFSET
0,3
COMMENTS
This sequence is a self-inverse permutation of the nonnegative integers.
This sequence has similarities with A054429 (where we complement the bits to the right of the leftmost one digit).
FORMULA
a(n) = n iff n = 0 or n belongs to A075427.
a(n) = XOR(n,2^(A063250(n)-1)-1) if n>0 and A063250(n)>0. Otherwise a(n) = n. - Chai Wah Wu, Mar 09 2025
EXAMPLE
The first terms, in decimal and in binary, are:
n a(n) bin(n) bin(a(n))
-- ---- ------ ---------
0 0 0 0
1 1 1 1
2 2 10 10
3 3 11 11
4 5 100 101
5 4 101 100
6 6 110 110
7 7 111 111
8 11 1000 1011
9 10 1001 1010
10 9 1010 1001
11 8 1011 1000
12 13 1100 1101
13 12 1101 1100
14 14 1110 1110
15 15 1111 1111
16 23 10000 10111
PROG
(PARI) a(n) = { my (b = binary(n)); for (i = 1, #b, if (b[i]==0, for (j = i+1, #b, b[j] = 1-b[j]; ); return (fromdigits(b, 2)); ); ); return (n); }
(Python)
def a(n):
b = bin(n)[2:]
zi = b.find('0')
return n if zi == -1 else int(b[:zi+1]+"".join('0' if bi == '1' else '1' for bi in b[zi+1:]), 2)
print([a(n) for n in range(70)]) # Michael S. Branicky, Mar 09 2025
(Python)
def A381852(n): return n^((1<<n.bit_length()-t-1)-1) if n and (t:=bin(n)[2:].find('0'))!=-1 else n # Chai Wah Wu, Mar 09 2025
CROSSREFS
KEYWORD
nonn,base,easy
AUTHOR
Rémy Sigrist, Mar 08 2025
STATUS
approved