EmbeddedRelated.com
Blogs
JetBrain Embedded Development Trends

There are 10 kinds of people in the world

Colin WallsSeptember 27, 2023

Many years ago - I must have been 11 or 12 - I was at school and we were scheduled to have a Mathematics lesson. A different teacher came into the room, explained that the Mathematics teacher was off sick and that he would take the class instead. He said “I have no idea where you are on the curriculum, so we’ll go off piste and have some fun.” He taught us about number bases, explaining that base 10 was arbitrary probably to do with our 10 fingers and a number system could be based on any value. He showed that base 4 would only need the digits 0, 1, 2 and 3 and that base 12 would two more digits that could be A and B. He said that you could even have base 2, where the only digits would be 0 and 1 and that this was called binary. Some of my classmates were baffled, but for me a light bulb came on in my head and the concept was crystal clear.

I wondered when I might have some use for this information. The teacher had said something about binary being the “language of computers”, but it was not until nearly a decade later that this made any sense to me and his teachings could be put to use.

My first programming was using high-level languages Fortran, BASIC and others which were designed to hide the inner workings of the computer. But I soon became curious about the details and learned about assembly language and how machine instructions were just binary sequences. One of the first computers that I studied at “low level” was a DEC PDP8 - a minicomputer with 12-bit words. On the front of the machine was a line of switches that could be used to look at memory and deposit values including machine instructions in binary.

This article is available in PDF format for easy printing

This was not a very efficient way to program, of course, and there were tools to make it easier. In assembly language, the binary values were normally represented by four octal - base 8 - digits. I quickly became fluent in using octal to visualize the binary value. A more advanced computer was the 16-bit PDP11, for which DEC continued to use octal, representing words with six digits, where the most significant one could only be 0 or 1 which I always thought was a bit messy. Later, the 32-bit VAX machines were introduced and DEC started to be more conventional and used base-16 - hexadecimal.

Of course, nowadays hex is ubiquitous. However, despite 40+ years of experience, I still cannot make the instant translation to binary that I could do so easily with octal. When I learned C, I was disappointed to find that the language did not have a means to enter binary constants - only octal and hex. In due course, I decided to do something about that …

My solution was very simple. I created a file - binary.h - that contain 256 lines like this:

#define b00000000 ((unsigned char) 0x00)
#define b00000001 ((unsigned char) 0x01)
#define b00000010 ((unsigned char) 0x02)
...

This easily enabled me to reach a personal “holy grail” - to write more readable code - as binary values in my C programs would be instantly understandable. Although it is very easy to create, if you would like a copy of the binary.h file, drop me an email.

My next problem was how to deal with 16-bit values. I could have just created an upgraded binary.h file with 65,536 lines providing symbols for every possible 16-bit number in binary. Although quite easy to create, the file would be unwieldy and would not really solve the problem, as an unbroken string of 16 binary digits would be hard to read. The solution was to deal with the upper and lower bytes separately. I made a file with 512 lines like this:

#define Hb00000000 ((unsigned short) 0x0000)
#define Hb00000001 ((unsigned short) 0x0100)
#define Hb00000010 ((unsigned short) 0x0200)
...
#define Lb00000000 ((unsigned short) 0x00)
#define Lb00000001 ((unsigned short) 0x01)
#define Lb00000010 ((unsigned short) 0x02)
...

Then it is just a matter of using the C language OR operator to join the high and low bytes:

Hb00001111 | Lb1111000

This would be equivalent to 0x0ff0.

The same idea could be applied to 32-bit values, defining each of the four bytes in a file with 1024 entries.

Having addressed this topic, I cannot resist quoting one of my favorite jokes. It may be old, but I like it: “There are 10 kinds of people in the world: those who understand binary and those who do not.” 🙂



JetBrain Embedded Development Trends

To post reply to a comment, click on the 'reply' button attached to each comment. To post a new comment (not a reply to a comment) check out the 'Write a Comment' tab at the top of the comments.

Please login (on the right) if you already have an account on this platform.

Otherwise, please use this form to register (free) an join one of the largest online community for Electrical/Embedded/DSP/FPGA/ML engineers: