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:
7 → 111 → 3 ones → odious
8 → 1000 → 1 one → odious
15 → 1111 → 4 ones → evil
0 → 0 → 0 ones → evil
n
Binary
Ones
Result
0
0
0
Evil
1
1
1
Odious
7
111
3
Odious
15
1111
4
Evil
Approach
How to Check an Odious Number in C#
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 BitOperations.PopCount for production code.
Method
Idea
Time
Best for
Divide by 2
Read bits with % 2 / / 2
O(bits)
Interviews, teaching binary
BitOperations.PopCount
Built-in popcount, then % 2
O(1)
Clear production C#
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)
Fast check
BitOperations.PopCount((uint)n) % 2 == 1
Binary string
Convert.ToString(n, 2)
Classic odious
1, 2, 4, 7, 8, 11, 13
Classic evil
0, 3, 5, 6, 9, 10, 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 = 7
Binary = 111
Ones = 3 (odd)
Verdict: ODIOUS NUMBER
Trace
Worked Walkthrough — n = 7
Peel bits with % 2, count the ones, then check parity.
Start with the divide-by-2 loop so you can show each bit. Then use BitOperations.PopCount when you want a short production check.
Example 1 — Divide-by-2 Loop
Read each bit with % 2, count the ones, then test whether that count is odd.
C#
using System;
class Program
{
static bool IsOdious(int n)
{
if (n < 0)
{
return false;
}
int ones = 0;
while (n > 0)
{
if (n % 2 == 1)
{
ones++;
}
n /= 2;
}
return ones % 2 == 1;
}
static void Main()
{
int number = 7;
if (IsOdious(number))
{
Console.WriteLine(number + " is an Odious Number.");
}
else
{
Console.WriteLine(number + " is not an Odious Number.");
}
}
}
Output
7 is an Odious 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.
n /= 2 shifts the remaining bits right (integer division).
Test set: 7 and 8 odious; 15 and 0 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.
No. Zero has zero 1-bits, and 0 is even, so 0 is evil.
7 in binary is 111, which has three 1-bits. Three is odd, so 7 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). BitOperations.PopCount is effectively O(1) for fixed-size integers.