-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIs_power_of_2_IMP.cpp
63 lines (58 loc) · 1.12 KB
/
Is_power_of_2_IMP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<math.h>
using namespace std;
// Method One
bool isPowOf2(int n){
if(n & (n-1)) // if n & (n-1) == 0 then n is power two
return false;
else
return true;
}
// Method Two
bool _isPowOf2(int n){
int count = 0;
while(n!=0){
if(n&1){
count++;
}
n = n>>1; //(counting ones) dividing by two
}
if(count==1) //if (count of set bits i.e no of ones = 1) then
return true; //its value will be power of 2
else
return false;
}
// Method Three
bool __isPowOf2(int n){
int ans = 1;
for(int i=0;i<30;i++){
if(ans==n){
return true;
}
if(ans<INT_MAX/2){
ans = ans*2; //multiplying by 2 and compairing
}
}
return false;
}
int main(){
cout<<"Enter the integer number : ";
int n;
cin>>n;
if(isPowOf2(n)){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
if(_isPowOf2(n)){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
if(__isPowOf2(n)){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
return 0;
}