Binary/hexadecimal palindromes are integers that are palindromic in both binary and hexadecimal. Unlike binary/decimal palindromes, for example, they have a predictable structure. This means they can be generated directly, rather than searched for. So what is their structure?

Certainly they’re made up of the hexadecimal digits that are themselves palindromic in binary: 0, 6, 9, F; for example, F060F_{16} = 11110000011000001111_{2} and 9F9_{16} = 100111111001_{2}. Each of these four hexadecimal digits maps neatly to a 4-digit binary palindrome, so any hexadecimal palindrome made from them is automatically palindromic in binary.

But there are other binary/hexadecimal palindromes, like 525_{16} = 10100100101_{2} and 70207_{16} = 1110000001000000111_{2}, that contain hexadecimal digits that are *not* palindromic in binary. In this case, binary palindromes are produced with *combinations* of hexadecimal digits. It turns out there are a limited number of valid combinations, and that they’re localized — they span only *two* hexadecimal digits.

In this article, I’ll analyze binary/hexadecimal palindromes and describe their structure — a structure due to the relationship of the two bases, binary and hexadecimal.

## Inferring the Structure of Binary/Hexadecimal Palindromes

Before determining the structure of binary/hexadecimal palindromes analytically, let’s look at more examples. I listed the first 124 (nonzero) binary/hexadecimal palindromes — that is, those palindromes consisting of five hexadecimal digits or less — using my C program that finds multi-base palindromes. I noticed that each palindrome starts (and thus ends) with one of six hexadecimal digits: 1, 3, 5, 7, 9, F. I also noticed that, depending on the starting digit, only certain middle digits appear:

- Palindromes starting with 1: only 0 and 1 appear as middle digits.
- Palindromes starting with 3: only 0 and 3 appear as middle digits.
- Palindromes starting with 5 or 7: only 0, 2, 5, and 7 appear as middle digits.
- Palindromes starting with 9 or F: only 0, 6, 9, and F appear as middle digits.

Within each of the four groups of palindromes, the middle digits alternate in each digit position, in the order listed above.

Here is the output of my program, segmented by starting digit (the highlighted digits in the hexadecimal palindromes are the digits “inserted” into them to take them from n digits to n+1 digits):

Palindromes starting with hex ‘1’:

Hexadecimal | Binary |
---|---|

1_{16} |
1_{2} |

11_{16} |
10001_{2} |

101_{16} |
100000001_{2} |

111_{16} |
100010001_{2} |

1001_{16} |
1000000000001_{2} |

1111_{16} |
1000100010001_{2} |

10001_{16} |
10000000000000001_{2} |

10101_{16} |
10000000100000001_{2} |

11011_{16} |
10001000000010001_{2} |

11111_{16} |
10001000100010001_{2} |

Palindromes starting with ‘3’:

Hexadecimal | Binary |
---|---|

3_{16} |
11_{2} |

33_{16} |
110011_{2} |

303_{16} |
1100000011_{2} |

333_{16} |
1100110011_{2} |

3003_{16} |
11000000000011_{2} |

3333_{16} |
11001100110011_{2} |

30003_{16} |
110000000000000011_{2} |

30303_{16} |
110000001100000011_{2} |

33033_{16} |
110011000000110011_{2} |

33333_{16} |
110011001100110011_{2} |

Palindromes starting with hex ‘5’:

Hexadecimal | Binary |
---|---|

5_{16} |
101_{2} |

55_{16} |
1010101_{2} |

505_{16} |
10100000101_{2} |

525_{16} |
10100100101_{2} |

555_{16} |
10101010101_{2} |

575_{16} |
10101110101_{2} |

5005_{16} |
101000000000101_{2} |

5225_{16} |
101001000100101_{2} |

5555_{16} |
101010101010101_{2} |

5775_{16} |
101011101110101_{2} |

50005_{16} |
1010000000000000101_{2} |

50205_{16} |
1010000001000000101_{2} |

50505_{16} |
1010000010100000101_{2} |

50705_{16} |
1010000011100000101_{2} |

52025_{16} |
1010010000000100101_{2} |

52225_{16} |
1010010001000100101_{2} |

52525_{16} |
1010010010100100101_{2} |

52725_{16} |
1010010011100100101_{2} |

55055_{16} |
1010101000001010101_{2} |

55255_{16} |
1010101001001010101_{2} |

55555_{16} |
1010101010101010101_{2} |

55755_{16} |
1010101011101010101_{2} |

57075_{16} |
1010111000001110101_{2} |

57275_{16} |
1010111001001110101_{2} |

57575_{16} |
1010111010101110101_{2} |

57775_{16} |
1010111011101110101_{2} |

Palindromes starting with hex ‘7’:

Hexadecimal | Binary |
---|---|

7_{16} |
111_{2} |

77_{16} |
1110111_{2} |

707_{16} |
11100000111_{2} |

727_{16} |
11100100111_{2} |

757_{16} |
11101010111_{2} |

777_{16} |
11101110111_{2} |

