An odious number is a nonnegative integer whose binary form has an odd number of 1 bits. That count is called the popcount (Hamming weight). An even popcount means the number is evil.
Remember
n is odious ⇔ (count of 1-bits in binary n) is odd
Examples:
1 → 1 → 1 one → odious
2 → 10 → 1 one → odious
11 → 1011 → 3 ones → odious
15 → 1111 → 4 ones → evil
n
Binary
Ones
Result
1
1
1
Odious
2
10
1
Odious
11
1011
3
Odious
15
1111
4
Evil
Approach
How to Check an Odious Number in Java
Four clear steps — then pick a teaching loop or the built-in popcount.
Require n >= 0 (this page rejects negatives).
Count how many 1 bits appear in binary n.
If that count is odd, n is odious; otherwise it is evil.
Use a divide-by-2 loop to learn the idea, or Integer.bitCount for production code.
Method
Idea
Time
Best for
Divide by 2
Read bits with % 2 / / 2
O(bits)
Interviews, teaching binary
Integer.bitCount
Built-in popcount, then % 2
O(1)
Clear production Java
Pseudocode
Pseudocode
function IsOdious(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) == 1
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Goal
Odd count of binary 1 bits
Opposite
Even popcount → evil
Zero
Not odious (0 ones is even, so 0 is evil)
Fast check
Integer.bitCount(n) % 2 == 1
Binary string
Integer.toBinaryString(n)
Classic odious
1, 2, 4, 7, 8, 11, 13, 14
Classic evil
0, 3, 5, 6, 9, 10, 12, 15
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 = 11
Binary = 1011
Ones = 3 (odd)
Verdict: ODIOUS NUMBER
Trace
Worked Walkthrough — n = 11
Peel bits with % 2, count the ones, then check parity.
Let the JDK count the 1-bits, then check for an odd count in one expression.
Java
class OdiousBitCount
{
static boolean isOdious(int n)
{
if (n < 0)
{
return false;
}
return Integer.bitCount(n) % 2 == 1;
}
public static void main(String[] args)
{
System.out.println(isOdious(11) ? "11 is odious" : "11 is evil");
System.out.println(isOdious(2) ? "2 is odious" : "2 is evil");
System.out.println(isOdious(15) ? "15 is odious" : "15 is evil");
System.out.println(isOdious(0) ? "0 is odious" : "0 is evil");
}
}
Output
11 is odious
2 is odious
15 is evil
0 is evil
How It Works
Reject negatives first so the definition stays nonnegative.
Integer.bitCount(n) returns the number of set bits.
% 2 == 1 means that count is odd → odious.
Same answers as the loop, with less code and no manual bit peeling.
Use the loop in interviews when you must show the process; use this in real Java.
Edge Cases & Pitfalls
Check these before calling the solution done.
Zero
0 is not odious
Zero ones is an even count, so 0 is evil. The odious check must return false.
Odd vs odious
Different parity
n % 2 checks the value. Odious checks the popcount. Example: 2 is even but odious; 3 is odd but evil.
Negatives
Out of scope
Most definitions use nonnegative integers. Return false or reject the input.
Binary string
Count only ones
If you convert to a string, count '1' characters — not the string length.
Analysis
Time and Space Complexity
Program
Time
Extra space
Divide-by-2 loop
O(bits)
O(1)
Integer.bitCount
O(1)
O(1)
For a 32-bit int, the loop runs at most 32 steps. Integer.bitCount is a fixed-width hardware-friendly operation.
Remember
Key Takeaways
Definition: odd popcount → odious; even popcount → evil.
Loop: peel bits with % 2 and / 2, then test ones % 2 == 1.
Built-in:Integer.bitCount(n) % 2 == 1.
Test set: 1, 2, 11 odious; 0 and 15 evil; 2 is even but still odious.
One line: count the 1-bits; if that count is odd, the number is odious.
Frequently Asked Questions
A nonnegative integer is odious if its binary form has an odd number of 1 bits (odd popcount).
If the binary 1-bit count is even, the number is evil — the opposite of odious. Every nonnegative integer is exactly one of the two.
No. Zero has zero 1-bits, and 0 is even, so 0 is evil, not odious.
11 in binary is 1011, which has three 1-bits. Three is odd, so 11 is odious.
Odd/even looks at the value (n % 2). Odious/evil looks at the parity of the count of 1-bits. For example, 2 is even but odious (binary 10 has one 1-bit).
Counting bits by dividing by 2 is O(number of bits). Integer.bitCount is effectively O(1) for fixed-size integers.