Header tag

Friday 18 November 2022

The BBC Micro and sums of 2^n

Our first home computer was an Acorn Electron. Not as widely known as the Commodore 64 or the Sinclair Spectrum, the Electron is most easily described as a cut-down version of the BBC Micro (which was extremely common in schools).  The Electron ran BBC BASIC, which made it relatively user-friendly. There was a full BASIC manual in the box, along with a book containing some relatively simple games and programs, and using the two together it was relatively easy to enter and the customise the games.

In particular, it was possible to modify the games' graphics, and the character sprites.  Each component of the game (a gameplay character) was made either from a combination of simple ASCII characters >->  o<-< or from user-defined graphics.  And the way that you defined the sprites used some simple maths based on a grid and the powers of 2 (1, 2, 4, 8, 16, etc.).  The grid is 8x8 pixels, and each column has a value.  The sum of each row gives its overall value.

Here's an example:


In order to program one of the game sprites, you take each row one at a time. You identify which of the columns have been filled for that row, and then sum their values (based on the column heading).  The first row in the example above has the two central squares shaded, and these have the values 16 and 8; the sum is 24.  The next row has four squares shaded, and these are 32, 16, 8 and 4.  The next has the central six squares, where 64+32+16+8+4 = 126, and the fourth row has all eight squares, the sum is 255.

The heart shape has some new and some familiar combinations, but the same principle applies.

And this walking stick-man has an asymmetrical arrangement, but this is no problem as the same principles apply.

Admittedly I was 8-10 years old when I was programming these sprites, but it never occurred to me that each combination was represented ny a unique sum, and conversely, each total could only be shown by one combination of pixels.

Why? In base 10, each number can only be expressed as a unique sum of 10^0s, 10^1s, 10^2s... and so on.  We call these units, tens and hundreds, and each can hold a value between 0 and 9. So, 9 x 10^0 is 9, and if we want to show an additional unit, we have to add this as an increment to the next column, and have 1x10^1 + 0 x 10^0.  Each number can (and must) be represented by a unique combination of digits in the hundreds, tens and units columns.

The same applies in binary, base 2, which is the underlying basis of the pixel grid. Each pixel can be either 1 or 0, with 1 representing a pixel, and 0 representing a gap.  And every number can be represented by a unique combination of 1s and 0s - binary is just the same as decimal counting, its just the digits that are different!  This time, the values of each column aren't 10^0, 10^1, 10^2 and so on, but 2^0, 2^1, 2^2, 2^3, 2^4 and so on. 

Naturally, the Acorn Electron was also fluent in hexadecimal, but that's a whole other story.

No comments:

Post a Comment