# Binary A computer uses software in the form of binary numbers controlling millions of logic gates to perform a task.

# Base Two

Binary is a numbering system also known as Base 2. There are only 2 numbers employed, Zero and One. In digital electronics they represent two states: Low and High. Low can also mean "off", and high would be "on".

Binary is the reason one kilobyte of memory is really 1024 bits, and a megabyte is 1024 X 1024, or 1, 048,576 bytes.

### Notation

Due to the difficulty in reading large binary numbers, other systems such as Octal (Base 8) and Hexadecimal (Base 16) are used. Hex(adecimal) is often used to display numbers in a human-readable form. Converting binary to hex saves space and makes the number easier to understand or less likely to be entered incorrectly. Hex can be identified by the 0 to F numerals it uses. Often it will be prefixed by a "\$", such as \$A4.

The DCCWiki uses several forms of notation.

A number can be expressed as 1010, 102 , 108 , or 1016. The subscript indicates the radix or base of the numbering system. Base 10 is Decimal. Other possibilities are 2: Binary, 8: Octal, and 16: Hexadecimal or "hex".

Most numbers will be decimal values and there will be no indication of their base. The preferred method of writing Hexadecimal is the "\$" prefix.

## Digital Electronics

Computers are very simple machines: They take a repetetive task and do it over and over and over again.

Deep inside the computer is an Arithmetic and Logic Unit (ALU). This is the 'brains' of the outfit-- it handles all of the 'thinking' of the computer.

