# Binary

Summary: Binary and Digital Command Control 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". The value of zero can also be expressed as 20, and one is 21. Binary is the reason one kilobyte of memory is really 1024 bytes or 210, 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 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

This is an advanced topic. It is presented to provide additional background information.

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

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

The ALU does its thinking using binary numbers. The computer does 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 its operations based on these states and their combinations. It can only add, but by using logical operations, it can do various calculations.

#### Boolean Algebra

Binary numbers make as much sense to a computer as decimal (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 XNOR (Exclusive NOR) gate would be used with an NAND gate in parallel to carry the "1", since the result of 1+1 would be "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 in its normal state, green. Should a facing switch further down the main line be thrown, the signal is orange instead of green. A thrown switch which 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 can also do this, and more.

Electrically, voltages represent binary values, when the signal is On or High, it is a 1, and when Off (low), a zero. These are often referred to a logic level signals, which have two states where ON is 5V, and OFF is zero. Logic gates are designed to accept a voltage above a certain level, such as 3.5V, as High, and those below 3V for example, as a Low value. The same can be done using pneumatics in an area where electrical signals present an explosion risk.

### Definitions

#### The 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 binary device -- it is 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/Low) or 1 (On/High).

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 a group of 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 permutations:

Binary Value
0000 0001
0000 0010
0000 0011
0000 0100
0000 0101
0000 0110
0000 1000
0000 1001
Skipping a few
0001 0000
0001 0001
0001 0010
0001 0011
0001 0100
0001 0101
Skip a few more
1111 1111

There are actually 256 combinations - from 0000 0000 thru 1111 1111

### Binary Numbering

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

0000 0001 1 1
0000 0010 2 2
0000 0011 3 3
0000 0100 4 4
0000 0101 5 5
0000 0110 6 6
0000 0111 7 7
0000 1000 8 8
0000 1001 9 9
0000 1010 A 10
Skipping a few
0001 0000 10 16
0001 0001 11 17
0001 0010 12 18
0001 0011 13 19
0001 0100 14 20
0001 0101 15 21
Skip a few more
1111 1111 FF 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."

Weight
10,000 1,000 100 10 1
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

BIT WEIGHT
27 26 25 24 23 22 22 21
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
1 0 0 0 0 1 0 0
1 0 0 1 0 1 1 0
1 1 1 1 1 1 1 1

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."

1. In the first line, just above, all values are zero, so the decimal value is also zero.
2. 2nd line-- There is a '1' in the "ones" column. So the decimal value is (1 x 1) = 1.
3. 3rd line-- There is a '1' in the "fours" column. So the decimal value is (1 x 4) = 4.
4. In the 4th line, we have (1 x 128) + (1 x 16) + (1 x 4) + (1 x 2) = 150
5. 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

An excellent example of this is calculating the value of the Mode Control variable CV29

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 1 Add the following binary numbers together
0 0 0 0 1 0 1 0
0 0 0 0 1 1 0 0
The Sum Is
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 2 Add the Following Binary Numbers
0 1 0 1 0 1 1 0
0 1 1 1 0 1 1 1
The Sum Is
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. And that's our answer: 11001101 Or, in decimal, 86 + 119 = 205.

#### Binary Subtraction

Addition and multiplication with logic gates is easy. Subtraction and division are not. Doing operations other than addition with a microprocessor is not easy. They can only add two binary numbers together. For multiplication, the same operation of addition is performed a number of times. for example, 4 × 3 = 4 + 4 + 4. For division, the subtract operation is repeated the appropriate number of times.

To subtract, the Ones' Complement method is used. Since this is an advanced topic, it will not be discussed. Following the link to the Wikipedia article will explain the process. The sign of a binary number is indicated by the first binary digit being a "1". For example, 1111 1111 could be either decimal 255 or −0, its Ones' Complement value.

The simple answer is: A computer subtracts by adding two numbers together, where one number is a Ones' Complement.

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