-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_type_powerof2.cpp
73 lines (61 loc) · 982 Bytes
/
all_type_powerof2.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
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std ;
bool power_of(int n)
{
if(n==0)
{
return 0;
}
int power = 1;
for(int i = 0; i<=30 ; i++)
{
if(n==power)
{
return true;
}
if(power < INT_MAX/2)
{
power = power * 2;
}
}
return 0;
}
bool frombitlevelcheck(int n)
{
int c = 0;
while(n != 0)
{
int bits = n & 1 ;
cout<<bits<<" ";
if(bits == 1)
{
c++;
}
n = n>>1;
}
if(c==1)
{
return 1;
}
return 0;
}
int main()
{
int n;cout<<"Enter the number for checking its power of 2 or not ? --> ";
cin>>n;
// if(power_of(n))
// {
// cout<<"Yes Its power of 2 ";
// }
// else{
// cout<<"NOt a power of 2 ";
// }
if(frombitlevelcheck(n))
{
cout<<"yes";
}
else{
cout<<"NO";
}
return 0;
}