A381839 In the binary expansion of n (without leading zeros): complement the bits strictly between the leftmost and the rightmost 0's, if any.
0, 1, 2, 3, 4, 5, 6, 7, 10, 9, 8, 11, 12, 13, 14, 15, 22, 21, 20, 19, 18, 17, 16, 23, 26, 25, 24, 27, 28, 29, 30, 31, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 47, 54, 53, 52, 51, 50, 49, 48, 55, 58, 57, 56, 59, 60, 61, 62, 63, 94, 93, 92, 91
Offset: 0
Examples
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 4 100 100 5 5 101 101 6 6 110 110 7 7 111 111 8 10 1000 1010 9 9 1001 1001 10 8 1010 1000 11 11 1011 1011 12 12 1100 1100 13 13 1101 1101 14 14 1110 1110 15 15 1111 1111 16 22 10000 10110
Links
Programs
-
PARI
a(n) = { my (b = binary(n)); for (i = 1, #b, if (b[i]==0, forstep (j = #b, 1, -1, if (b[j]==0, for (k = i+1, j-1, b[k] = 1-b[k];); return (fromdigits(b, 2)););););); return (n); }
-
Python
def a(n): b = bin(n)[2:] zl, zr = b.find('0'), b.rfind('0') return n if abs(zl-zr) < 2 else int(b[:zl+1]+"".join('0' if bi == '1' else '1' for bi in b[zl+1:zr])+b[zr:], 2) print([a(n) for n in range(70)]) # Michael S. Branicky, Mar 09 2025
-
Python
def A381839(n): return n^((1<
1 else n # Chai Wah Wu, Mar 09 2025
Formula
a(2*n + 1) = 2*a(n) + 1.
Comments