# Consider the following code snippet for checking whether a number is the power of 2 or not.if x=0

``````bool isPowerOfTwo (unsigned int x)
{
return (!(x&(x-1)));
}
``````

I am really confused in this problem according to my concept the answer is yes but the main answer is . this snippet is not correct for 0. help me, anyone, plz

You are using unsigned int..
If x=0, x-1=-1 which does fit in the range of unsigned int which is [0,4,294,967,295] hence there will be underflow.
Also, 0 is not a power of 2.
To handle this case use

``````return x && (!(x&(x-1)));
``````

thankyou bro 1 Like
{"ft_lang_label":"__label__en","ft_lang_prob":0.93677014,"math_prob":0.96363115,"size":677,"snap":"2020-34-2020-40","text_gpt3_token_len":192,"char_repetition_ratio":0.12035661,"word_repetition_ratio":0.60655737,"special_character_ratio":0.31019202,"punctuation_ratio":0.15384616,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97730404,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-19T00:25:42Z\",\"WARC-Record-ID\":\"<urn:uuid:5150926d-b873-49e7-8103-3e4154d69da6>\",\"Content-Length\":\"17428\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:629da23a-4bb8-414f-81df-c8c34812fca7>\",\"WARC-Concurrent-To\":\"<urn:uuid:8447dde7-d963-4181-b4ff-1cab207071f4>\",\"WARC-IP-Address\":\"52.54.40.124\",\"WARC-Target-URI\":\"https://discuss.codechef.com/t/consider-the-following-code-snippet-for-checking-whether-a-number-is-the-power-of-2-or-not-if-x-0/73599\",\"WARC-Payload-Digest\":\"sha1:26ZPVYKZSR4UYH5F6PLYVM5MDVANVBND\",\"WARC-Block-Digest\":\"sha1:WR5QPSPYRRV7VERQ3OVSD2RSNACVMX6M\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400189264.5_warc_CC-MAIN-20200918221856-20200919011856-00660.warc.gz\"}"}