Path: Home ==> Mikro beginner ==> Bin&Hex numbers     Diese Seite in Deutsch (extern):   # Brief introduction to binary and hexa-decimal numbers

Assembler, and therefore this course, too, very often use binary or hexa-decimal numbers. This page provides the basics of these number formats for beginners.
Binaries Hexa-decimals Conversion tools

## Binaries

Controllers only understand zeroes and ones, and several of those in packages of four, eight, sixteen, thirtytwo or whatever size in a row. All is done in 0 and 1.

Not only pure numbers are encoded in this mode, but also bits in registers, I/O ports or port registers. Those can only be one or zero. There is only one exception: Analog-Digital-converter input pins can handle non-digital voltages, but only to convert those to digitals.

All (theoretical and practical) number systems are based on a certain base number. The basis of our usual number system, the decimal system, is ten. We have ten digits, zero to nine, to construct all kind of numbers with those decimal digits.

In binary, based on two, only two digits are available: zero and one. If numbers reach or exceed the base number (ten or two), further digits are added to the left. Twelve in decimal is a one (left shifted a ten) plus a two. 12 in binary is a two (1 left shifted, a two) plus a one. So 12 in binary is 3 in decimal.

Numbers of any size can be handled like this. A five-digit decimal is
12345 = 1 * 104 + 2 * 103 + 3 * 102 + 4 * 101 + 5 * 100

The last expression part, 100, equals one (any number powered by 0 is one, in whatever number system).

The equivalen of decimal 12345 in binary is the following number:
11.0000.0011.1001 = 1 * 213 + 1 * 212 + 0 * 211 + 0 * 210 + 0 * 29 + 0 * 28 + 0 * 27 + 0 * 26 + 1 * 25 + 1 * 24 + 1 * 23 + 0 * 22 + 0 * 21 + 1 * 20

This is even simpler that the decimal calculation, as all multipliers are either 0 (do not add the power term) or 1 (add the power term). By leaving the not-to-be-added terms away and by listing only those that are (trivially) added, this simplifies to
213 + 212 + 25 + 24 + 23 + 20

The decimal values of the powers of two are 1, 2, 4, 8, 16, 32, 64, 128, etc., and those we have to add to yield 12345 decimal.

It is clear, that larger decimal numbers, here: 5 digits, have more binary digits (here: 14).

But: no one has to handle now 40 digit (the largest in this course) long binaries and read through a half-line long number, as there are methods to simplify those numbers (see the hexa-decimal section below) and because there are tools available for this.

The every-day length in AVR processing is eight bits long, more does not fit into one register of the AVRs. Therefore the powers of 2 until decimal 128 have to be remembered, more is not needed (as long as you do not have to assemble for a 64-bit-CPU).

In the contrary: the small binaries are often the more powerful: one single bit allows to switch a LED on or off, three bits (CS02, CS01 and CS00) select whether the timer/counter 0 is prescaled by one or by 8, 64, 256 or even 1,024. Eight bits can drive a whole organ of LEDs. Small, but never-the-less powerful.

To signal to the assembler that the following number is meant as a binary number (not a decimal by default), we precede binaries by 0b. The 0b111 means decimal 7, and not decimal 111. A remarkable difference!

In order to have bit 7 of a register set to one, the rest of it all zeroes, we formulate:
ldi R16,1<<7

The ldi means "load immediate" or "load a constant" the R16 means one of the 32 registers of the AVR. The constant to be loaded into that register is 1<<7. What the hack does that mean? Simple: take a one and shift that 7 times to the left (by introducing seven zeroes from the right. By shifting seven times we made a decimal 2 from the 1, a four from the two, etc. etc., until shfiting yields decimal 128. A ldi R16,128 would have done the same, but the left shifters look much nicer than the naked result.

And, to make it even more in-transparent, we could define the decimal 12345 from above not as .equ Number = 12345, but as
.equ Number = (1<<13) | (1<<12) | (1<<5) | (1<<4) | (1<<3) | (1<<0)

and by kicking at the brain of the reader. Those who search for "12345" in the source code editor now, won't find it, it is carefully hidden now behind left shifters.

Binaries Hexa-decimals Tools

## Hexa-decimals

To simplify the lengthy binaries, any four of those can be converted to a hexa-decimal digit. Four binary digits reach from 0b0000 (decimal 0) to 0b1111 (decimal 15). The 0 to 9 (0b1001) in hexa-decimal are the same as in decimal, but 10 (0b1010) is A, 11 (0b1011) is B, etc., etc., until 15 (0b1111), which is F. So a 40 bit binary can be expressed as a much shorter 10-digit hexa-decimal, even shorter than a decimal.

Decimal 12345 translates to hexa-decimal 0x3039, because
3 * 163 + 0 * 162 + 3 * 161 + 9 * 160 = 12345

We see that this is the same logic like converting binaries, but with the larger base number 16 instead of 2. Simple, but effective.
Binaries Hexa-decimals Tools

## Tools

For converting decimals to binary or to hex, and vice versa, we do not need to switch our brain on. There are several tools available that can do that for us. The first: calculators are available for any operating system. You'll have to bring them to the programmer mode. The one you see here is from Windows 10. This one knows 64 bits resolution, others are shorter.

If you'll need more numbers to be converted: the LibreOffice spreadsheet has the functions DEC2BIN, BINTODEC,DECTOHEX and HEXTODEC. In later versions of LibreOffice even with 64 bit resolution.