Comments on: Check if number is a power of 2 https://demo.ritambhara.in/check-if-number-is-a-power-of-2/ Coding / System Design Interviews Tue, 01 Nov 2022 10:49:02 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Vithesh https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6958 Tue, 01 Nov 2022 10:49:02 +0000 http://www.ritambhara.in/?p=362#comment-6958 In reply to Anonymous.

its valid for all numbers
if its not then just reply that number here

]]>
By: Or https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6614 Sun, 29 May 2022 12:34:15 +0000 http://www.ritambhara.in/?p=362#comment-6614 Method 2 will only work if n is a double or a float, since it will be cast to an int after n=n/2.
Another option of course is initializing a new double that will take the value of n before the while loop, in case changing the input of the function is not an option.

]]>
By: Betty Mitchell https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6497 Tue, 15 Mar 2022 15:11:28 +0000 http://www.ritambhara.in/?p=362#comment-6497 A Very Good Method In programming languages is to anding two numbers like
N = int (input())
If N != (N&-N): print yes
Else no

]]>
By: Heba Allah Hashim https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6217 Fri, 07 Jan 2022 07:43:26 +0000 http://www.ritambhara.in/?p=362#comment-6217 method two should be (n > 1) not (n != 1)
—————————————————–
n = int(input())
state = “YES”

while n > 1:
n = n / 2
if n % 2 != 0 and n != 1:
state = “NO”
else:
state = “YES”
print(state)

]]>
By: twopi https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6203 Fri, 31 Dec 2021 11:30:32 +0000 http://www.ritambhara.in/?p=362#comment-6203 Inside the while loop of Method 2, “n = n/2;” should be placed after “if(n%2 != 0 && n != 1){ return 0; }”

]]>
By: Nitesh Pandey https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6202 Wed, 29 Dec 2021 17:41:08 +0000 http://www.ritambhara.in/?p=362#comment-6202 Method-3: Use Bitwise operators.

Wrong Code.- The operator ! is undefined for the argument type(s) int

]]>
By: "Trouver si l'élément est des pouvoirs de code de code de 2". - Coder les réponses https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6116 Sat, 13 Nov 2021 14:05:42 +0000 http://www.ritambhara.in/?p=362#comment-6116 […] La Source […]

]]>
By: "Trouver si l'élément est des pouvoirs de code de code de 2". - Coder les réponses https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6102 Sun, 07 Nov 2021 11:21:11 +0000 http://www.ritambhara.in/?p=362#comment-6102 […] Kaynak Étiquetteswhatever […]

]]>
By: "Öğenin 2" kodu olup olmadığını bul "kodu Cevaplar - Kod Yanıtları https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-6068 Fri, 29 Oct 2021 05:37:15 +0000 http://www.ritambhara.in/?p=362#comment-6068 […] Kaynak whatever whatever […]

]]>
By: Sukumar https://demo.ritambhara.in/check-if-number-is-a-power-of-2/#comment-5621 Wed, 05 May 2021 16:26:22 +0000 http://www.ritambhara.in/?p=362#comment-5621 Thanks for the methods. Will method 2 work the number 12 that is not power of 2 ?

]]>