Denary to binary · Decimal para binário
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.
Decimal para binário
Faça o caminho inverso: de um número decimal para seus dígitos binários.
Divida por 2 repetidamente. O resto n % 2 é o próximo bit, e n // 2 é o que sobra para converter. Os restos aparecem da direita para a esquerda, então coloque cada novo bit na frente da string. 0 precisa de sua própria resposta, pois o loop nunca executaria.
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 · e 0 gives "0". · Escreva to_binary(n) que retorne os dígitos binários de um número inteiro n (0 ou mais) como uma string, sem usar bin(). 11 dá "1011" e 0 dá "0".
Click Run to see the output here. · Clique em Executar para ver a saída aqui.