Denary to binary · Décimal vers binaire
Denary to binary
Go the other way: from a denary number to its binary digits.
Divide by 2 again and again. The remainder n % 2 is the next bit, and n // 2 is what is left to convert. The remainders come out right to left, so put each new bit at the front of the string. 0 needs its own answer, because the loop would never run.
Décimal vers binaire
Faire l'inverse : d'un nombre décimal vers ses chiffres binaires.
Diviser par 2 encore et encore. Le reste n % 2 est le prochain bit, et n // 2 est ce qui reste à convertir. Les restes apparaissent de droite à gauche, donc placez chaque nouveau bit au début de la chaîne. 0 nécessite sa propre réponse, car la boucle ne s'exécuterait jamais.
Write to_binary(n) that returns the binary digits of a whole number n (0 or more) as a string, without using bin(). 11 gives "1011" and · et 0 gives "0". · Écrivez to_binary(n) qui retourne les chiffres binaires d'un entier n (0 ou plus) sous forme de chaîne, sans utiliser bin(). 11 donne "1011" et 0 donne "0".
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.