## Definition of Evil Number

Evil number, in its’ binary representation, has even number of bits in switched-on condition or true condition or set to value 1. A bit can have value either 1 or 0 / on or off / true or false conditions. Evil number is an integer and it has positive value only. Zero values in evil number follow Thue-Morse order and that is the reason they are known as Thue-Morse set. Non-evil characters are known as odious numbers though odious has a negative meaning. In computer parlance property of evil number is called as even parity.

### Logic behind an Evil number

Numbers are stored in computer memory in bits. In a byte of memory there are 8 bits. We can store 256 values of 0-255 in one byte. Small numbers up to 255 can be in one byte, numbers up to 65535 can be stored in 2 bytes (word) and higher valued numbers can be stored in long word (4 Bytes), quad word (8 Bytes).

**Bit representation**

The table below explains a bit map of various numbers and how they are graded as evil number or otherwise.

Number | Bit Map | Remarks |

0 | 00000000 | 0 is an evil number. Sum of binary is 0 and 0 is considered to be even (since it does not leave any remainder when divided by 2) |

1 | 00000001 | 1 is not an evil number. Sum of binary is 1 and it is an odd |

3 | 00000011 | 3 is an evil number. Sum of binary is 2 which is even |

4 | 00000100 | Count of bits is 1 and hence 4 is not an evil number. |

5 | 00000101 | 5 is an evil number |

22 | 00010110 | Bit sum is 3. Hence 22 is not evil number |

39 | 00100111 | 39 is and evil number since the bit sum is even. |

168 | 10101000 | It is not an evil number as the bit count is odd. |

245 | 11110101 | Sigma of bits 6 an even number Hence. 245 is an evil number |

### How to check Evil number?

The steps to compute whether a given number is an evil number or not are as follows.

**Accept the number**

Determine the number of significant binary positions to be checked for bit map settings.

For example – If the number is 22, bit map is 10110, 5-bit positions need to be checked. For 245, bit map 11110101, 8 bit positions need to be checked.

• Iterate the program for that many times as the no of binary positions and check the binary value for each position.

• The binary value of each position can be computed thru remainder by dividing the scaled-down value of the number by 2

o For number 245 the first binary position ( 20 ) by dividing the number 2 and remainder gives the result.

o In the next iteration scale down the number by half i.e. number = number/2

o Second binary position (21) can be found out by computing the remainder by dividing the scaled-down value of the number by 2

o By repeating the above 2 steps for rest of the binary positions, the binary values for the entire bit map can be computed.

• Add the binary values of all the positions

• If the added value is even, then the number is evil otherwise it is an odious number

4.8 (13,296 ratings)

View Course

### Example (Python Program)

**Code:**

`# Python Program to check whether a given number is Evil number or not`

num =int(input("Number please: ")) #Accept the number

terminate = "N" #Define program variables

y = 0

# Loop to find no of binary

while terminate == "N": # positions

if num%(2**y)==num: #If 2**position > num terminate

terminate = "Y" # (position starts with 0)

break

y = y +1

remctr =0 # Define bit count

for i in range (0, y): # Loop for no of position times

num1 = int(num/2**i) # scaling down the number from

# second iteration onwards

if num1%2 == 1: # Remainder by dividing by 2

remctr = remctr + 1 # Total binary counter

if remctr%2 == 0: #inferring the counter

print (f"The Given number {num} is Evil")

else:

print (f"The Given number {num} is Odious")

**Result is:**

**Example (C++ Program)**

The logic is same as that of Python.

**Code:**

`#include <iostream>`

#include <math.h>

using namespace std;

int main()

{

int num;

cout<<"Enter the number:";

cin>>num;

string terminate = "N";

int y = 0;

int num1 = 0;

int num2 = 0;

while (terminate == "N") // Loop to find out no of binary positions

{

num1 = pow(2,y);

if (num%num1==num)

{

terminate = "Y";

break;

}

y = y +1;

}

int remctr = 0;

for (int i=0; i<y; i++) // Iterate for binary position times from 0

{

num1 = pow(2,i);

num2 = int(num/num1);

if (num2%2 == 1)

{

remctr = remctr + 1; // Accumulate binary counter

}

}

// infer the binary counter to get results

if ((remctr%2) == 0)

cout << "The given number " << num << " is evil ";

else

cout << " The given number " << num << " is Odious";

}

**Output:**

### Conclusion

Methods of finding out whether a given number is evil number or not, were explained in this article with examples from Python and C++. This concept is used in number theory and data science to study the behaviour of numbers and applicate them in games and statistics scenarios.

### Recommended Articles

This is a guide to Evil Number. Here we discuss the definition, logic behind evil number, How to check Evil number? examples with code implementation. You may also have a look at the following articles to learn more –