# Binary coded decimal practice

Most implementations are big endian , i. The lower nibble of the rightmost byte is usually used as the sign flag, although some unsigned representations lack a sign flag. As an example, a 4-byte value consists of 8 nibbles, wherein the upper 7 nibbles store the digits of a 7-digit decimal value and the lowest nibble indicates the sign of the decimal integer value. Other allowed signs are A and E for positive and B for negative. Most implementations also provide unsigned BCD values with a sign nibble of F.

Burroughs systems used D for negative, and any other value is considered a positive sign value the processors will normalize a positive sign to C. No matter how many bytes wide a word is, there are always an even number of nibbles because each byte has two of them. Note that, like character strings, the first byte of the packed decimal — with the most significant two digits — is usually stored in the lowest address in memory, independent of the endianness of the machine.

The extra storage requirements are usually offset by the need for the accuracy and compatibility with calculator or hand calculation that fixed-point decimal arithmetic provides. Denser packings of BCD exist which avoid the storage penalty and also need no arithmetic operations for common conversions. Ten's complement representations for negative numbers offer an alternative approach to encoding the sign of packed and other BCD numbers. In this case, positive numbers always have a most significant digit between 0 and 4 inclusive , while negative numbers are represented by the 10's complement of the corresponding positive number.

As a result, this system allows for, a bit packed BCD numbers to range from ,, to 49,,, and -1 is represented as As with two's complement binary numbers, the range is not symmetric about zero. These languages allow the programmer to specify an implicit decimal point in front of one of the digits. The decimal point is not actually stored in memory, as the packed BCD storage format does not provide for it.

Its location is simply known to the compiler and the generated code acts accordingly for the various arithmetic operations. If a decimal digit requires four bits, then three decimal digits require 12 bits. However, since 2 10 1, is greater than 10 3 1, , if three decimal digits are encoded together, only 10 bits are needed. The latter has the advantage that subsets of the encoding encode two digits in the optimal seven bits and one digit in four bits, as in regular BCD.

Some implementations, for example IBM mainframe systems, support zoned decimal numeric representations. Each decimal digit is stored in one byte, with the lower four bits encoding the digit in BCD form. The upper four bits, called the "zone" bits, are usually set to a fixed value so that the byte holds a character value corresponding to the digit.

For signed zoned decimal values, the rightmost least significant zone nibble holds the sign digit, which is the same set of values that are used for signed packed decimal numbers see above.

These characters vary depending on the local character code page setting. The IBM series are character-addressable machines, each location being six bits labeled B, A, 8, 4, 2 and 1, plus an odd parity check bit C and a word mark bit M. For encoding digits 1 through 9 , B and A are zero and the digit value represented by standard 4-bit BCD in bits 8 through 1. For most other characters bits B and A are derived simply from the "12", "11", and "0" "zone punches" in the punched card character code, and bits 8 through 1 from the 1 through 9 punches.

A "12 zone" punch set both B and A , an "11 zone" set B , and a "0 zone" a 0 punch combined with any others set A. Thus the letter A , which is 12,1 in the punched card format, is encoded B,A,1. This allows the circuitry to convert between the punched card format and the internal storage format to be very simple with only a few special cases. One important special case is digit 0 , represented by a lone 0 punch in the card, and 8,2 in core memory.

The memory of the IBM is organized into 6-bit addressable digits, the usual 8, 4, 2, 1 plus F , used as a flag bit and C , an odd parity check bit. BCD alphamerics are encoded using digit pairs, with the "zone" in the even-addressed digit and the "digit" in the odd-addressed digit, the "zone" being related to the 12 , 11 , and 0 "zone punches" as in the series.

A variable length Packed BCD numeric data type is also implemented, providing machine instructions that perform arithmetic directly on packed decimal data. All of these are used within hardware registers and processing units, and in software.

The MicroVAX and later VAX implementations dropped this ability from the CPU but retained code compatibility with earlier machines by implementing the missing instructions in an operating system-supplied software library. This is invoked automatically via exception handling when the no longer implemented instructions are encountered, so that programs using them can execute without modification on the newer machines. The Intel x86 architecture supports a unique digit ten-byte BCD format that can be loaded into and stored from the floating point registers, and computations can be performed there.

The Motorola series had BCD instructions. In more recent computers such capabilities are almost always implemented in software rather than the CPU's instruction set, but BCD numeric data is still extremely common in commercial and financial applications. There are tricks for implementing packed BCD and zoned decimal add or subtract operations using short but difficult to understand sequences of word-parallel logic and binary arithmetic operations. Conversion of the simple sum of two digits can be done by adding 6 that is, 16 — 10 when the five-bit result of adding a pair of digits has a value greater than 9.

As an example, a 4-byte value consists of 8 nibbles, wherein the upper 7 nibbles store the digits of a 7-digit decimal value and the lowest nibble indicates the sign of the decimal integer value. Other allowed signs are A and E for positive and B for negative. Most implementations also provide unsigned BCD values with a sign nibble of F. Burroughs systems used D for negative, and any other value is considered a positive sign value the processors will normalize a positive sign to C.

