Check Evil Number in Java

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

What you’ll learn

  • The definition of an evil number: even count of 1 bits in binary.
  • A bitwise method and a division-by-two method in Java.
  • Live preview, edge cases (0, negatives), and interview tips.

Prerequisites

You only need basic Java loops and binary-bit intuition.

  • Binary numbers and set-bit counting (popcount).
  • Java loops plus bitwise or division-based digit extraction.
  • Understanding parity (even/odd count).

Math definition

A nonnegative integer n is evil iff popcount(n) % 2 == 0. If odd, the number is odious.

Intuition and examples

Convert to binary and count 1-bits. Numbers like 3 (11), 5 (101), and 15 (1111) are evil because their count of ones is even.

What is an evil number?

A nonnegative integer n is evil if its binary representation contains an even number of 1 bits. Otherwise it is odious.

Examples: 0, 3, 5, 6, 9, 10, 12, 15 are evil.

Live preview

Nonnegative integers in JavaScript safe range. Same logic as Java examples.

Try 0, 7, or 10.

Live result
Press "Classify" to see result.

Algorithm

Goal: decide if n has even popcount.

1

Count 1 bits

Use loop + n & 1 and right shift, or use Integer.bitCount(n).

2

Check parity

If count % 2 == 0, it is evil; otherwise odious.

📜 Pseudocode

Pseudocode
function popcount(n):  // n >= 0
    c = 0
    while n > 0:
        c += (n mod 2)
        n = floor(n / 2)
    return c

function isEvil(n):
    return (popcount(n) mod 2) == 0
1

Bitwise popcount (single value)

Checks 15 using manual bit counting in Java.

java
public class Main {
    static int countSetBits(int n) {
        int count = 0;
        int x = n;
        while (x != 0) {
            count += (x & 1);
            x >>>= 1;
        }
        return count;
    }

    static boolean isEvil(int n) {
        if (n < 0) return false;
        return countSetBits(n) % 2 == 0;
    }

    public static void main(String[] args) {
        int number = 15;
        if (isEvil(number)) {
            System.out.println(number + " is an Evil Number.");
        } else {
            System.out.println(number + " is not an Evil Number.");
        }
    }
}
📤 Output
15 is an Evil Number.
2

Evil numbers in [1, 10]

Prints 3 5 6 9 10 using division-by-two bit extraction.

java
public class Main {
    static boolean isEvilNonNegative(int num) {
        int ones = 0;
        int x = num;
        while (x > 0) {
            if (x % 2 == 1) ones++;
            x /= 2;
        }
        return ones % 2 == 0;
    }

    public static void main(String[] args) {
        System.out.println("Evil numbers in the range 1 to 10:");
        for (int i = 1; i <= 10; i++) {
            if (isEvilNonNegative(i)) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }
}
📤 Output
Evil numbers in the range 1 to 10:
3 5 6 9 10

⏱️ Time and space complexity

OperationTimeExtra space
Popcount of nO(log n)O(1)
Scan [a,b]O((b-a+1) log U)O(1)

Optimization tips

Built-in: use Integer.bitCount(n) when allowed.

Manual: bitwise shifting is usually faster than converting to a string.

Input scope: reject negatives up front when definition is nonnegative only.

Input/output examples

Input nOutput
15Evil number
7Odious / not evil
0Evil number

Edge cases

n = 0

Zero case

0 is evil because popcount is 0, and 0 is even.

n < 0

Negative inputs

Usually excluded by the nonnegative definition used in interview problems.

Large n

Range limits

Stay within Java integer limits unless you intentionally switch to long.

Summary

  • Evil numbers have an even count of 1 bits in binary.
  • In Java, use manual bit counting or Integer.bitCount.
  • Handle 0 clearly and define whether negatives are allowed.

❓ FAQ

A nonnegative integer n is evil if its base-2 representation contains an even number of 1 bits (even popcount).
If the count of 1 bits is odd, the number is odious. Every nonnegative integer is either evil or odious.
Yes. 0 has zero 1 bits, and zero is even.
15 in binary is 1111. It has four 1 bits, and four is even.
Yes. Integer.bitCount(n) (or Long.bitCount(n)) gives popcount directly.
Bit counting for one value costs O(log n) bit-steps. Scanning a range is O((b-a+1) log U).
Did you know?

The complementary class is odious numbers: nonnegative integers whose binary representation has an odd number of 1 bits. The names are mathematical wordplay.

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