Analog Circuits Training - Binary to Integer and Integer to Binary Conversions
In this lesson you will learn how to convert Binary to Decimal and Decimal to Binary.
For these exercises you will need to refer to the following chart which gives the value of each bit. The 16 and 32 bit values are the same except where the 16 bit column ends at bit 15. Instead of being 32,768 as the 32 bit column, it is -32,768 which makes this a 16 bit signed integer capable of values from -32768 to 32,767. The 32 bit integer's bit 31 value of -2,147,483,648 which makes this a 32 bit signed integer capable of values from -2,147,483,648 to +2,147,483,647.
Bit # | 16 Bit Integer | 32 Bit Integer |
Bit 00 | 1 | 1 |
Bit 01 | 2 | 2 |
Bit 02 | 4 | 4 |
Bit 03 | 8 | 8 |
Bit 04 | 16 | 16 |
Bit 05 | 32 | 32 |
Bit 06 | 64 | 64 |
Bit 07 | 128 | 128 |
Bit 08 | 256 | 256 |
Bit 09 | 512 | 512 |
Bit 10 | 1,024 | 1,024 |
Bit 11 | 2,048 | 2,048 |
Bit 12 | 4,096 | 4,096 |
Bit 13 | 8,192 | 8,192 |
Bit 14 | 16,384 | 16,384 |
Bit 15 | -32,768 | 32,768 |
Bit 16 | 65,536 | |
Bit 17 | 131,072 | |
Bit 18 | 262,144 | |
Bit 19 | 524,288 | |
Bit 20 | 1,048,576 | |
Bit 21 | 2,097,152 | |
Bit 22 | 4,194,304 | |
Bit 23 | 8,388,608 | |
Bit 24 | 16,777,216 | |
Bit 25 | 33,554,432 | |
Bit 26 | 67,108,864 | |
Bit 27 | 134,217,728 | |
Bit 28 | 268,435,456 | |
Bit 29 | 536,870,912 | |
Bit 30 | 1,073,741,824 | |
Bit 31 | -2,147,483,648 |
A few other things to note before you start. When viewing the data table in RsLogix, the bits are laid out right to left with bit 0 being on the far right and either bit 15 or 31 being on the far left. Data files that being with a "N" are 16 bit and a "L" are 32 bit.
Converting Binary to Decimal
Randomly we are picking a binary number of 1001101101010101. If it were in an N7 data file then you would use the 16 bit column.
Lining these up in columns we can identify the bits that have 1's in them and enter the values from the table above as seen below. If a bit has a 0 in it then the value will also be 0:
Bit | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
0 or 1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
Value | -32,768 | 0 | 0 | 4,096 | 2048 | 0 | 512 | 256 | 0 | 64 | 0 | 16 | 0 | 4 | 0 | 1 |
Now we can simply add these values together.
-32,768+0+0+4,096+2048+0+512+256+0+64+0+16+8+4+1=-25,771
The easiest way to check this is in RsLogix. Open up your N7 data file, switch your Radix to Binary, enter the binary value above, then switch back to decimal.
Now lets say it were 32 bit or a LONG data type.
We will use the same binary number of 1001101101010101. This time we will make it L9 and use the 32 bit column.
Lining these up in columns we can identify the bits that have 1's in them and enter the values from the table above as seen below. If a bit has a 0 in it then the value will also be 0:
Bit | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
0 or 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
Value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 32,768 | 0 | 0 | 4,096 | 2048 | 0 | 512 | 256 | 0 | 64 | 0 | 16 | 0 | 4 | 0 | 1 |
Now we can simply add these values together.
0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+32,768+0+0+4,096+2048+0+512+256+0+64+0+16+8+4+1=39,765
The easiest way to check this is in RsLogix. Create a data file with a LONG data type, switch your Radix to Binary, enter the binary value above, then switch back to decimal.
Converting Decimal to Binary
To convert decimal values to binary values, you must first know whether you are using a 16 bit number or a 32 bit number so you know which column to use in the chart. It is simply a matter of finding the row with a value as close to the number you wish to convert without going over, putting a 1 in its bit position, then subtracting that value from the number you are converting then repeating this until you get a result of "0". This is better understood after working a few problems with it. We will take the results that we came up with in our Binary to Decimal exercise above and work them back to binary.
16 Bit Binary to Decimal -25,771 to a 16 bit integer.
The closest number to -25,771 without going over is bit 15's -32,768. Don't let the negative signs confuse you.
Bit 15 = -32,768. -25,771-(-32,768) = 6,997. The next largest bit without going over is bit 12, 4,096.
Bit 12 = 4,096. 6,997-4096 = 2,901. The next largest bit without going over is bit 11, 2,048.
Bill 11 = 2,048. 2901-2048 = 853. The next largest bit without going over is bit 9, 512.
Bit 9 = 512. 853-512 = 341. The next largest bit without going over is bit 8, 256.
Bit 8 = 256. 341-256 = 85. The next largest bit without going over is bit 6, 64.
Bit 6 = 64. 85-64 = 21. The next largest bit without going over is bit 4, 16.
Bit 4 = 16. 21-16 = 5. The next largest bit without going over is bit 2, 4.
Bit 2 = 4. 5-4 = 1. The next largest bit without going over is bit 0, 1.
Bit 0 = 1. 1-1 = 0. Since the result is zero, the decimal to binary conversion is complete.
-25,771 16 bit integer = 1001101101010101 16 bit binary
32 Bit Binary to Decimal 39,765 to a 32 bit integer.
The closest number to 39,765 without going over is bit 15's 32,768.
Bit 15 = 32,768. 39,765-32,768 = 6,997. The next largest bit without going over is bit 12, 4,096.
Bit 12 = 4,096. 6,997-4096 = 2,901. The next largest bit without going over is bit 11, 2,048.
Bill 11 = 2,048. 2901-2048 = 853. The next largest bit without going over is bit 9, 512.
Bit 9 = 512. 853-512 = 341. The next largest bit without going over is bit 8, 256.
Bit 8 = 256. 341-256 = 85. The next largest bit without going over is bit 6, 64.
Bit 6 = 64. 85-64 = 21. The next largest bit without going over is bit 4, 16.
Bit 4 = 16. 21-16 = 5. The next largest bit without going over is bit 2, 4.
Bit 2 = 4. 5-4 = 1. The next largest bit without going over is bit 0, 1.
Bit 0 = 1. 1-1 = 0. Since the result is zero, the decimal to binary conversion is complete.
-25,771 32 bit integer = 1001101101010101 32 bit binary
Using these basic steps, you can convert any binary number to decimal and vice versa whether it is 8, 16,32, or 64 bit.
Next Steps
Go to the Analog Circuits Training Lesson Series to select your next lesson. There are also many other Lesson Series on PLC Programming and Industrial Automation.