Skip to content

Commit 3c16a4e

Browse files
authored
Create power_of_2.cpp
Program to check whether a given number is a power of 2 using bit-magic
1 parent 9393133 commit 3c16a4e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: power_of_2.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Program to check whether a given number is a power of 2 using bit-magic /*
2+
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
int main() {
8+
int t;
9+
cin>>t;
10+
while(t--)
11+
{
12+
long long int n;
13+
cin>>n;
14+
if(n==0)
15+
cout<<"NO";
16+
else if((n&(-n))==n)
17+
cout<<"YES";
18+
else
19+
cout<<"NO";
20+
21+
cout<<'\n';
22+
23+
24+
25+
26+
27+
28+
}
29+
return 0;
30+
}

0 commit comments

Comments
 (0)