whats is OOP up-arrow binary Arduino hints and tips - Binary - 2
Dual arduinos in control panel hdr

Binary - how computers represent numbers


Most people if asked "How many fingers to you have?" would reply, "I have 10!"

Perhaps this is why we tend to think of numbers in terms of decimal digits - that is, a series of digits taking on one of 10 values from 0 to 9.

Each digit's value is dependent on it's position in the number.

What does the number 2074 mean?

We all (at least most of use) have been taught that the farthest right digit is in the one's position. The next to it's left is in the 10's position. The next in the 100's position and so on. Note how each position's value is 10 times that of the one to it's right. We say numbers written this way are encoded using a base or radix of 10.

Decoding a decimal number
    
       2 x 1000 = 2000
       0 x  100 =    0
       7 x   10 =   70
       4 x    1 =    4
                  ----
                  2074 - when summed they add up to the number we call 2074             
    

How many fingers does a computer have?

Obviously that's that's a philosophical question as computers (at least most of them) have no fingers at all.

However we are probably justified if we say a computer has two fingers and therefor uses binary (rather than decimal) to represent numbers. The binary system offers a restricted set of values for its digits compared with the 10 offered by decimal - only 1 and 0 are available.

Perhaps you can think of some common applications of binary around your house? The most common would be a light switch. It's off - or zero. Or it's on - or one. You can almost certain think of others. Is the door open or closed? Is the refridgerator compressor running or stopped?

It turns out that binary is good fit for digital electronics as it is based on lots and lots of transistorized switches that are either on or off.

Binary encoded numbers are sometimes referred to as base or radix 2 values. Which gets back to the discussion of how many fingers a computer has.

In decimal, the right-most digit is the one's position, then the 10's position, etc.

What might the digits be called in binary. Well, once again the farthest right (referred to as the least significant) digit is the one's position. Multiplying that by the radix, the next digit to the left is the two's position, then the four's, eight's, sixteen's etc.

Another reason is that when I see bcsjTime used to declare a variable or function parameter I know that element is intended to hold a time value instead of something else.

Decoding a binary number
    
       What does the binary number 10010011 mean?
       
       1 x  128 =  128
       0 x   64 =    0
       0 x   32 =    0
       1 x   16 =   16
       0 x    8 =    0
       0 x    4 =    0
       1 x    2 =    2
       1 x    1 =    1
                  ----
                   147 - the decimal equivalent of binary 10010011             
    

Most though you won't need to deal with binary numbers (although an understanding of them can be helpful if you are trying to manually program a CV in a DCC decoder!)

The digits in a binary number are usually referred to a bits. In our previous example, the binary number 10010011 has 8 bits. Eight bits are commonly called a byte.

The char and unsigned char variable types in C/C++ both hold 8-bit values.

The long and unsigned long variable types hold 32-bit values (or four bytes).

Binary works great for computers. However, they take a lot of space to represent to humans. Which would you rather work with (at least for most purposes), 10010011 or 147?

However sometimes we'd like to be able to quickly tell which bits are set and which are clear in a value. Decimal won't do this for us and binary is a bit cumbersome. Are there alternatives? As it happens there are!

There are two. They are hexadecimal (base 16) and octal (base 8).

In the days of yore some computers had 6-bit bytes. A 6-bit value can be represented as a pair of octal (0 to 7) digits each of which covers 3 bits. See the illustration below. With the advent of 8-bit byte computers, octal notation slowly faded away and is hardly used anymore. However C/C++ still support octal notation. If you see a contant value starting with 0 (zero) in the program's source code, the compiler will treat that as being octal (base 8).

Therefor do NOT add zeros in front of your constant literals! 0123 != 123 Instead 0123 means 1 x 64 + 2 x 8 + 3 = 83 (in decimal)!

Hexadecimal or base 16 uses digits that can represent 16 values from 0 to 15. Because we don't have single digit numbers representing 10 to 15 the letters 'a' to 'f' are used. Each hexadecimal digit spans four bits. So two hex digits can represent a byte and 8 will represent a 32 bit value. In a C/C++ source file a number starting with "0x" is asumed by the compiler to be hex. The digit positions in hexadecimal are (you guessed it) one's, sixteen's, 256's, 4096's, 16384's, etc.

Converting binary to hex or octal
       
      1    5     0     5     7     1     Octal value  0150571
        |     |     |     |     |     |
        
       1 1 0 1 0 0 0 1 0 1 1 1 1 0 0 1   Binary value
       
      |       |       |       |       |
          d       1       7       9      Hex value  0xd179