Convert decimal to binary in Scratch

Asked

Viewed 7,108 times

1

Isn’t displaying the result on the screen? I tried to convert decimal number to binary in Scratch, but it’s not working. Can anyone see what the error is?

inserir a descrição da imagem aqui

  • The logic is incorrect. Use a list for the output of each binary digit within one looping if. Add 0 or 1 to the list as the result of the rest of the variable division entradaUsuario by 2. In addition to this check, update ("change") entradaUsuario for arredondar para baixo de "entradaUsuario" / 2 and in the condition of looping, place repita até que "entradaUsuario" < 0 ou "entradaUsuario" = 0

1 answer

1

The best solution to generate binary digits is to use a list to store the digits and a looping to reduce the variable entradaUsuario at each iteration.

With each iteration of looping, the algorithm extracts the binary digit through a split-by-2 rest operation and stores that digit in the list.

Then divide entradaUsuario by 2 until it reaches zero.

Follow the algorithm below:

inserir a descrição da imagem aqui

Link to the project on the Scratch website: Decimal for Binary


If you really need to get the digits in separate variables, just run the algorithm without the looping, repeating the code for the number of binary digits you need:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.