What does the following program do: bit move - C++ Operator

C++ examples for Operator:Bit Operator

Description

What does the following program do: bit move

Demo Code

#include <iostream> 
using namespace std; 

bool mystery( unsigned ); 

int main() /* w w  w.jav  a 2s  . c o m*/
{ 
   unsigned x; 

    cout << "Enter an integer: "; 
    cin >> x; 
    cout << boolalpha << "The result is " << mystery( x ) << endl; 
}

bool mystery( unsigned bits ) { 
   const int SHIFT = 8 * sizeof( unsigned ) - 1; 
   const unsigned MASK = 1 << SHIFT; 
    unsigned total = 0; 

    for ( int i = 0; i < SHIFT + 1; ++i, bits <<= 1 ) 
        if ( ( bits & MASK ) == MASK ) 
            ++total; 

    return !( total % 2 ); 
}

Result


Related Tutorials