c++ - Find previous power of 2 in 64 bit number -
// objective : verify 64 bit input power of 2.if number not power of 2, find previous value , power of 2.
using namespace std;
int main() {
//input. 64 bit number unsigned long long int input = 13174607262084689114; //check number power of two. if (!((input && (input & (input - 1))) == 0)) { cout<<"the number not power of 2"<<endl; //find previous value of input, should power of 2. //step 1 : find next power of 2 input. //step 2 : divide number 2 /*end*/ // step 1 input |= input >> 1; input |= input >> 2; input |= input >> 4; input |= input >> 8; input |= input >> 16; input |= input >> 32; input = input + 1; // input holds next number , power of 2. // step 2 input = input >> 1; cout<<"power of 2: 64 bit: "<<input<<endl; } return 0;
}
(!((input) && (input & (input - 1))) == 0)
this should be
(!(input && (input & (input - 1)) == 0))
Comments
Post a Comment