[[pow2]]

Test if a number is 2^n

#include <iostream>
#include <math.h>
#include <limits.h>
 
using namespace std;
 
int main(void);
 
int main()
{
    cout << "Enter number: ";
    int num = -1;
    cin >> num;
    cout << "You entered " << num << endl;
    int result = num & (num-1);
    cout << "result : " << result << endl;
    if ((num > 0) && (result == 0)) {
        cout << num << " is 2^n" << endl;
    }
    return 0;
}

pow2.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0