Any non-negative whole number that, when represented in binary, has an even number of 1s is classified as an evil number. For instance, the number 9 (in binary: 1001) is considered an evil number since it contains two 1s. Evil numbers are commonly used to practice manipulating binary values and bitwise operations in C++ programming. To determine if a number is evil, it must first be converted to binary; subsequently, the count of 1s needs to be checked to confirm it is an even number. Familiarity with this principle can enhance a programmer's comprehension of binary math, bitwise operations, and effective algorithms for analyzing binary digits. Evil numbers serve as a straightforward yet engaging method to introduce mathematical ideas 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.
Utilize bitwise manipulation to count the number of set bits in the binary representation of the integer.
Validate the integer to ensure it is an even number before displaying it to check for a property known as 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's consider a scenario to demonstrate the concept of Evil Numbers in the C++ programming language.
#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:
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's consider another instance to demonstrate the concept of Evil Numbers in C++.
#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:
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.
- 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.
Benefits:
Conclusion:
In summary, Evil numbers contribute to the exploration of binary numbers and their practical use in computers. Due to their binary form containing an even count of 1s, they provide a straightforward yet captivating method to practice bitwise operations and binary math. Proficiency in bitwise operations, loops, and logical thinking can be exhibited through the development of a C++ script dedicated to detecting evil numbers. To optimize results, leveraging predefined functions such as _builtinpopcount is recommended. These activities not only improve problem-solving abilities but also expand understanding of binary manipulation, making them valuable for individuals aiming to solidify their understanding of core programming principles.