class Solution { public: bool isPowerOfTwo(int n) { int count =0; if(n<0) { return 0; } while(n) { n&=n-1; count = count +1; } if(count ==1){ return 1; } else{ return 0; } } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter