Check Evil Number in Python

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 2 Code Examples
Popcount parity

What you’ll learn

  • What evil and odious numbers mean in binary.
  • How to count 1 bits and check parity.
  • Single check, range listing, and live preview.

Overview

An evil number has an even number of 1s in binary. If the count is odd, it is called odious.

Two programs

Single value and range [1, 10].

Live preview

Shows popcount and verdict quickly.

Zero case

0 is evil because it has 0 ones.

Prerequisites

Loops, integer division, modulo, and basic binary understanding.

  • Use n % 2 and n // 2 to read binary digits.
  • Optional: bitwise & and bit_count().

What is an evil number?

Count how many 1 digits appear in binary. Even count means evil, odd count means odious.

Example: 15 is 1111, which has four ones, so 15 is evil.

Evilones count even
Odiousones count odd
0evil

Hamming weight

Hamming weight means number of 1-bits. Evil numbers are exactly those with even Hamming weight.

15

15 = 1111, weight is 4 (even), so 15 is evil.

Intuition

15Evil
Binary
1111 (4 ones)
7Odious
Binary
111 (3 ones)

Takeaway: the parity of the count of ones is the only thing that matters.

Live preview

Nonnegative integers only for this definition.

Live result
Press "Classify" to see result.

Algorithm

Goal: check if popcount parity is even.

Count ones

Use bit loop or Python bit_count().

Check count parity

Even count means evil.

📜 Pseudocode

Pseudocode
function is_evil(n):  // n >= 0
    ones = count_ones_in_binary(n)
    return (ones mod 2) == 0
1

Single check with bit_count()

Python has built-in bit_count() for integers.

python
def is_evil(n: int) -> bool:
    if n < 0:
        return False
    return n.bit_count() % 2 == 0


number = 15
if is_evil(number):
    print(f"{number} is an Evil Number.")
else:
    print(f"{number} is not an Evil Number.")

Explanation

15.bit_count() is 4, and 4 is even.

2

Evil numbers in [1, 10]

Range output matches the classic sample.

python
def is_evil_nonneg(num: int) -> bool:
    ones = 0
    while num > 0:
        if num % 2 == 1:
            ones += 1
        num //= 2
    return ones % 2 == 0


print("Evil numbers in the range 1 to 10:")
for i in range(1, 11):
    if is_evil_nonneg(i):
        print(i, end=" ")

Explanation

The loop counts ones in binary by repeatedly dividing by 2.

Optimization

Use bit_count(). It is fast and readable for Python integers.

Kernighan loop. n &= n - 1 removes one set bit per step.

Interview: define nonnegative scope and mention zero case.

❓ FAQ

A nonnegative integer is evil if its binary form has an even number of 1 bits.
If the binary 1-bit count is odd, the number is odious.
Yes. Zero has zero 1-bits, and 0 is even.
15 in binary is 1111, which has 4 ones. Four is even.
Yes, Python integers are arbitrary precision, so bit counting still works correctly.
For one number, bit counting is O(number of bits). For a range, multiply by number of values.

🔄 Input / output examples

Try these values in either method.

nBinaryOnesVerdict
000Evil
3112Evil
71113Odious
1511114Evil

Edge cases and pitfalls

Most definitions use nonnegative integers. Keep that policy clear in your code.

Zero

n = 0

Evil because ones count is 0 (even).

Negative

Policy needed

This page rejects negatives for clarity.

Representation

Binary form

Use canonical nonnegative binary representation.

Range

Include bounds

Use inclusive loops when required by question.

⏱️ Time and space complexity

OperationTimeExtra space
Single number popcountO(bits)O(1)
Kernighan methodO(popcount)O(1)
Range scanO(range * bits)O(1)

Summary

  • Evil means even number of 1-bits.
  • Use bit_count() or manual bit loops.
  • Remember: 0 is evil, negatives need explicit policy.
Did you know?

The opposite of an evil number is an odious number. Evil means an even count of 1 bits; odious means odd.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful