Binary to decimal conversion

This guide has been designed to help you create a simple binary to decimal conversion program. Follow these step-by-step instructions to create a useful conversion program.

Use "Ask" and "Set" to get user input to a calculation. Stage 1 - Making the background

Start a new Scratch project - "File" and then "New" Switch to the stage and click on "backgrounds". Use the line drawing tool to draw this grid as closely as possible to the example. Now use the text tool to write the titles for the grid. You will have to use spaces to separate the words. The little black dot to the left and above "Eights" is the handle that you can use to move the titles to the right place. Like this... then press "OK" Stage 2 - Communicating with the user

Now we need the get the cat to tell the user what is going to happen like this... When you run this "thread" the cat will say "You are going to type in a 4 bit binary number." for two seconds which may not be long enough so change it to be 5 seconds. An "ask" block will ask a question and wait for a response. So ask the question "Please type in the first binary number. It must be 0 or 1." ... ... like this: The next step is to store the answer in a variable. I shall call the variable "eights". Click on "variables" then "Make a variable" and call the variable "eights". From the varables well select "set variable to" and drag it in and then choose"answer" from "sensing". So, "ask" asks a question and "set variable to" stores the answer, We can do this with the next 3 variables, "fours", then "twos" and "ones"

Note that I have arranged the variables on the screen so that they are in the right place on the grid. Screenshot your project so far and describe the difference between "Ask" and "Say" in Scratch.

Stage 3 - Making the calculation

Now for the arithmetic. We need another variable which can be called "total" ... , if any of the ones, twos, fours, or eights variables is 1 then we add 1, 2, 4 or 8 to the toal like this... Just to add some suspense we will pretend that the computer is thinking by adding a dramatic pause to the calculation, like this... Now the computer can give the answer...

However, can you spot the mistake. I have run the program twice and the "total" variable has not forgotten the first answer... ... there needs to be a way of setting the variables back to zero when the program starts.

Stage 4 - Initialisation

Add this new thread.A "thread" is a section of code started by an event, in this case "When the green flag is clicked". This thread sets all of the variables to zero at the start.

Have you noticed that I have moved the cat so that there is room to read the comments and see the variables. Your finished project should look something like this...

Is the decimal value of 1101 really 13?

Screenshot your project so far and the meaning of the word "Thread" in Scratch.

Stage 5 - Extension

Now make scratch convert a decimal number to binary.

This makes use of "lists" a variable that contains an unknown number of similar items.

Begin as always with "File" and then "New" to begin a new project. This is the algorithm (sequence of instruction to solve a problem). In words the decimal number is continually divided by 2 until there is nothing left. The remainder is put to one side. The answer is the remainders read from bottom to top. The trick here is that we do not know how many variables we would need. This is the reason for the list.

The first thing to do is to get a few variables and lists. We need variables "decimal" to store the decimal number we are converting, "remainder" to store the remainder of each calculation and lists "binary" to store the results of all the calculations and "answer" which is the"binary" list written backwards. Now begin the script that introduces the program with a "say" block and then ask the user for the number that they want to convert as an "ask" block; like this... Next pass the value that the user typed in to the variable "decimal" using a "set variable to" block. When you run the program now the variable "decimal" should contain the number that you typed in . Now add a code block that tells the user what is being done, Say it for 2 seconds. Next we are going to work on the algorithm using an "If ... else" block.

There is a form of arithmetic that works on whole numbers only; "Mod". Mod only shows the remainder, so for example 17 mod 2 is 1 and 8 mod 2 is 0.

We can use this to help with the arithmetic. We need to know if the remainder is 0 or 1 so add an "operator" block to the "If ... else" block so that it says "if decimal mod 2 = 1" like this ...

We need to repeat this process until decimal is zero (look at the algorithm again to see why) so put the "If ... else" block in a repeat loop with "decimal = 0" as the condition. There are two possible results, "decimal mod 2" can have the result of 0 or 1. If the result is 1 then set the remainder to be 1 and decimal to be (decimal - 1) / 2

This gets the decimal value ready for the next iteration (technical word meaning the next go round the algorithm). Now the other possibility, the answer to "decimal mod 2" is 0. In this case set the remainder to be 0 and decimal to be decimal / 2 Next we need to add "remander" to the list. There is a block on the varaibles well that says "Add thing to list", use this block and make it say "add remainder to binary" in each of the "if" and "else" blocks. Now comes the list bit that may be a littel difficult.
We need to mke sure that the "answer" list is empty so add a "repeat" block where the condition is "length of answer = 0" and then in the loop add a block that says "delete 1st of answer". This will go through the list as many times as it takes deleting the first entry in the list each time until there are no more entries in the list.

Next add another repeat block that will add the last of binary to answer and then delete the last of binary. If this is done until ther is nothing left in binary then answer will have the same values as binary but in reverse order. Finally add a "say" block that shows the answer The programs works well and should cope with any size of number.

So what is the binary for 106?

See if you can extend the calculation so that an 8 bit number can be converted.

Year 8 Extension

See if you can extend the calculation so that an 8 bit number can be converted. Use 11001100 as an example.

Recent Posts May 24

Repairing a hyperspace window

You may have noticed the time shift in the Blue coridor, there is a redshift at certain times of the day; this has caused the bells to malfunction and the PCs are runing slow as the server room is also moving ever so slowly into the 5th dimension. The Lightspeed engineers are working hard to fix the hyperspace window being generated by the tidal effects of big data and the weaknesses in the walls caused by excessive data mining. Cambridge Analytica's data scraping has also contributred to the weakness in the space time continuum. There is no need to be afraid as everything is should be resolved in the near future; nevertheless steps are being taken, mostly from the gold staircase.

Any further information will be included here when and if we get it. July 18

Adventuring with A GDPR SUrveyor

The GDPR surveyors will be in the College in the near future checking that you understand your obligations under GDPR. Are you aware of the consequences? Do you know who knows about you? Do you know what the people you don't know about, know about you? Have you checked your own settings? Are you privacy minded? The GDPR surveyors will check. They know you, possibly better than you do but maybe not as well as the Internet knows you. July 14