determines if a value is a multiple of X : bitwise AND « Operators statements « C++ Tutorial






#include <iostream>
using namespace std;
int multiple( int );

int main()
{
   int y;

   cout << "Enter an integer between 1 and 32000: ";
   cin >> y;

   if ( multiple( y ) )
      cout << y << " is a multiple of X" << endl;
   else
      cout << y << " is not a multiple of X" << endl;

   return 0;
}

int multiple( int num )
{
   int mask = 1, mult = 1;

   for ( int i = 0; i < 10; i++, mask <<= 1 )
      if ( ( num & mask ) != 0 ) {
         mult = 0;
         break;
      }

   return mult;
}








3.9.bitwise AND
3.9.1.Demonstrate bitwise &
3.9.2.Uppercase letters using bitwise AND.
3.9.3.determines if a value is a multiple of X