Your Challenge is to take a binary string as input and get all binary numbers between itself and the reverse of its string representation (the reverse may be less than or greater than the original input).

Output must be padded with zeroes to be the same length as the input.

The Order in which the values are displayed doesn't matter, but the Code needs to work with Values that are bigger than its reverse.

Test Cases

Input: (1 or 0)
Output: None, string and its reverse are identical

Input: (any palindrome binary)
Output: None, same as above

Input: 01
Output: None, since there is no binary between 01 and 10 (1 and 2)

Input: 001
Output: 010,011 (Numbers between 001 (1) and 100 (4): 2,3)

Input: 100
Output: 010,011 (Same as above)

Input: 0010
Output: 0011 (Numbers between 0010 (2) and 0100 (4): 3)

This is Code Golf, shortest code wins!

EDIT: After seeing some of the solutions i have removed the requirement of the number representation of the binary being in ascending order. This is my first code golf challenge and i don't want to make it to tedious, also added some clarifications from the comments.


14 Answers 14


Jelly, 23 bytes


Try It Online!

Follows the input format very strictly (input as string, output as list of strings including padding zeroes).

Jelly, 8 bytes


Try It Online!

Using actually reasonable I/O formatting, if we accept a list of digits and output as digit lists without doing leading zeroes, this is a lot more suitable for Jelly.


Vyxal, 12 bytes


Try it Online! Outputs as a list of lists of bits.

ṘB           # x reversed as an integer
  $B         # x as an integer
    rḢ       # Exclusive range
      ƛ      # Map...
       b     # convert to binary
          ∆Z # Zfill to...
        ?L   # Input length
  • 1
    \$\begingroup\$ Output needs to have leading zeroes \$\endgroup\$
    – pxeger
    Commented Dec 15, 2021 at 20:57

x86-16 machine code, 58 57 bytes

00000000: 8be9 e82a 008b da8b cdfd 4ee8 2100 3bda  ...*......N.!.;.
00000010: 7602 87d3 433b da73 1f8b cd8b c303 f957  v...C;.s.......W
00000020: 4fd1 e850 b000 1430 aa58 e2f5 5feb e533  O..P...0.X.._..3
00000030: d2ac d0e8 13d2 e2f9 c3                   .........


8B E9           MOV  BP, CX         ; save input "Binary Pad" length to BP 
E8 002A         CALL BINDEC         ; BX = forward input value 
8B DA           MOV  BX, DX         ; forward number in BX 
8B CD           MOV  CX, BP         ; restore input pad length 
FD              STD                 ; reverse string load/store direction 
4E              DEC  SI             ; fix offset - SI is one char off 
E8 0021         CALL BINDEC         ; DX = reversed input value 
3B DA           CMP  BX, DX         ; is reversed number smaller? 
76 02           JBE  MID_LOOP       ; if not, start looping through middle numbers 
87 D3           XCHG DX, BX         ; otherwise swap numbers 
43              INC  BX             ; increment middle number 
3B DA           CMP  BX, DX         ; is less than end number? 
73 1F           JAE  DONE           ; if not, return
8B CD           MOV  CX, BP         ; restore input pad length
8B C3           MOV  AX, BX         ; AX = next number
            DECBIN:                 ; convert value in AX to string at [DI] 
03 F9           ADD  DI, CX         ; move pointer to end of padded output buffer 
57              PUSH DI             ; save position for next number 
4F              DEC  DI             ; fix offset - DI is one char off 
D1 E8           SHR  AX, 1          ; CF = LSb of number 
50              PUSH AX             ; save number
B0 00           MOV  AL, 0          ; zero AL
14 30           ADC  AL, '0'        ; convert CF to ASCII 
AA              STOSB               ; write char to [DI] 
58              POP  AX             ; restore number
E2 F5           LOOP LOOP_BIT       ; loop next bit              
5F              POP  DI             ; restore next number output offset
EB E5           JMP  MID_LOOP       ; keep looping
            BINDEC:                 ; convert ASCII binary string at [SI] to DX 
33 D2           XOR  DX, DX         ; zero output value
AC              LODSB               ; load next char into AL
D0 E8           SHR  AL, 1          ; CF = LSb of ASCII char
13 D2           ADC  DX, DX         ; shift LSb left onto result
E2 F9           LOOP LOOP_DEC       ; loop until end of string
C3              RET                 ; return to caller 

