C Odious Number Program

Beginner
6 min read
Updated: Nov 2026
3 programs

What Is an Odious Number?

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.

Remember
n is odious  <=>  popcount(n) is odd

7  = 111b    3 ones  -> odious
10 = 1010b   2 ones  -> evil
13 = 1101b   3 ones  -> odious

Odious numbers: 1 2 4 7 8 11 13 14 ...
nBinary1-bitsResult
000Evil
111Odious
3112Evil
71113Odious

How to Check an Odious Number in C

Count the set bits, then test whether the count is odd.

  1. Read a non-negative integer into an unsigned int.
  2. Count 1-bits with Kernighan's trick: n &= n - 1 clears the lowest set bit each pass.
  3. Test the count with count % 2 == 1 (or count & 1).
  4. Print odious when the count is odd, evil when it is even.
  5. Compile with gcc file.c -std=c11 -o out and run.
MethodCostNotes
Shift and mask each bitO(bits)Simple; checks all bit positions
Kernighan n &= n - 1O(set bits)Loops only once per 1-bit
XOR foldingO(log bits)Parity only, no count needed

Pseudocode

Pseudocode
read n (>= 0)
count = 0

while n != 0:
    n = n AND (n - 1)     // clears the lowest 1-bit
    count = count + 1

if count is odd: odious
else:            evil

Cheat Sheet

Quick facts for the odious-number drill.

TopicDetail
DefinitionOdd number of 1-bits in binary
OppositeEvil number (even count, including 0)
Odious up to 151 2 4 7 8 11 13 14
Evil up to 150 3 5 6 9 10 12 15
Clear lowest 1-bitn & (n - 1)
Parity onlyXOR-fold the bits down to one

Worked Walkthrough — n = 7

Count the 1-bits of 7 using n &= n - 1.

Calculation
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

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;
      }

How It Works

  1. Unsigned input. Bitwise work is cleanest on unsigned int.
  2. Kernighan loop. n &= n - 1 removes the lowest 1-bit each time.
  3. Count is the answer. The loop runs once per 1-bit.
  4. 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;
      }

How It Works

  1. Range 1 to 15. 0 is skipped because it is evil.
  2. Test each value. count_ones(i) % 2 == 1 keeps the odious ones.
  3. Powers of two. 1, 2, 4, 8 each have exactly one bit, so they are odious.
  4. 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;
      }

How It Works

  1. Fold the halves. XOR with the value shifted right by 16, 8, 4, 2, then 1.
  2. Parity gathers in bit 0. XOR of all bits equals 1 only when the count of 1-bits is odd.
  3. Mask with 1. n & 1u is the parity.
  4. 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.

Performance Notes

ApproachTimeBest for
Shift and test each bitO(w)Simplest version
Kernighan loopO(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.

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.

Next: Factorial of a Number

Compute n! with a loop.

Factorial 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.

8 people found this page helpful