Evil Number Calculator

Binary

is 23 an evil number?Yes - its binary form has 4 ones, an even count
binary form of n10111
number of 1 bits4
the complementary classodious numbers - an odd count of 1 bits
How this was calculated
  1. Write 23 in binary: 10111.
  2. Count the 1 bits: there are 4, and the parity of that count is the whole test.
  3. An even count makes the number evil; the complementary odd-count numbers are called odious.
  4. Evil and odious numbers each take exactly half of any complete block of consecutive integers, so the classes stay evenly matched forever.

Write a number in binary and count its 1 bits. If the count is even, the number is evil; if odd, it is odious - a piece of terminology that is playful in name and serious in content, because the evil/odious classification is exactly the Thue-Morse sequence in disguise. The anchors: 3 = 11 in binary has two 1 bits, so 3 is evil; 5 = 101 in binary also has two, so 5 is evil; 6 = 110 in binary has two as well - also evil. And 7 = 111 has three, making 7 odious.

This calculator reports the binary form, the popcount and the parity verdict for your number, and in list mode prints the evil numbers up to your limit - the first twelve are 0, 3, 5, 6, 9, 10, 12, 15, 17, 18, 20 and 23. Note that 0 is evil: it has zero 1 bits, and zero is even.

The parity of the popcount sounds like a curiosity until it is iterated: the sequence of parities, read off for 0, 1, 2, 3, ..., is the Thue-Morse sequence, one of the most studied objects in combinatorics on words - cube-free, overlap-free, and connected to everything from chess draws to fractal geometry.

Worked examples

Is 23 evil?

(0 - test a single number · 23)

is 23 an evil number?Yes - its binary form has 4 ones, an even count
binary form of n10111
number of 1 bits4
the complementary classodious numbers - an odd count of 1 bits

Is 7 evil?

(0 - test a single number · 7)

is 7 an evil number?No - its binary form has 3 ones, an odd count, so it is odious
binary form of n111
number of 1 bits3
the complementary classodious numbers - an odd count of 1 bits

Evil numbers up to 30

(1 - list up to a limit · 30)

evil numbers up to 300, 3, 5, 6, 9, 10, 12, 15, 17, 18, 20, 23, 24, 27, 29, 30
how many there are (0 included)16
largest of them30
the complementary classodious numbers - an odd count of 1 bits

Popcount parity

The test has three steps: expand n in binary, count the 1s (the popcount, or Hamming weight), and read the parity. Even popcount: evil. Odd popcount: odious. The calculator shows all three steps, because the binary expansion is where the intuition lives: 23 = 10111 has four 1 bits - evil; its neighbour 22 = 10110 has three - odious. Adjacent numbers routinely land in opposite classes, since incrementing flips a trailing block of bits.

A few anchor cases fix the pattern: 0 is evil (popcount 0), 1 is odious (popcount 1), 2 = 10 is odious (popcount 1), 3 = 11 is evil (popcount 2), and powers of two are always odious (popcount exactly 1). Numbers one less than a power of two, like 7, 15, 31, have popcount equal to their bit length, so their class alternates with length: 7 is odious (3 bits), 15 is evil (4 bits), 31 is odious (5 bits).

  • Evil: even number of 1 bits; odious: odd
  • 0 is evil (zero 1 bits); powers of two are odious (one 1 bit)
  • First evil numbers: 0, 3, 5, 6, 9, 10, 12, 15, 17, 18, 20, 23
  • 23 = 10111 (four 1s) is evil; 22 = 10110 (three 1s) is odious

The Thue-Morse sequence

Read the classes in order - E for evil, O for odious - starting from 0: E, O, O, E, O, E, E, O, ... Replace E by 0 and O by 1 and you have the Thue-Morse sequence: 0, 1, 1, 0, 1, 0, 0, 1, ... The t(n) term of Thue-Morse is exactly the parity of the popcount of n, which is this page's computation. The sequence is generated by starting with 0 and repeatedly appending the complement of everything so far: 0 -> 01 -> 0110 -> 01101001.

Thue-Morse is famous for what it avoids: it contains no cube (no block repeated three times consecutively) and no overlap, properties Axel Thue proved in 1906-1912 in the founding papers of combinatorics on words. That a bit-parity test generates a sequence with such rigid structure is the first hint that 'evil' numbers are not a joke topic.

  • Class sequence from 0: evil, odious, odious, evil, ... = Thue-Morse
  • Generated by appending complements: 0, 01, 0110, 01101001
  • Thue-Morse is cube-free and overlap-free (Thue, 1906)

Equal numbers of each - and where they differ

In any complete block of 2^k consecutive integers starting at 0, exactly half are evil and half odious - the Thue-Morse self-similarity forces the balance. The calculator's list mode up to 100 shows the running counts staying close throughout, never drifting apart by more than a few. This balance is why the classification is useful as a fair splitter: assigning items by popcount parity divides any long initial segment almost perfectly evenly.

The balance breaks in weighted questions - sum the evil numbers up to x versus the odious ones and the comparison becomes a genuinely studied problem (the sums differ by amounts connected to the sequence's fractal structure). For the plain count, though, fairness is exact at powers of two, and the calculator's counts let you watch it happen.

nBinaryPopcountClass
3112Evil
51012Evil
61102Evil
71113Odious
1511114Evil
23101114Evil

Why 'evil' and 'odious'?

The names are a pun with a pedigree: 'evil' from 'even' with a twist, 'odious' from 'odd' - the terminology is generally credited to the playful tradition catalogued by Berlekamp, Conway and Guy in Winning Ways. The pun stuck because the underlying dichotomy kept turning up: in the Thue-Morse sequence, in the ruler function, in the parity of permutation inversions counted by bits, and in the analysis of the Tower of Hanoi's optimal solution, whose move parities follow the same pattern.

Names aside, the computation on this page - popcount parity - is a workhorse primitive in computer science, where it appears as the parity bit, the Hamming weight instruction, and the core of the bitwise tricks in chess programming. The calculator's per-number verdict is one instruction's worth of work; the mathematics it touches is a century deep.

The classification also partitions familiar families cleanly. Among the powers of two, all are odious (a single 1 bit). Among the Mersenne numbers 2^k - 1, whose binary forms are solid blocks of k ones, the class alternates with k: 7 (k = 3) is odious, 15 (k = 4) is evil, 31 (k = 5) is odious. Among factorials and other structured sequences the classes mix without an obvious pattern - which is itself the Thue-Morse signature: rigidly balanced in aggregate, patternless to local inspection, and generated by a rule a child can apply.

Quick calibration: 0 evil, 1 odious, 2 odious, 3 evil, 15 evil, 31 odious. If your intuition said 0 should be neither, note the definition counts 1 bits - zero of them is an even count.

Computing popcount parity fast

The parity test has a classical bit trick: XOR the number with itself shifted - repeatedly folding n ^= n >> 1, n ^= n >> 2, n ^= n >> 4 and so on - leaves the parity bit in the lowest position, because XOR addition is addition without carry, and parity is exactly the sum of bits modulo 2. Modern processors expose popcount as a single instruction, and its lowest bit is the answer this page reports. The calculator's step display shows the binary expansion and the count, but the underlying computation is that one instruction's worth of work.

The same parity function is the error-detection bit of classical computing: a parity bit appended to a byte is chosen to make the total popcount even - the 'evil' convention - so single-bit errors flip the class and get caught. The playful name sits on top of a mechanism that has guarded data for seventy years.

  • Parity = popcount mod 2 - one instruction on modern hardware
  • Folding by XOR-shifts computes it without counting
  • Parity bits in data transmission enforce the even (evil) class

Evil and odious in sequences

Because the classification is Thue-Morse, the evil numbers inherit its rigidities. Thue-Morse never has three equal consecutive terms, so no three consecutive integers are all evil or all odious - and the gaps between consecutive evil numbers, which run 3, 2, 1, 3, 1, 2, 3 in the opening list (0, 3, 5, 6, 9, 10, 12, 15), never exceed 3. The odious sequence obeys the same bound by symmetry.

The two sequences also interlace perfectly with the ruler-like structure of binary carries: incrementing a number flips its class according to the number of trailing 1s - one trailing 1 pattern decides whether the class toggles. Readers who enjoy the OEIS will find the evil numbers as the positions of 0 in Thue-Morse and the odious numbers as the positions of 1 - two of the most cross-referenced sequences in the encyclopedia.

  • Gaps between consecutive evil numbers are at most 3
  • Four consecutive integers are never all odious (or all evil)
  • Evil = positions of 0 in Thue-Morse; odious = positions of 1

FAQs

What is an evil number?

A non-negative integer whose binary expansion has an even number of 1 bits. 3 = 11 (two 1s), 5 = 101 (two 1s) and 15 = 1111 (four 1s) are evil; 0 is evil too, with zero 1 bits.

What is an odious number?

The complement class: an odd number of 1 bits. 7 = 111 (three 1s) and every power of two (exactly one 1 bit) are odious.

Is 0 an evil number?

Yes. 0 in binary has zero 1 bits, and zero is even, so 0 opens the evil sequence: 0, 3, 5, 6, 9, 10, 12, 15, ...

How are evil numbers connected to the Thue-Morse sequence?

The parity of the popcount of n is the n-th term of the Thue-Morse sequence: even parity (evil) maps to 0 and odd parity (odious) to 1, giving 0, 1, 1, 0, 1, 0, 0, 1, ...

Are there equally many evil and odious numbers?

In any block of 2^k consecutive integers from 0, exactly half are evil and half are odious - a consequence of the Thue-Morse sequence's complement-appending construction.

Is 23 evil or odious?

Evil: 23 = 10111 in binary has four 1 bits, an even count. Its neighbour 22 = 10110 has three 1 bits and is odious.

Sources

Related calculators

Binary Converter Calculator
Convert decimal numbers to binary, octal and hexadecimal — or read binary and octal numbers back into decimal — with the place-value working shown.
Absolute Value Calculator
Absolute values and the distance between two numbers on the number line: |a|, |b| and |a - b|, with the sign-stripping logic spelled out.
Abundancy Index Calculator
Compute the abundancy index sigma(n)/n of a number, classify it as deficient, perfect or abundant, and test whether two numbers are friendly.