In this tutorial, we will write a program that checks if a given binary number is divisible by 64.
We are given a binary number and we can remove some bits to make it divisible by 64. After removing the digits, if the number is divisible by 64, Yes is printed, otherwise No is printed.
The method we will use is very simple. Let's look at the steps to solve the problem.
Initialize binary numbers in string format.
Traverse the given binary number.
Count the number of zeros.
If a binary number contains greater than or equal to 6 zero bits, the number is divisible by 64.
Print whether the given binary number is divisible by 64.
Let’s look at the code.
#include <bits/stdc++.h> using namespace std; bool isBinaryStringDivisibleBy64(string binary_number, int n) { int zeroes_count = 0; for (int i = n - 1; i >= 0; i--) { if (binary_number[i] == '0') { zeroes_count++; } if (zeroes_count >= 6 && binary_number[i] == '1') { return true; } } return false; } int main() { string binary_string = "100100100100100"; if (isBinaryStringDivisibleBy64(binary_string, 15)) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
If you run the above code, you will get the following results.
Yes
If you have any questions during the tutorial, please mention it in the comment section.
The above is the detailed content of In a C++ program, when removing bits is allowed, determine whether a number is divisible by 64.. For more information, please follow other related articles on the PHP Chinese website!