# Binary

Summary: Binary Numbers 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 NOT OR) gate would be used with an NAND (NOT AND) 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.
AND gate constructed from two 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 a 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 Numbers
DCCWiki.com
Numbers 48-95 and 128-255 omitted for brevity.
Decimal Binary Decimal Binary Decimal Binary Decimal Binary Decimal Binary
0 00000000 16 00010000 32 00100000 96 01100000 112 01110000
1 00000001 17 00010001 33 00100001 97 01100001 113 01110001
2 00000010 18 00010010 34 00100010 98 01100010 114 01110010
3 00000011 19 00010011 35 00100011 99 01100011 115 01110011
4 00000100 20 00010100 36 00100100 100 01100100 116 01110100
5 00000101 21 00010101 37 00100101 101 01100101 117 01110101
6 00000110 22 00010110 38 00100110 102 01100110 118 01110110
7 00000111 23 00010111 39 00100111 103 01100111 119 01110111
8 00001000 24 00011000 40 00101000 104 01101000 120 01111000
9 00001001 25 00011001 41 00101001 105 01101001 121 01111001
10 00001010 26 00011010 42 00101010 106 01101010 122 01111010
11 00001011 27 00011011 43 00101011 107 01101011 123 01111011
12 00001100 28 00011100 44 00101100 108 01101100 124 01111100
13 00001101 29 00011101 45 00101101 109 01101101 125 01111101
14 00001110 30 00011110 46 00101110 110 01101110 126 01111110
15 00001111 31 00011111 47 00101111 111 01101111 127 01111111

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 number things starting with 0! This means, of course, that one must make a mental adjustment –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 21 20
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! 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 hexadecimal digits along with the equivalent decimal value.

DCCWiki.com
Some sequences of numbers were removed for brevity.
Decimal Hex Decimal Hex Decimal Hex Decimal Hex Decimal Hex Decimal Hex Decimal Hex Decimal Hex Decimal Hex
0 0x00 16 0x10 32 0x20 128 0x80 160 0xA0 176 0xB0 208 0xD0 224 0xE0 240 0xF0
1 0x01 17 0x11 33 0x21 129 0x81 161 0xA1 177 0xB1 209 0xD1 225 0xE1 241 0xF1
2 0x02 18 0x12 34 0x22 130 0x82 162 0xA2 178 0xB2 210 0xD2 226 0xE2 242 0xF2
3 0x03 19 0x13 35 0x23 131 0x83 163 0xA3 179 0xB3 211 0xD3 227 0xE3 243 0xF3
4 0x04 20 0x14 36 0x24 132 0x84 164 0xA4 180 0xB4 212 0xD4 228 0xE4 244 0xF4
5 0x05 21 0x15 37 0x25 133 0x85 165 0xA5 181 0xB5 213 0xD5 229 0xE5 245 0xF5
6 0x06 22 0x16 38 0x26 134 0x86 166 0xA6 182 0xB6 214 0xD6 230 0xE6 246 0xF6
7 0x07 23 0x17 39 0x27 135 0x87 167 0xA7 183 0xB7 215 0xD7 231 0xE7 247 0xF7
8 0x08 24 0x18 40 0x28 136 0x88 168 0xA8 184 0xB8 216 0xD8 232 0xE8 248 0xF8
9 0x09 25 0x19 41 0x29 137 0x89 169 0xA9 185 0xB9 217 0xD9 233 0xE9 249 0xF9
10 0x0A 26 0x1A 42 0x2A 138 0x8A 170 0xAA 186 0xBA 218 0xDA 234 0xEA 250 0xFA
11 0x0B 27 0x1B 43 0x2B 139 0x8B 171 0xAB 187 0xBB 219 0xDB 235 0xEB 251 0xFB
12 0x0C 28 0x1C 44 0x2C 140 0x8C 172 0xAC 188 0xBC 220 0xDC 236 0xEC 252 0xFC
13 0x0D 29 0x1D 45 0x2D 141 0x8D 173 0xAD 189 0xBD 221 0xDD 237 0xED 253 0xFD
14 0x0E 30 0x1E 46 0x2E 142 0x8E 174 0xAE 190 0xBE 222 0xDE 238 0xEE 254 0xFE
15 0x0F 31 0x1F 47 0x2F 143 0x8F 175 0xAF 191 0xBF 223 0xDF 239 0xEF 255 0xFF

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