7007_{16} |
111000000000111_{2} |

7227_{16} |
111001000100111_{2} |

7557_{16} |
111010101010111_{2} |

7777_{16} |
111011101110111_{2} |

70007_{16} |
1110000000000000111_{2} |

70207_{16} |
1110000001000000111_{2} |

70507_{16} |
1110000010100000111_{2} |

70707_{16} |
1110000011100000111_{2} |

72027_{16} |
1110010000000100111_{2} |

72227_{16} |
1110010001000100111_{2} |

72527_{16} |
1110010010100100111_{2} |

72727_{16} |
1110010011100100111_{2} |

75057_{16} |
1110101000001010111_{2} |

75257_{16} |
1110101001001010111_{2} |

75557_{16} |
1110101010101010111_{2} |

75757_{16} |
1110101011101010111_{2} |

77077_{16} |
1110111000001110111_{2} |

77277_{16} |
1110111001001110111_{2} |

77577_{16} |
1110111010101110111_{2} |

77777_{16} |
1110111011101110111_{2} |

Palindromes starting with hex ‘9’:

Hexadecimal | Binary |
---|---|

9_{16} |
1001_{2} |

99_{16} |
10011001_{2} |

909_{16} |
100100001001_{2} |

969_{16} |
100101101001_{2} |

999_{16} |
100110011001_{2} |

9F9_{16} |
100111111001_{2} |

9009_{16} |
1001000000001001_{2} |

9669_{16} |
1001011001101001_{2} |

9999_{16} |
1001100110011001_{2} |

9FF9_{16} |
1001111111111001_{2} |

90009_{16} |
10010000000000001001_{2} |

90609_{16} |
10010000011000001001_{2} |

90909_{16} |
10010000100100001001_{2} |

90F09_{16} |
10010000111100001001_{2} |

96069_{16} |
10010110000001101001_{2} |

96669_{16} |
10010110011001101001_{2} |

96969_{16} |
10010110100101101001_{2} |

96F69_{16} |
10010110111101101001_{2} |

99099_{16} |
10011001000010011001_{2} |

99699_{16} |
10011001011010011001_{2} |

99999_{16} |
10011001100110011001_{2} |

99F99_{16} |
10011001111110011001_{2} |

9F0F9_{16} |
10011111000011111001_{2} |

9F6F9_{16} |
10011111011011111001_{2} |

9F9F9_{16} |
10011111100111111001_{2} |

9FFF9_{16} |
10011111111111111001_{2} |

Palindromes starting with hex ‘F’:

Hexadecimal | Binary |
---|---|

F_{16} |
1111_{2} |

FF_{16} |
11111111_{2} |

F0F_{16} |
111100001111_{2} |

F6F_{16} |
111101101111_{2} |

F9F_{16} |
111110011111_{2} |

FFF_{16} |
111111111111_{2} |

F00F_{16} |
1111000000001111_{2} |

F66F_{16} |
1111011001101111_{2} |

F99F_{16} |
1111100110011111_{2} |

FFFF_{16} |
1111111111111111_{2} |

F000F_{16} |
11110000000000001111_{2} |

F060F_{16} |
11110000011000001111_{2} |

F090F_{16} |
11110000100100001111_{2} |

F0F0F_{16} |
11110000111100001111_{2} |

F606F_{16} |
11110110000001101111_{2} |

F666F_{16} |
11110110011001101111_{2} |

F696F_{16} |
11110110100101101111_{2} |

F6F6F_{16} |
11110110111101101111_{2} |

F909F_{16} |
11111001000010011111_{2} |

F969F_{16} |
11111001011010011111_{2} |

F999F_{16} |
11111001100110011111_{2} |

F9F9F_{16} |
11111001111110011111_{2} |

FF0FF_{16} |
11111111000011111111_{2} |

FF6FF_{16} |
11111111011011111111_{2} |

FF9FF_{16} |
11111111100111111111_{2} |

FFFFF_{16} |
11111111111111111111_{2} |

You can see that binary palindromes starting with hex digits 1, 3, 5, and 7 are not multiples of four bits. This is because leading zeros do not count. Each of those four hex digits — when used as a starting digit — requires less than four bits. In this case, it’s important to remember that hex digits are formed in groups of four bits, *from right to left*.

## Starting Digits

We’ve seen empirically that a **nonzero binary/hexadecimal palindrome starts with the hexadecimal digit 1, 3, 5, 7, 9, or F** — now it’s time to prove it.

First, observe that the starting hex digit must be odd: an even hex digit ends with a 0 when written in binary, and palindromes can’t start with 0. This leaves eight starting digits as candidates: 1, 3, 5, 7, 9, B, D, F.

Let’s classify our palindromes into two categories: single hex digit and multiple hex digit. A single hex digit palindrome is a single hex digit that is palindromic in binary: 1, 3, 5, 7, 9, and F are the only choices (1_{16} = 1_{2}, 3_{16} = 11_{2}, 5_{16} = 101_{2}, 7_{16} = 111_{2}, 9_{16} = 1001_{2}, F_{16} = 1111_{2}). A multiple hex digit palindrome *as a whole* is palindromic in binary, but individually, each hex digit may not be — and in fact, *will* not be — palindromic in binary.

