Problème
Afficher la traductionDecide whether n equals 2ᵏ for some whole number k ≥ 0 (1, 2, 4, 8, …).
Implement is_power_of_two(n).
Input
n: The number to check.
Output
Return true if n is a power of two.
Contraintes
-10¹⁸ ≤ n ≤ 10¹⁸
Exemples
Exemple 1
Entrée :
n = 64
Sortie attendue :
expected = True
Entrée :
long long n = 64;
Sortie attendue :
bool expected = true;
Entrée :
long n = 64;
Sortie attendue :
bool expected = true;
Explication
64 = 2⁶.
Exemple 2
Entrée :
n = 12
Sortie attendue :
expected = False
Entrée :
long long n = 12;
Sortie attendue :
bool expected = false;
Entrée :
long n = 12;
Sortie attendue :
bool expected = false;
Explication
12 = 4 × 3 has an odd factor, so it is not a power of two.
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.