No matter how many bytes wide a word is, there are always an even number of nibbles because each byte has two of them. Note that, like character strings, the first byte of the packed decimal — with the most significant two digits — is usually stored in the lowest address in memory, independent of the endianness of the machine. The extra storage requirements are usually offset by the need for the accuracy and compatibility with calculator or hand calculation that fixed-point decimal arithmetic provides.

Denser packings of BCD exist which avoid the storage penalty and also need no arithmetic operations for common conversions. Ten's complement representations for negative numbers offer an alternative approach to encoding the sign of packed and other BCD numbers. In this case, positive numbers always have a most significant digit between 0 and 4 inclusive , while negative numbers are represented by the 10's complement of the corresponding positive number.

As a result, this system allows for, a bit packed BCD numbers to range from ,, to 49,,, and -1 is represented as As with two's complement binary numbers, the range is not symmetric about zero. These languages allow the programmer to specify an implicit decimal point in front of one of the digits.

The decimal point is not actually stored in memory, as the packed BCD storage format does not provide for it. Its location is simply known to the compiler and the generated code acts accordingly for the various arithmetic operations. If a decimal digit requires four bits, then three decimal digits require 12 bits. However, since 2 10 1, is greater than 10 3 1, , if three decimal digits are encoded together, only 10 bits are needed. The latter has the advantage that subsets of the encoding encode two digits in the optimal seven bits and one digit in four bits, as in regular BCD.

Some implementations, for example IBM mainframe systems, support zoned decimal numeric representations. Each decimal digit is stored in one byte, with the lower four bits encoding the digit in BCD form. The upper four bits, called the "zone" bits, are usually set to a fixed value so that the byte holds a character value corresponding to the digit.

For signed zoned decimal values, the rightmost least significant zone nibble holds the sign digit, which is the same set of values that are used for signed packed decimal numbers see above. These characters vary depending on the local character code page setting.

The IBM series are character-addressable machines, each location being six bits labeled B, A, 8, 4, 2 and 1, plus an odd parity check bit C and a word mark bit M. For encoding digits 1 through 9 , B and A are zero and the digit value represented by standard 4-bit BCD in bits 8 through 1. For most other characters bits B and A are derived simply from the "12", "11", and "0" "zone punches" in the punched card character code, and bits 8 through 1 from the 1 through 9 punches.

A "12 zone" punch set both B and A , an "11 zone" set B , and a "0 zone" a 0 punch combined with any others set A. Thus the letter A , which is 12,1 in the punched card format, is encoded B,A,1. This allows the circuitry to convert between the punched card format and the internal storage format to be very simple with only a few special cases.

One important special case is digit 0 , represented by a lone 0 punch in the card, and 8,2 in core memory. The memory of the IBM is organized into 6-bit addressable digits, the usual 8, 4, 2, 1 plus F , used as a flag bit and C , an odd parity check bit. BCD alphamerics are encoded using digit pairs, with the "zone" in the even-addressed digit and the "digit" in the odd-addressed digit, the "zone" being related to the 12 , 11 , and 0 "zone punches" as in the series.

A variable length Packed BCD numeric data type is also implemented, providing machine instructions that perform arithmetic directly on packed decimal data. All of these are used within hardware registers and processing units, and in software.

The MicroVAX and later VAX implementations dropped this ability from the CPU but retained code compatibility with earlier machines by implementing the missing instructions in an operating system-supplied software library. This is invoked automatically via exception handling when the no longer implemented instructions are encountered, so that programs using them can execute without modification on the newer machines. The Intel x86 architecture supports a unique digit ten-byte BCD format that can be loaded into and stored from the floating point registers, and computations can be performed there.

The Motorola series had BCD instructions. In more recent computers such capabilities are almost always implemented in software rather than the CPU's instruction set, but BCD numeric data is still extremely common in commercial and financial applications. There are tricks for implementing packed BCD and zoned decimal add or subtract operations using short but difficult to understand sequences of word-parallel logic and binary arithmetic operations.

Conversion of the simple sum of two digits can be done by adding 6 that is, 16 — 10 when the five-bit result of adding a pair of digits has a value greater than 9. Note that is the binary, not decimal, representation of the desired result.

Also note that it cannot fit in a 4-bit number. The only difference is that if bit 0 is set it is complex. Bit 0 of the first byte of the real part and the first byte of the complex part will both be set if the number is complex.

Remember that the TI86 displays Complex numbers in parenthesis: Real part , Complex part. Here's how a complex number would be displayed like the above numbers. I saw an example of this by Joshua Seagoe called AtoF. It will help solidify your understanding of the format of these numbers. Find where the decimal point is and count left or right until you have the decimal in the right place. Put the digits in BCD through the rest of the OP1 register any digits over the allowed 14 are left out.