Let’s analyze multiple hex digit palindromes by classifying them into two categories, depending on whether their ending hex digit has a binary equivalent that starts with a 1 or 0:

**Binary equivalent starts with 1**.The hexadecimal palindrome will have an associated binary palindrome that is a multiple of four bits. This means that each hex digit must stand alone as a binary palindrome: 9 and F are the only choices for the starting digit. There are two 2-digit palindromes that start with 9 and F: 99

_{16}= 10011001_{2}and FF_{16}= 11111111_{2}; these will serve as “bookends” for all palindromes of 3 digits or more.**Binary equivalent starts with 0**.The hexadecimal palindrome will have an associated binary palindrome that is

*not*a multiple of four bits. This means that each hex digit does*not*stand alone as a binary palindrome.The best way to show which starting digits are allowed in this case is to try all four candidates as 2-digit palindromes: 11

_{16}= 10001_{2}, 33_{16}= 110011_{2}, 55_{16}= 1010101_{2}, and 77_{16}= 1110111_{2}. All are binary palindromes, so 1, 3, 5, 7 are the valid starting digits.

## Middle Digits

We’ve seen empirically what the middle digits of multiple digit binary/hexadecimal palindromes are, and that they follow a pattern — now I’ll show why.

**Definition**. I’ll say a number is a * leading-zero palindrome* or is

*if it is palindromic with zero or more leading zeros.*

**leading-zero palindromic**### Middle Digits for Starting Digits 9, F

Palindromes starting with 9 or F are straightforward to analyze, so I’ll start with them. Each middle hex digit must stand alone as a leading-zero binary palindrome, so 0, 6, 9, and F are the choices.

The structure of these palindromes is easy to describe. The n+1 hex digit palindromes can be created from the n hex digit palindromes by inserting the digits 0, 6, 9, or F. When n is odd, one n+1 digit palindrome is spawned from each n digit palindrome, by replicating the middle hex digit (which will be 0, 6, 9 or F). When n is even, four n+1 digit palindromes are spawned from each n digit palindrome, by inserting the digits 0, 6, 9, and F in turn.

### Middle Digits for Starting Digits 1, 3, 5, 7

The analysis of palindromes starting with 1, 3, 5, or 7 is based on the number of leading zeros in the binary representation of the ending hex digit; let’s define the three cases:

- 1 leading zero: 5 (0101) and 7 (0111).
- 2 leading zeros: 3 (0011).
- 3 leading zeros: 1 (0001).

Recall how palindrome generation works: each inserted hex digit takes an n digit palindrome and turns it into an n+1 digit palindrome. To ensure the result remains a binary palindrome, the inserted hex digit must maintain a binary palindrome around the “center”. Here’s how it’s done:

- The binary representation of the inserted hex digit must have the same number of leading zeros as the binary representation of the hex digit to its right.
- The binary representation of the inserted hex digit must be palindromic without the leading zeros.

In other words, the inserted leading zeros mirror the existing leading zeros, and the rest of the hex digit — which is between the sets of zeros — stands alone as a binary palindrome.

(There’s an alternate way to think of this: the inserted hex digit, with the one, two, or three leading binary zeros in the hex digit to its right, is a leading zero palindrome. The left side of this palindrome replaces the leading zeros used from the right.)

So what are the allowable middle digits? Hex digits 0-7 are the candidates, since they’re the only 4-bit hex digits that have leading zeros in their binary representations. But after you strip away the required number of leading zeros, only 0, 1, 2, 3, 5, and 7 stand alone as binary palindromes:

- 1 leading zero: 0 (000), 2 (010), 5 (101), 7 (111).
- 2 leading zeros: 0 (00) or 3 (11).
- 3 leading zeros: 0 (0) or 1 (1).

The insertion of middle digits can continue indefinitely, since the number of binary leading zeros in each group is the same for each digit. Any middle digit in each group can be adjacent to any other. The number of leading zeros in the ending digit is the number of leading zeros maintained throughout.

#### Example

This diagram shows an example of binary/hexadecimal palindrome generation:

## Summary

In summary, the (nonzero) binary/hexadecimal palindromes are

- The single digit hexadecimal palindromes 1, 3, 5, 7, 9, and F.
- All hexadecimal palindromes that start and end with 1 and have only 0s and 1s between.
- All hexadecimal palindromes that start and end with 3 and have only 0s and 3s between.
- All hexadecimal palindromes that start and end with 5 or 7 and have only 0s, 2s, 5s, and 7s between.
- All hexadecimal palindromes that start and end with 9 or F and have only 0s, 6s, 9s, and Fs between.

(And now that we know their structure we can count them — see my article “Counting Binary/Hexadecimal Palindromes”.)

(Cookies must be enabled to leave a comment...it reduces spam.)