Evil Numbers In C++

Any non-negative integer that, in its binary form, contains an even number of 1s is known as an evil number. For example, because it contains two 1's, 9 (binary: 1001) is an evil number. Evil numbers are very popular for practicing binary manipulation and bitwise operators in C++ programming. In order to check if a number is evil, it has to be converted to binary; after counting the ones, it should be verified that the count was even. The understanding of this concept by a programmer can help better understand binary arithmetic, bitwise operator , and efficient algorithms for binary number analysis. Evil numbers are a simple yet fun way of introducing mathematical concepts into programming.

Example:

  • 9 is an evil number since its binary form, 1001, contains two ones (even).
  • 5 is also an inadequate number because it has two ones (even) in its binary form, 101.
  • Seven is not allowed as it is odd in binary, 111 contains three ones.

In C++ , finding out if a number is evil involves:

  • The number is converted into its binary form.
  • The binary representation is calculated for 1 count.
  • Checking the number for equality.

The following is an outline of a simple C++ program :

  • Use bitwise operations to determine how many 1s there are in the binary form of the integer.
  • Verify that the number is even before printing it to check for evil.
  • Why to learn evil numbers?

  • Examples of binary operations include learning bitwise operations and comprehending binary representations of integers.
  • Applying mathematical concepts to logical reasoning is known as computational logic.
  • One way to solve issues is to come up with new ways to categorize numbers.
  • Example 1:

Let us take an example to illustrate the Evil Numbers in C++.

Example

#include <iostream>
using namespace std;
// Function to count the number of 1s in the binary representation of a number
int countOnes(int n) {
    int count = 0;
    while (n > 0) {
        count += (n & 1); // Check if the last bit is 1
        n >>= 1;          // Right shift the number to process the next bit
    }
    return count;
}
// Function to check if a number is an Evil number
bool isEvilNumber(int n) {
    int ones = countOnes(n);
    return (ones % 2 == 0); // Check if the count of 1s is even
}
int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;

    if (isEvilNumber(num)) {
        cout << num << " is an Evil number." << endl;
    } else {
        cout << num << " is not an Evil number." << endl;
    }
    return 0;
}

Output:

Output

Enter a number: 9
9 is an Evil number.
Enter a number: 23
23 is an Evil number.
Enter a number: 35
35 is not an Evil number.

Explanation:

  • In this example, the countOnes function uses bitwise & and right shift (>>) operators to count the number of 1s in the binary representation.
  • The isEvilNumber function checks to see if the count of 1s is even.
  • The main function takes user input and determines whether the number is Evil.
  • Example 2:

Let us take another example to illustrate the Evil Numbers in C++.

Example

#include <iostream>
using namespace std;

// Function to check if a number is an Evil number
bool isEvilNumber(int n) {
    int ones = __builtin_popcount(n); // Counts the number of 1s in the binary representation
    return (ones % 2 == 0);           // Check if the count of 1s is even
}

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;

    if (isEvilNumber(num)) {
        cout << num << " is an Evil number." << endl;
    } else {
        cout << num << " is not an Evil number." << endl;
    }

    return 0;
}

Output:

Output

Enter a number: 56
56 is not an Evil number.

Explanation:

  • _builtinpopcount(n) function : In the binary representation of n, the built-in GCC/Clang function _builtinpopcount(n) counts the number of 1s directly. The procedure is made simpler and the need to manually apply the logic to count 1s is eliminated.
  • Logic: The program checks to see if the number of 1s is even by using the result of _builtinpopcount.
  • Benefits:

  • Efficiency: It is optimized and quicker than manually iterating through bits is the _builtinpopcount function.
  • Conciseness: The code is shorter and easier to comprehend when it is concise because it eliminates the need for extra helper functions.
  • Conclusion:

In conclusion, Evil numbers add interest to learning the properties of binary numbers and their computer applications. Because its binary representation is composed of an even number of 1s, and it offers a simple yet engaging exercise for learning bitwise operations and binary arithmetic. Mastery of bitwise operations, loops, and logical reasoning is demonstrated by creating a C++ program to identify evil numbers. For best outcomes, it additionally utilizes built-in routines like _builtinpopcount. Because they enhance problem-solving skills and broaden knowledge of binary processing, these exercises are helpful for students and programmers who want to reinforce their grasp of fundamental programming concepts.

Input Required

This code uses input(). Please provide values below: