Here’s a simple micro:bit project to help you practice converting between binary and decimal (denary / base 10) numbers up to 31.
Binary place values | ||||
---|---|---|---|---|
16 | 8 | 4 | 2 | 1 |
1 | 0 | 1 | 0 | 1 |
In this example, 10101 in binary in decimal is 16 + 4 + 1 = 21.
Press button A and your micro:bit shows a random decimal number between 1 and 31.
Press button B and it shows the binary equivalent, represented on the top row of LEDs as lit for 1 and dark for 0.
You can use this to challenge yourself – press B to see the dots and work out the decimal equivalent, press A to see if you’re right. Or press A to get the decimal number, work out what the 0s and 1s should be, and press B to see if you got it correct.
Shake the micro:bit to generate a new random number.
It uses a version of the ‘divide by 2′ algorithm for converting base 10 (decimal) numbers to binary:
1 – divide number by 2
2 – store the remainder in an array
3 – repeat steps 1 and 2 until number = 0
4 – print the array in reverse order
View the MakeCode project here: https://makecode.microbit.org/_FMJb1PR6o6LR