C Evil Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is an Evil Number?

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
nBinaryOnesResult
000Evil
3112Evil
71113Odious
1511114Evil

How to Check an Evil Number in C

Four clear steps — then pick a teaching loop or Brian Kernighan’s popcount.

  1. Require n >= 0 (this page rejects negatives).
  2. Count how many 1 bits appear in binary n.
  3. If that count is even, n is evil; otherwise it is odious.
  4. Use a divide-by-2 loop to learn the idea, or Kernighan’s n & (n - 1) trick for a shorter count.
MethodIdeaTimeBest for
Divide by 2Read bits with % 2 / / 2O(bits)Interviews, teaching binary
Brian KernighanClear lowest set bit each stepO(ones)Faster when few 1-bits

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

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
GoalEven count of binary 1 bits
OppositeOdd popcount → odious
ZeroEvil (0 ones is even)
Kernighann = n & (n - 1) clears the lowest 1-bit
Bit testn & 1 or n % 2
Classic evil0, 3, 5, 6, 9, 10, 15
Classic odious1, 2, 4, 7, 8

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

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

C Programs

Start with the divide-by-2 loop so you can show each bit. Then use Brian Kernighan’s popcount when you want a shorter count of set bits.

Example 1 — Divide-by-2 Loop

Read each bit with % 2, count the ones, then test whether that count is even.

C
#include <stdio.h>

int is_evil(int n)
{
    int ones = 0;

    if (n < 0)
    {
        return 0;
    }

    while (n > 0)
    {
        if (n % 2 == 1)
        {
            ones++;
        }
        n /= 2;
    }

    return ones % 2 == 0;
}

int main(void)
{
    int number = 15;
    if (is_evil(number))
        printf("%d is an Evil Number.\n", number);
    else
        printf("%d is not an Evil Number.\n", number);
    return 0;
}

How It Works

  1. Reject negatives so the definition stays on nonnegative integers.
  2. n % 2 is the least significant bit; if it is 1, increment ones.
  3. n /= 2 shifts the remaining bits right (integer division).
  4. Repeat until n becomes 0.
  5. Return whether ones % 2 == 0. For 15, ones = 4 → evil.

Example 2 — Brian Kernighan Popcount

Clear the lowest set bit each step with n & (n - 1), then check parity.

C
#include <stdio.h>

int popcount(unsigned int n)
{
    int ones = 0;
    while (n > 0)
    {
        n &= (n - 1); /* clear lowest set bit */
        ones++;
    }
    return ones;
}

int is_evil(int n)
{
    if (n < 0)
    {
        return 0;
    }
    return popcount((unsigned int)n) % 2 == 0;
}

int main(void)
{
    printf("%s\n", is_evil(15) ? "15 is evil" : "15 is odious");
    printf("%s\n", is_evil(7) ? "7 is evil" : "7 is odious");
    printf("%s\n", is_evil(0) ? "0 is evil" : "0 is odious");
    return 0;
}

How It Works

  1. Cast to unsigned int so the bit tricks stay well-defined for nonnegative values.
  2. n & (n - 1) turns off the lowest 1 bit each iteration.
  3. The loop runs once per set bit — that is the popcount.
  4. % 2 == 0 means that count is even → evil.
  5. Same answers as Example 1; use the divide loop when you must show every bit, Kernighan when set-bit count matters.

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 print bits to a buffer, count '1' characters — not the string length.

Time and Space Complexity

ProgramTimeExtra space
Divide-by-2 loopO(bits)O(1)
Brian KernighanO(ones)O(1)

For a 32-bit int, the divide loop runs at most 32 steps. Kernighan runs once per set bit.

Key Takeaways

  • Definition: even popcount → evil; odd popcount → odious.
  • Loop: peel bits with % 2 and / 2, then test ones % 2.
  • Kernighan: n &= (n - 1) clears one set bit per step.
  • 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). Brian Kernighan’s loop is O(number of 1-bits).

Next: Odious Number

Check numbers with an odd count of 1-bits.

Odious number tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful