Algorithms Quiz | Bit Algorithms | Question 4

Consider the following code snippet for checking whether a number is power of 2 or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Incorrect function to check if x is power of 2*/
bool isPowerOfTwo (unsigned int x) 
  return (!(x&(x-1))); 
}

chevron_right


What is wrong with above function?
(A) It does reverse of what is required
(B) It works perfectly fine for all values of x.
(C) It does not work for x = 0
(D) It does not work for x = 1


Answer: (C)

Explanation: Please see https://www.geeksforgeeks.org/program-to-find-whether-a-no-is-power-of-two/

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.