An evil number is a nonnegative integer whose binary form has an even number of 1 bits. That count is called the popcount (Hamming weight). An odd popcount means the number is odious.
Remember
n is evil ⇔ (count of 1-bits in binary n) is even
Examples:
15 → 1111 → 4 ones → evil
7 → 111 → 3 ones → odious
0 → 0 → 0 ones → evil
3 → 11 → 2 ones → evil
n
Binary
Ones
Result
0
0
0
Evil
3
11
2
Evil
7
111
3
Odious
15
1111
4
Evil
Approach
How to Check an Evil Number in Python
Four clear steps — then pick a teaching loop or a short bin().count check.
Require n >= 0 (this page rejects negatives).
Count how many 1 bits appear in binary n.
If that count is even, n is evil; otherwise it is odious.
Use a divide-by-2 loop to learn the idea, or bin(n).count("1") for a short Python check.
Method
Idea
Time
Best for
Divide by 2
Read bits with % 2 / // 2
O(bits)
Interviews, teaching binary
bin + count
Count "1" in the binary string
O(bits)
Clear production Python
Pseudocode
Pseudocode
function IsEvil(n):
if n < 0:
return false
ones = 0
while n > 0:
if (n mod 2) == 1:
ones = ones + 1
n = n / 2
return (ones mod 2) == 0
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Goal
Even count of binary 1 bits
Opposite
Odd popcount → odious
Zero
Evil (0 ones is even)
Fast check
bin(n).count("1") % 2 == 0
Binary string
bin(n)[2:] or format(n, "b")
Classic evil
0, 3, 5, 6, 9, 10, 15
Classic odious
1, 2, 4, 7, 8
Try it
Live Preview
Enter a nonnegative integer to see its binary form, popcount, and verdict.
Nonnegative whole numbers only. Tap a chip or type a value.
Result
n = 15
Binary = 1111
Ones = 4 (even)
Verdict: EVIL NUMBER
Trace
Worked Walkthrough — n = 15
Peel bits with % 2, count the ones, then check parity.
Calculation
15 in binary = 1111
Step 1: 15 % 2 = 1 → ones = 1; n = 7
Step 2: 7 % 2 = 1 → ones = 2; n = 3
Step 3: 3 % 2 = 1 → ones = 3; n = 1
Step 4: 1 % 2 = 1 → ones = 4; n = 0
ones = 4 → even → 15 is evil
Code
Python Programs
Start with the divide-by-2 loop so you can show each bit. Then use bin().count when you want a short Python check.
Example 1 — Divide-by-2 Loop
Read each bit with % 2, count the ones, then test whether that count is even.
python
def is_evil(n: int) -> bool:
if n < 0:
return False
ones = 0
while n > 0:
if n % 2 == 1:
ones += 1
n //= 2
return ones % 2 == 0
number = 15
if is_evil(number):
print(f"{number} is an Evil Number.")
else:
print(f"{number} is not an Evil Number.")
Output
15 is an Evil Number.
How It Works
Reject negatives so the definition stays on nonnegative integers.
n % 2 is the least significant bit; if it is 1, increment ones.
Convert to a binary string, count the "1" characters, then check parity.
python
def is_evil(n: int) -> bool:
if n < 0:
return False
return bin(n).count("1") % 2 == 0
# or: return format(n, "b").count("1") % 2 == 0
print("15 is evil" if is_evil(15) else "15 is odious")
print("7 is evil" if is_evil(7) else "7 is odious")
print("0 is evil" if is_evil(0) else "0 is odious")
Output
15 is evil
7 is odious
0 is evil
How It Works
Reject negatives first so the definition stays nonnegative.
bin(n) builds a string like "0b1111".
.count("1") returns the number of set bits (the 0b prefix has no ones).
% 2 == 0 means that count is even → evil.
Same answers as the loop, with less code. Use the loop in interviews when you must show the process.
Edge Cases & Pitfalls
Check these before calling the solution done.
Zero
0 is evil
Zero ones is an even count. Do not treat 0 as a special false case.
Even vs evil
Different parity
n % 2 checks the value. Evil checks the popcount. Example: 2 is even but odious.
Negatives
Out of scope
Most definitions use nonnegative integers. Return False or reject the input.
Binary string
Count only ones
If you convert with bin, count "1" characters — not the string length.
Analysis
Time and Space Complexity
Program
Time
Extra space
Divide-by-2 loop
O(bits)
O(1)
bin + count
O(bits)
O(bits) for the string
For a typical fixed-width integer, the loop runs in a small number of steps. The string method builds a short binary text, then counts ones.
Remember
Key Takeaways
Definition: even popcount → evil; odd popcount → odious.
Loop: peel bits with % 2 and // 2, then test ones % 2.
Short Python:bin(n).count("1") % 2 == 0.
Test set: 15 and 0 evil; 7 odious; 2 is even but still odious.
One line: count the 1-bits; if that count is even, the number is evil.
Frequently Asked Questions
A nonnegative integer is evil if its binary form has an even number of 1 bits (even popcount).
If the binary 1-bit count is odd, the number is odious — the opposite of evil.
Yes. Zero has zero 1-bits, and 0 is even.
15 in binary is 1111, which has four 1-bits. Four is even, so 15 is evil.
Even/odd looks at the value (n % 2). Evil/odious looks at the parity of the count of 1-bits.
Counting bits by dividing by 2 is O(number of bits). Using bin(n).count('1') is also O(bits) for typical integers.