Home > Backend Development > C++ > In a C++ program, when removing bits is allowed, determine whether a number is divisible by 64.

In a C++ program, when removing bits is allowed, determine whether a number is divisible by 64.

PHPz
Release: 2023-09-01 20:17:14
forward
1294 people have browsed it

In a C++ program, when removing bits is allowed, determine whether a number is divisible by 64.

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.

Example

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] == &#39;0&#39;) {
         zeroes_count++;
      }
      if (zeroes_count >= 6 && binary_number[i] == &#39;1&#39;) {
         return true;
      }
   }
   return false;
}
int main() {
   string binary_string = "100100100100100";
   if (isBinaryStringDivisibleBy64(binary_string, 15)) {
      cout << "Yes" << endl;
   }
   else {
      cout << "No" << endl;
   }
   return 0;
}
Copy after login

Output

If you run the above code, you will get the following results.

Yes
Copy after login

Conclusion

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template