Posts

Home |
By Category |
By Title |
By Date |
About |
Contact

### A Useful Python Algorithm For Displaying Binary Numbers

**2016/06/12**

One of the challenges you face teaching introductory coding is binary, base 16, and the relationship between them.

The rationale for binary eventually becomes obvious, but what is the purpose of hexadecimal? When I first encountered it, I just assumed that computer programmers used base 16 arithmetic because they're all a little weird.

Many students will get the rationale for hex and its relationship to binary instantly when you show them binary counting taking place on two groups of four LEDs. Assembling a device to do this makes a nice prototyping and programming exercise on the Raspberry Pi.

It's been a while since I've had to train anybody on it but I still like to use a binary counting or light-chaser program as a sort of "Hello, world!" exercise on a microcontroller.

Doing this in a higher-level language such as Python is a bit different from doing it in assembler. Here's a nice little algorithm to decode a decimal value into eight binary digits.

This function is called with a decimal number as a parameter, and returns a list with eight elements each of which will be zero or one. At the heart of it is a **for** loop that populates the list by doing a **bitwise logical AND** operation with the original decimal number and the value of two raised to each power from 0 through 7.

This works because the **AND** operation (represented by the **&** operator in the code) is a "bitwise" operation that takes two numbers and compares them bit-by-bit at the binary level. If the bit being examined is a one in both numbers, then that bit in the result will be a one.

The other sense of an **AND** operation would be the truth-functional one where we need to do something like:

IF ({out_of_stock} AND {none_on_order}) THEN {order_more}

That's pseudocode, by the way, not Python

The astute reader will notice that the list of 0/1 values returned by this function is backwards to what one would expect. That is, the least-significant bit comes first.

All right, then. Here's the complete code for the binary counter demonstration project I alluded to at the start of this:

If you want to turn your binary counting demonstrator into a simple light-chaser, change:

...to:

And change:

...to:

I'll leave it to you and your more perspacious students to explain what's going on with that. Something very similar saved an entire project and about four months' work for me a number of years ago.

And finally, here's the breadboard layout for the binary counting demonstrator / light chaser:

Bruce Grant's Web Page is licensed under a Creative Commons Attribution 4.0 International License.

Based on a work at http://www.wbga.ca..