A non-negative integer is an odious number when its binary form has an odd count of 1 bits. If the count is even, the number is an evil number. Zero has no 1-bits, so it is evil.
start: n = 7 (111b), count = 0
1) n = 111b & 110b = 110b count = 1
2) n = 110b & 101b = 100b count = 2
3) n = 100b & 011b = 000b count = 3
n == 0 -> stop
count = 3 (odd) => 7 is an odious number
Code
C Examples
Compile with gcc file.c -std=c11 -o out. Pair with Evil Number and Power of 2 for more bit tricks.
Example 1 — Check One Number with Kernighan's Loop
Count set bits one at a time, then test the count for oddness.
C
#include <stdio.h>
static int count_ones(unsigned int n) {
int count = 0;
while (n != 0) {
n &= n - 1;
count++;
}
return count;
}
int main(void) {
unsigned int n;
printf("Enter a non-negative integer: ");
if (scanf("%u", &n) != 1) {
printf("Invalid input\n");
return 1;
}
int ones = count_ones(n);
if (ones % 2 == 1) {
printf("%u has %d one-bits -> odious number\n", n, ones);
} else {
printf("%u has %d one-bits -> evil number\n", n, ones);
}
return 0;
}
Output
Enter a non-negative integer: 7
7 has 3 one-bits -> odious number
How It Works
Unsigned input. Bitwise work is cleanest on unsigned int.
Kernighan loop.n &= n - 1 removes the lowest 1-bit each time.
Count is the answer. The loop runs once per 1-bit.
Parity decides. Odd count means odious.
Example 2 — List Odious Numbers from 1 to 15
Loop over a range and print every value with an odd bit count.
C
#include <stdio.h>
static int count_ones(unsigned int n) {
int count = 0;
while (n != 0) {
n &= n - 1;
count++;
}
return count;
}
int main(void) {
printf("Odious numbers from 1 to 15:\n");
for (unsigned int i = 1; i <= 15; i++) {
if (count_ones(i) % 2 == 1) {
printf("%u ", i);
}
}
printf("\n");
return 0;
}
Output
Odious numbers from 1 to 15:
1 2 4 7 8 11 13 14
How It Works
Range 1 to 15. 0 is skipped because it is evil.
Test each value.count_ones(i) % 2 == 1 keeps the odious ones.
Powers of two. 1, 2, 4, 8 each have exactly one bit, so they are odious.
Result. 8 of the 15 values are odious.
Example 3 — Parity with XOR Folding
You only need the parity, not the full count. XOR-fold a 32-bit value down to one bit.
C
#include <stdint.h>
#include <stdio.h>
static int parity(uint32_t n) {
n ^= n >> 16;
n ^= n >> 8;
n ^= n >> 4;
n ^= n >> 2;
n ^= n >> 1;
return (int)(n & 1u);
}
int main(void) {
uint32_t values[] = { 7u, 10u, 13u };
for (int i = 0; i < 3; i++) {
printf("%u -> %s\n", (unsigned int)values[i],
parity(values[i]) ? "odious" : "evil");
}
return 0;
}
Output
7 -> odious
10 -> evil
13 -> odious
How It Works
Fold the halves. XOR with the value shifted right by 16, 8, 4, 2, then 1.
Parity gathers in bit 0. XOR of all bits equals 1 only when the count of 1-bits is odd.
Mask with 1.n & 1u is the parity.
Fixed width.uint32_t makes the fold sizes exact.
Edge Cases & Pitfalls
Check these before shipping the bit counter.
zero
Zero is evil, not odious
0 has no 1-bits (count 0, which is even). Treat it as evil.
sign
Negative values
Right shifts and bit patterns of negative int values are implementation-defined. Use unsigned.
width
Fixed fold size
The XOR-fold above assumes 32 bits. Use uint64_t and add a >> 32 step for 64-bit values.
format
Printing unsigned
Use %u for unsigned int; %d misprints large values.
Analysis
Performance Notes
Approach
Time
Best for
Shift and test each bit
O(w)
Simplest version
Kernighan loop
O(k)
Few set bits
XOR fold (parity)
O(log w)
Only odd/even needed
Here w is the bit width and k is the number of 1-bits. For interview programs any method is fine; mention Kernighan's trick to show you know bit manipulation.
Remember
Key Takeaways
Definition: an odious number has an odd count of 1-bits.
Opposite: evil numbers have an even count, and 0 is evil.
Technique:n &= n - 1 clears the lowest set bit.
Type: use unsigned for predictable bit behavior.
One line: count the 1-bits; odd means odious, even means evil.
Frequently Asked Questions
A non-negative integer whose binary representation has an odd number of 1 bits, such as 1, 2, 4, 7, 8, 11, 13, and 14.
Evil numbers have an even count of 1-bits (including 0). Odious numbers have an odd count. Together they cover every non-negative integer.
No. 0 has zero 1-bits, which is even, so it is evil.
Subtracting 1 flips the lowest 1-bit and the zeros below it. ANDing with the original clears exactly that lowest 1-bit, so the loop runs once per set bit.
Yes. Every power of two has exactly one 1-bit, and one is odd.
On GCC and Clang you can, but it is not standard C. A portable loop like Kernighan's works everywhere.