The usual way of expressing a decimal number in terms of a binary number is known as pure binary coding and is discussed in the Number Systems section.
A number of other techniques can be used to represent a decimal number. These are summarised below.
In the 8421 Binary Coded Decimal (BCD) representation each decimal digit is converted to its 4-bit pure binary equivalent.
For example: 57dec = 0101 0111bcd
Addition is analogous to decimal addition with normal binary addition taking place from right to left. For example,
6 | 0110 | BCD for 6 | 42 | 0100 0010 | BCD for 42 | |
+3 | 0011 | BCD for 3 | +27 | 0010 0111 | BCD for 27 | |
____ | __________ | |||||
1001 | BCD for 9 | 0110 1001 | BCD for 69 |
Where the result of any addition exceeds 9(1001) then six (0110) must be added to the sum to account for the six invalid BCD codes that are available with a 4-bit number.
This is illustrated in the example below
8 | 1001 | BCD for 8 |
+7 | 0111 | BCD for 7 |
_____ | ||
1111 | exceeds 9 (1001) so | |
0110 | add six (0110) | |
__________ | ||
0001 0101 | BCD for 15 |
Note that in the last example the 1 that carried forward from the first group of 4 bits has made a new 4-bit number and so represents the "1" in "15".
In the examples above the BCD numbers are split at every 4-bit boundary to make reading them easier. This is not necessary when writing a BCD number down.
This coding is an example of a binary coded (each decimal number maps to four bits) weighted (each bit represents a number, 1, 2, 4, etc.) code.
The 4221 BCD code is another binary coded decimal code where each bit is weighted by 4, 2, 2 and 1 respectively.
Unlike BCD coding there are no invalid representations.
The decimal numbers 0 to 9 have the following 4221 equivalents
Decimal | 4221 | 1's complement |
---|---|---|
0 | 0000 | 1111 |
1 | 0001 | 1110 |
2 | 0010 | 1101 |
3 | 0011 | 1100 |
4 | 1000 | 0111 |
5 | 0111 | 1000 |
6 | 1100 | 0011 |
7 | 1101 | 0010 |
8 | 1110 | 0001 |
9 | 1111 | 0000 |
the 1's complement of a 4221 representation is important in decimal arithmetic. In forming the code remember the following rules
Gray coding is an important code and is used for its speed, it is also relatively free from errors.
In pure binary coding or 8421 BCD then counting from 7 (0111) to 8 (1000) requires 4 bits to be changed simultaneously.
If this does not happen then various numbers could be momentarily generated during the transition so creating spurious numbers which could be read.
Gray coding avoids this since only one bit changes between subsequent numbers.
To construct the code there are two simple rules. First start with all 0s and then proceed by changing the least significant bit (lsb) which will bring about a new state.
The first 16 Gray coded numbers are indicated below.
Decimal | Gray Code |
---|---|
0 | 0000 |
1 | 0001 |
2 | 0011 |
3 | 0010 |
4 | 0110 |
5 | 0111 |
6 | 0101 |
7 | 0100 |
8 | 1100 |
9 | 1101 |
10 | 1111 |
11 | 1110 |
12 | 1010 |
13 | 1011 |
14 | 1001 |
15 | 1000 |
To convert a Gray-coded number to binary then follow this method
Gray coding is a non-BCD, non-weighted reflected binary code.
bit0 = %I1.0 bit1 = %I1.1
bit2 = %I1.2
bit3 = %I1.3
bit4 = %I1.4 bit5 = %I1.5
bit6 = %I1.6 bit7 = %I1.7
bit8 = %I0.0 bit9 = %I0.1 bit10 = %I0.2 bit11 = %I0.3 IEC !
bit12 = %I0.4 bit13 =
%I0.5 bit14 = %I0.6 bit15 = %I0.7
The M0.0 stay in the 8th place of MW0. Siemens does
so to be IEC conform... accordance Conformity
correspondence appropriateness
parity
WORD in data block DB1 So address is: %DB1.DBW0
0.bit is %DB1.DBX1.0 7.bit
is %DB1.DBX1.7 8.bit is %DB1.DBX0.0
15.bit is %DB1.DBX0.7
operator "=" means data contain
Add 1,
%MD200 (32bit) = %MW200 (16bit) + %MW202 (16bit) = %MB200 (8bit) + %MB201 (8bit)
+ %MB202 (8bit) + %MB203 (8bit)
Add 2,
%DB1.DBW50 (16bit) = %DB1.DBB50 (8bit) + %DB1.DBB51 (8bit)
MWx = MBx + MB(x+1)
MDx = MWx + MW(x+2) =MBx + MB(x+1)+MB(x+2) + MB(x+3)
DBx = DBBx + DBB(x+1)
DBx = DBWx + DBW(x+2) = DBBx + DBB(x+1)+DBB(x+2) + DBB(x+3)