Callable function, input binary string in [SI] length in CX, output to [DI].

No helpful built-ins in machine code... just brute force string to decimal and back stuff!

Test program output:

enter image description here


Pip -p, 27 bytes


Replit! Or, here's a 28-byte equivalent in Pip Classic: Try it online!

Could be 17 bytes without the 0-padding requirement.


                      [   ]  List containing
                       Ra     Reverse of argument
                         a    and argument
                    FB       Convert each from binary
                  SN         Sort in ascending order
              [1]+           Add 1 to the first (smaller) number in the list
            $,               Create a range from the first (inclusive) to the second (exclusive)
          TB                 Convert each to binary
         M                   Map this function to each:
  #a-#_                       Length of original input minus length of argument
0X                            That many zeros
       ._                     Concatenate with argument

JavaScript (ES6), 88 bytes

  • 1
    \$\begingroup\$ Um, I think output has to be in binary \$\endgroup\$
    – emanresu A
    Commented Dec 15, 2021 at 20:37
  • \$\begingroup\$ @emanresuA Oh, oops \$\endgroup\$ Commented Dec 15, 2021 at 20:40

Python, 87 bytes

lambda s:[f"{x:b}".zfill(len(s))for x in range(*sorted([int(s,2),int(s[::-1],2)]))][1:]

Attempt This Online!

Can probably be shorter, but I'm not interested in golfing it.

  • \$\begingroup\$ lambda x:[f"{i:0{len(x)}b}"for i in range(*sorted([int(x,2),int(x[::-1],2)]))][1:] for -5 bytes \$\endgroup\$
    – Jakque
    Commented Dec 19, 2021 at 12:01

Charcoal, 26 bytes


Try it online! Link is to verbose version of code. Outputs the values in descending order. Explanation:


Push the input and its reverse to the predefined empty list.


Convert the minimum and maximum of the input and its reverse from base 2, loop over the intermediate values, convert them back to base 2, then print them in reverse order and right-justified.


Fill unprinted areas with zeros.


Python3, 74 bytes:

lambda x:[f"{i:b}".zfill(len(x))for i in range(int(x,2)+1,int(x[::-1],2))]

Try it online!

  • \$\begingroup\$ This does not work with input bigger than its reverse, fixing it makes it identical to the Answer of pxeger, still cool though \$\endgroup\$ Commented Dec 16, 2021 at 12:10

Retina, 62 bytes


Try it online! Link includes test cases. Explanation:


Append the reverse of the input.


Sort in ascending order.


Repeat until the last two lines are identical.


Duplicate the last but one line.


Increment the newly duplicated line.


Delete the original input.


Delete the line that duplicated the last line.


APL (Dyalog Extended), 33 29 27 bytes


Try it online!

Explanation (slightly outdated):

⍎¨ Evaluate each character of the input to get a vector of 0's and 1's.
…⍥⊥ Inclusive range, after converting the arguments from base 2: of the the vector and its reverse ⌽⍵.
¯1↓1↓ Make the range exclusive by dropping one value from the start and one from the end.
⊤¨ Convert all values in the exclusive range to base 2.
(-≢⍵)↑¨ Pad the binary values to the length of the input with 0's in the front.
∊¨⍕¨¨ In each binary number, convert each digit to a string, then join into a string.


05AB1E, 13 bytes


Try it online or verify all test cases.


R             # Reverse the (implicit) input
 ‚            # Pair it with the (implicit) input
  C           # Convert both from a binary-string to an integer
   Ÿ          # Convert this pair to a ranged list
    ¨¦        # Remove the first and last items to make it an exclusive list
      b       # Convert each integer to a binary-string
       Ig     # Push the input-length
         j    # Pad leading spaces to each binary-string to make it that length
          ð0: # Replace every space with a 0
              # (after which the result is output implicitly)

JavaScript (SpiderMonkey), 90 bytes


Try it online!


Perl 5, 84 bytes

sub{($_,$e,@r)=sort@_,"".reverse@_;push@r,$_ while s/01*$/$&=~y|01|10|r/e&&$_<$e;@r}

Try it online!


Ruby, 78 bytes


Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.