20 = 1
= 000001 = 1 & (1-1) =000001 & 000000 = 000000
21
= 2
= 000010 = 2 & (2-1) =000010 & 000001 = 000000
22 = 4
= 000100 = 4 & (4-1) =000100 & 000011 = 000000
23 = 8
= 001000 = 8 & (8-1) =001000 & 000111 = 000000
24 = 16
= 010000 = 16 &
(16-1) =010000 & 001111 = 000000
25 = 32
= 100000 = 32 &
(32-1) =100000 & 011111 = 000000
Two
observations :
1 . number
of set bit for each number is always one
2 .
n&(n-1) == 0
Code :
#include<bits/stdc++.h> using namespace std; int main() { int n,i;
cin>>n;
if((n&(n-1))==0){
cout<<"yes\n"; } else
cout<<"not\n"; } |
Comments
Post a Comment