The ALU does its thinking using binary numbering. The computer does the arithmetic while counting on its fingers. Of course, binary numbering is for people with only 2 fingers. (There are only 10 kinds of people in the world: Those who understand Binary, and those who don't.)

Since a computer is digital and only understands two states (low/high), it does all it's operations based on these states and their combinations. It can only add, but by using various logical operations, it can do various calculations.

### Boolean Algebra

Binary numbers make as much sense to a computer as base 10 numbers do to us. A microprocessor is a large collection of very simple logic circuits that work with Boolean Algebra.

The functions are as follows:

AND (multiply): 1 X 1=1, 1 X 0 = 0

OR (add): 1+1=1, and 1 + 0 = 1.

XOR: One or the other, but not both: 1+1=0, while 1+0=1. An XOR (Exclusive OR) gate would be used with an NAND gate in parallel to carry the "1", since the result of 1+1 would be 2, or "10".

NOT (invert): 1=0

There are combinations based on inverting logic, called NAND, and NOR. Any logic function can be implemented using NAND gates.

Implementing logic is easy, and by using various combinations of binary numbers, things can be made to happen. These logic gates can have more than 2 inputs, which will determine the resulting output depending on the logic function. For example, four inputs to an AND gate could be used to check if four devices (like door switches) are positioned correctly. A light, or another function, cannot become active until all the input conditions are met, such as the gates being in place on a large machine before the operator can start the process.

Another example would be a switch with a signal protecting it. If the switch is thrown, the signal is red. If closed, green. Should a facing switch further down the main line be thrown, the signal is orange instead of green. A switch that does not face the direction of travel will cause the signal to go red. This is easily done with simple logic gates. A simple program on a simple microprocessor can also do this, and more.

## Bit

A bit is the smallest piece of information a computer can deal with. A bit can be on or off. Consider your flashlight. It is a bit -- it's ON or OFF. It's that simple!

In more technical terms, a bit (binary digit) refers to a digit in the binary numeral system, which consists of base 2 digits, that is, there are only 2 possible values: 0 (Off) or 1 (on).

We use the following notation to represent the status of a bit. In its OFF state, we show the bit as a 0 (zero). In its ON state, we show the bit as a 1 (one).

## Nibble

A nibble is four bits. Or half a byte.

## Byte

Well, a bit is nifty and all that, but it doesn't do much or represent very much by itself. It can't count or can't keep track of multiple pieces of information. So we need to complicate things.

A byte is simply 8 bits in a row. Or, think of it as 8 flashlights, all lined up. Each flashlight can be on or off. If you try, you can list all of the possible combinations:

00000000

00000001

00000010

00000011

00000100

00000101

00000110

00000111

00001000

-skipping a few here-

11111000

11111001

11111010

11111011

11111100

11111101

11111110

11111111

There are actually 256 combinations - from 00000000 thru 11111111

## Binary Numbering

Well, probably not so surprising, the combinations shown above are in order. And they represent numbers, too!

00000000 - 0

00000001 - 1

00000010 - 2

00000011 - 3

00000100 - 4

00000101 - 5

00000110 - 6

00000111 - 7

00001000 - 8

-skipping a few here-

11111000 - 248

11111001 - 249

11111010 - 250

11111011 - 251

11111100 - 252

11111101 - 253

11111110 - 254

11111111 - 255

Notice (you can take my word for it for the time being) that although there are 256 possible combinations, we use one of those combinations as zero, so the possible numbers represented by a byte are 0 thru 255.

This is why some computer-based equipment numbers things starting with 0! This means, of course, that you have to make a mental adjustment-- you start counting with 0, not 1. You can also think of this as starting at 0, then the numbers represent the steps you have taken down a path-- 1, 2, 3, etc.

All you have to do is memorize the complete table, and you've mastered Binary Numbering!

Well, you can. But that's probably the hard way! (;

## Decimal Arithmetic

Let's examine the numbers we're most familiar with: Decimal Numbers.

We have a number of columns, and each column represents a "power of 10."

10,000 1,000 100 10 1

0 1 0 1 1

1 1 1 0 0

In the first line, we have 1 in the "thousands" column, 1 in the "tens" column, and 1 in the "ones" column. We add them together to get "One thousand eleven."

That's (1 x 1000) + (1 x 10) + (1 x 1) = 1011

In the 2nd line, we have 1 in the "ten-thousands" column, 1 in the "thousands" column, and 1 n the "hundreds" column-- "Eleven thousand one hundred."

In the 2nd line again, we could say we have

(1 x 10000) + (1 x 1000) + (1 x 100) = 11100

Notice I've only selected examples using 0 and 1 in each column. This is because once we get to Binary Arithmetic we won't need any other numerals!

## Binary Arithmetic

Always remember-- in most cases you DO NOT have to translate back and forth between Binary and Decimal!

Of course, you should know how to convert back and forth-- it's what makes CVs in your DCC decoder so much -=FUN!=-

Just as in the Decimal world, we have columns, and each column represents a "power of 2."

128 - 64 - 32 - 16 - 8 - 4 - 2 - 1

```0	  0	  0	  0	  0	  0	  0	  0

0	  0	  0	  0	  0	  0	  0	  1

0	  0	  0	  0	  0	  1	  0	  0

1	  0	  0	  1	  0	  1	  1	  0

1	  1	  1	  1	  1	  1	  1	  1
```

In the first line, just above, all values are zero, so the decimal value is also zero.

2nd line-- There is a '1' in the "ones" column. So the decimal value is (1 x 1) = 1.

3rd line-- There is a '1' in the "fours" column. So the decimal value is (1 x 4) = 4.

In the 4th line, we have (1 x 128) + (1 x 16) + (1 x 4) + (1 x 2) = 150

In the 5th line, we have (1 x 128) + (1 x 64) + (1 x 32) + (1 x 16) + (1 x 8) + (1 x 4) + (1 x 2) + (1 x 1) = 255

Binary addition is just like addition in Decimal, except you have to carry awfully soon! Since we can only have 0 or 1 in a position, if we attempt to add binary 1 + 1, we don't get two, we get 10!

Example: Add the following binary numbers together:

0 0 0 0 1 0 1 0

0 0 0 0 1 1 0 0

0 0 0 1 0 1 1 0

The 2s and 4s columns were easy-- 1 + 0 = 1

In the 8s column, we need to add 1 + 1. The answer is 0 and carry the 1.

We carry the 1 to the 16s column, so our answer is 00010110

Example: Add the following binary numbers together:

0 1 0 1 0 1 1 0

0 1 1 1 0 1 1 1

1 1 0 0 1 1 0 1

Whooo, boy! What fun! I had to do that a couple of times, then translate to decimal and verify my answer:

Let's examine what happened:

Starting in the 1s column: 1 + 0 = 1

2s column: 1 + 1 = 0 and carry a 1 to the 4s column

4s column: 1 + 1 = 0 and carry a 1 to the 8s column, plus a 1 carried from the 2s column = 1.

8s column: 0 + 0 = 0 plus a 1 carried from the 4s column = 1.

16s column: 1 + 1 = 0 and carry a 1 to the 32s column.

32s column: 0 + 1 = 1 plus 1 carried from the 16s column = 0 and carry a 1 to the 64s column.

64s column: 1 + 1 = 0 and carry a 1 to the 128s column, plus a 1 carried from the 64s column = 1.

128s column:0 + 0 = 0, plus 1 carried from the 64s column = 1.

Or, in decimal, 86 + 119 = 205.

Well, it's nice to know that the value for your CV is 11010011, but that's a lot to remember! So there are ways to make the memorization easier.

First, lets divide a byte into two nibbles. *sigh* Yes, it's geeky. So our number 11010011 is written 1101 0011. We can represent these two nibbles as two numbers or letters using hexadecimal notation.

Binary may be converted to and from hexadecimal somewhat more easily. This is due to the fact that the radix of the hexadecimal system (16) is a power of the radix of the binary system (2). More specifically, 16 = 24, so it takes four digits of binary to represent one digit of hexadecimal.

The following table shows each hexadecimal digit along with the equivalent decimal value and four-digit binary sequence:

Hex Dec Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
Hex Dec Binary
4 4 0100
5 5 0101
6 6 0110
7 7 0111
Hex Dec Binary
8 8 1000
9 9 1001
A 10 1010
B 11 1011
Hex Dec Binary
C 12 1100
D 13 1101
E 14 1110
F 15 1111

To convert a hexadecimal number into its binary equivalent, simply substitute the corresponding binary digits:

3A16 = 0011 10102
E716 = 1110 01112

To convert a binary number into its hexadecimal equivalent, divide it into groups of four bits. If the number of bits isn't a multiple of four, simply insert extra 0 bits at the left (called padding). For example:

10100102 = 0101 0010 grouped with padding = 5216
110111012 = 1101 1101 grouped = DD16

To convert a hexadecimal number into its decimal equivalent, multiply the decimal equivalent of each hexadecimal digit by the corresponding power of 16 and add the resulting values:

C0E716 = (12 × 163) + (0 × 162) + (14 × 161) + (7 × 160) = (12 × 4096) + (0 × 256) + (14 × 16) + (7 × 1) = 49,38310