Displaying binary numbers in Python

Understanding a binary number

Understanding a binary number

One topic that needs to be covered in OCR GCSE Computing (and presumably in the other exam boards as well) is converting denary numbers to binary and vice versa, so this would seem to be a good topic to use in a practical programming session in the various different resources.  This time I’ll be looking at writing a program in Python to input a denary (base 10) number and display the result as binary.

The program would use the following concepts: 

  • inputting a number and storing it as a variable
  • simple arithmetic on that variable
  • simple string concatenation (recognising 1 and 0 as characters as well as numbers)
  • if statements testing number for size (>=; greater than or equal to)
  • writing to screen

I would recommend that most of these skills are introduced as part of getting used to Python, for example inputting a number and reporting if it is bigger than or smaller than a set number; inputting two strings and putting them together before displaying (“Hello ” + name, for instance).

Extension possibilities for this exercise: loop the program to continue asking for numbers and displaying them, testing input for validity and stopping program or giving error message if invalid number is input (could be used to end program), exploring ways to display the answer other than outputting a string (this could possibly be on a revisit having learnt graphic display options later on).

the code for displaying a number as binary

binary display program

The algorithm for converting a number from denary to binary is straightforward – each digit in the binary represents a specific number. If the denary number is at least as big as that number, then turn that switch on.  Then you have accounted for that part of the number so can subtract it from the running total.   This program will work for numbers from 0 to 255 (using one byte, or 8 bits (switches) for storage).  This is basically the same algorithm I used for the arduino, to turn on or off a set of LEDs to display a number.  The only difference is that for the arduino I set all lights to off first, whereas here I’m building a string character by character, so have to include the else: line to turn the bit off if not in use.

binary code program results

Results of running the code

Note that at the moment there is no check on the input, so it’s up to the user to enter a number in the correct range, and I have to re-run the program each time I want to enter a new number.  This would be easy enough to fix, using a while loop for the entry and looping as long as a valid number is input, displaying that number then asking for the next.

I could also extend the program by writing a function to deal with displayBinary, and calling that within the loop, so separating out the main running loop (ask for number, display answer) from the rest of the work (work out what answer to display), but I was trying to keep this program as simple as possible in order to be able to have students run it and understand it very early on in their learning.

 

Advertisements

About emmyleigh
Writer/editor/proofreader who loves technology

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: