Mega Code Archive

 
Categories / C++ / Language
 

Match mask with bit operator

#include <iostream> using namespace std; int mystery( unsigned ); int main() {    unsigned x;    cout << "Enter an integer: ";    cin >> x;    cout << "The result is " << mystery( x ) << endl;    return 0; } int mystery( unsigned bits ) {    unsigned mask = 1 << 15, total = 0;    for ( int i = 0; i < 16; i++, bits <<= 1 )       if ( ( bits & mask ) == mask )           ++total;    return total % 2 == 0 ? 1 : 0; }