This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.
%I A381839 #14 Mar 10 2025 11:11:50 %S A381839 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, %T A381839 24,27,28,29,30,31,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,47,54, %U A381839 53,52,51,50,49,48,55,58,57,56,59,60,61,62,63,94,93,92,91 %N A381839 In the binary expansion of n (without leading zeros): complement the bits strictly between the leftmost and the rightmost 0's, if any. %C A381839 This sequence is a self-inverse permutation of the nonnegative integers. %C A381839 This sequence has similarities with A122155 (where we complement bits between the leftmost and the rightmost 1's). %C A381839 This sequence has infinitely many fixed points: A000225, A030130, and the positive numbers whose binary expansion have exactly two 0's that are also adjacent to each other. %H A381839 Rémy Sigrist, <a href="/A381839/b381839.txt">Table of n, a(n) for n = 0..8191</a> %H A381839 <a href="/index/Bi#binary">Index entries for sequences related to binary expansion of n</a> %H A381839 <a href="/index/Per#IntegerPermutation">Index entries for sequences that are permutations of the natural numbers</a> %F A381839 a(2*n + 1) = 2*a(n) + 1. %e A381839 The first terms, in decimal and in binary, are: %e A381839 n a(n) bin(n) bin(a(n)) %e A381839 -- ---- ------ --------- %e A381839 0 0 0 0 %e A381839 1 1 1 1 %e A381839 2 2 10 10 %e A381839 3 3 11 11 %e A381839 4 4 100 100 %e A381839 5 5 101 101 %e A381839 6 6 110 110 %e A381839 7 7 111 111 %e A381839 8 10 1000 1010 %e A381839 9 9 1001 1001 %e A381839 10 8 1010 1000 %e A381839 11 11 1011 1011 %e A381839 12 12 1100 1100 %e A381839 13 13 1101 1101 %e A381839 14 14 1110 1110 %e A381839 15 15 1111 1111 %e A381839 16 22 10000 10110 %o A381839 (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); } %o A381839 (Python) %o A381839 def a(n): %o A381839 b = bin(n)[2:] %o A381839 zl, zr = b.find('0'), b.rfind('0') %o A381839 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) %o A381839 print([a(n) for n in range(70)]) # _Michael S. Branicky_, Mar 09 2025 %o A381839 (Python) %o A381839 def A381839(n): return n^((1<<n.bit_length()-t-1)-1)^(((~n&n+1)<<1)-1) if n and (t:=(s:=bin(n)[2:]).find('0'))!=-1 and s.count('0')>1 else n # _Chai Wah Wu_, Mar 09 2025 %Y A381839 Cf. A000225, A030130, A122155, A381852. %K A381839 nonn,base,easy %O A381839 0,3 %A A381839 _Rémy Sigrist_, Mar 08 2025