Divisibility by 64 with removal of bits allowed in C++ Program


In this tutorial, we are going to write a program that checks whether the given binary number is divisible by 64 or not.

We have given a binary number and we can remove the bits to make it divisible by 64. After removing the bits, if the number is divisible by 64, then print Yes else No.

The method that we are going to use is very simple. Let's see the steps to solve the problem.

  • Initialize the binary number in string format.

  • Iterate over the given binary number.

  • Count the number of zeros.

  • If the binary number contains more than or equal to 6 and there is a 1 bit, then the number is divisible by 64.

  • Print whether the given binary number is divisible by 64 or not.

Example

Let's see 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;
}

Output

If you run the above code, then you will get the following result.

Yes

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 28-Jan-2021

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements