-
Binary-coded decimal
From
Wikipedia, the free encyclopedia
Jump
to:
navigation
,
search
In
computing
and
electronic
systems,
binary-coded decimal
(
BCD
) (sometimes called
natural binary-coded
decimal
,
NBCD
) is
an encoding for decimal numbers in which each
digit is represented by its own binary sequence.
Its main
virtue
is
that
it
allows
easy
conversion
to
decimal
digits
for
printing
or
display
and
faster
decimal
calculations.
Its
drawbacks
are
the
increased
complexity
of
circuits
needed
to
implement
mathematical
operations
and
a
relatively
inefficient
encoding
—
it occupies more
space than a pure binary representation.
In
BCD,
a
digit
is
usually
represented
by
four
bits
which,
in
general,
represent
the
values/digits/characters
0
–
9.
Other bit combinations are sometimes
used for a
sign
or other
indications.
Although BCD is not as
widely used as it once was, decimal
fixed-point
and
floating-point
are still
important and
continue to be used in
financial, commercial, and industrial
computing.
[1]
Modern decimal
floating-point
representations use
base-10 exponents, but not BCD encodings. Current
hardware implementations, however, convert
the
compressed
decimal
encodings to BCD internally
before carrying out computations. Software
implementations use
BCD or some other
10
n
base, depending on the
operation.
Contents
[
hide
]
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
1 Basics
2 BCD
in Electronics
3 Packed
BCD
o
3.1 Fixed-point packed
decimal
o
3.2 Higher-density encodings
4 Zoned decimal
o
4.1 EBCDIC
zoned decimal conversion table
o
4.2 Fixed-point
zoned decimal
5 IBM and
BCD
6 Addition with
BCD
7 Subtraction with
BCD
8 Background
9 Legal history
10 Comparison with pure
binary
o
10.1 Advantages
o
10.2
Disadvantages
11
Application
12
Representational variations
13 Alternative encodings
14 See also
15
References
16 External
links
[
edit
] Basics
To BCD-encode a decimal number using
the common encoding, each decimal digit is stored
in a four-bit
nibble
.
Decimal: 0 1 2 3 4
5 6 7 8 9
BCD: 0000
0001 0010 0011 0100 0101 0110 0111 1000
1001
Thus, the BCD encoding for the
number 127 would be:
0001 0010 0111
Since most computers store data in
eight-bit
bytes
, there are
two common ways of storing four-bit BCD digits in
those bytes:
?
each digit is stored in one nibble of a
byte, with the other nibble being set to all
zeros, all ones (as in
the
EBCDIC
code), or to 0011 (as
in the
ASCII
code)
?
two digits are
stored in each byte.
Unlike binary-
encoded numbers, BCD-encoded numbers can easily be
displayed by mapping each of the nibbles to a
different character. Converting a
binary-encoded number to decimal for display is
much harder, as this generally
involves
integer multiplication or divide operations. BCD
also avoids problems where fractions that can be
represented exactly in decimal cannot
be in binary (eg one-tenth).
[
edit
] BCD in
Electronics
BCD
is
very
common
in
electronic
systems
where
a
numeric
value
is
to
be
displayed,
especially
in
systems
consisting
solely of digital logic, and not
containing a microprocessor. By utilizing BCD, the
manipulation of numerical data
for display can be greatly simplified
by treating each digit as a separate single sub-
circuit. This matches much
more
closely
the
physical
reality
of
display
hardware
—
a
designer
might
choose
to
use
a
series
of
separate
identical
7-segment displays to build a metering
circuit, for example. If the numeric quantity were
stored and manipulated
as pure binary,
interfacing to such a display would require
complex circuitry. Therefore, in cases where the
calculations
are
relatively
simple
working
throughout
with
BCD
can
lead
to
a
simpler
overall
system
than
converting
to binary.
The same argument applies when hardware
of this type uses an embedded microcontroller or
other small processor.
Often,
smaller
code
results
when
representing
numbers
internally
in
BCD
format,
since
a
conversion
from
or
to
binary
representation can be expensive on such
limited processors. For these applications, some
small processors feature
BCD arithmetic
modes, which assist when writing routines that
manipulate BCD quantities.
[
edit
] Packed BCD
A widely used variation of the two-
digits-per-byte encoding is called
packed BCD
(or simply
packed decimal
). All
of
the
upper
bytes
of
a
multi-byte
word
plus
the
upper
four
bits
(
nibble
)
of
the
lowest
byte
are
used
to
store
decimal
integers. The lower four bits of the
lowest byte are used as the sign flag. As an
example, a 32 bit word contains
4 bytes
or 8 nibbles. Packed BCD uses the upper 7 nibbles
to store the integers of a 7-digit decimal value
and uses
the lowest nibble to indicate
the sign of those integers.
Standard
sign values are 1100 (
hex
C)
for positive (+) and 1101 (D) for negative
(
?
). Other allowed signs are
1010
(A)
and
1110
(E)
for
positive
and
1011
(B)
for
negative.
Some
implementations
also
provide
unsigned
BCD
values
with
a
sign nibble of 1111 (F). In packed BCD, the number
127 is represented by 0001 0010 0111 1100 (127C)
and
?
127 is
represented by 0001 0010 0111 1101
(127D).
Sign
BCD
Digit
8 4 2 1
A
B
C
D
E
F
1 0 1 0
1 0 1 1
1 1 0 0
1
1 0 1
1 1 1 0
1 1 1 1
Sign
+
?
+
?
+
+
Notes
Preferred
Preferred
Unsigned
No matter how many
bytes wide a
word
is, there
are always an even number of nibbles because each
byte has two of
them.
Therefore,
a
word
of
n
bytes
can
contain
up
to
(2
n
)
?
1
decimal
digits,
which
is
always
an
odd
number
of
digits.
A
decimal number with
d
digits requires
?(
d
+1) bytes of storage
space.
For example, a four-byte (32bit)
word can hold seven decimal digits plus a sign,
and can represent values ranging
from
±9,999,999. Thus the number
?
1,234,567 is 7 digits wide
and is encoded as:
0001 0010 0011 0100
0101 0110 0111 1101
1 2 3 4
5 6 7
?
(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).
In
contrast,
a
four-byte
binary
two's
complement
integer
can
represent
values
from
?
2,147,483,648
to
+2,147,483,647.
While packed
BCD does
not make optimal
use of storage
(about
1
/
6
of
the memory
used
is
wasted),
conversion to
ASCII
,
EBCDIC
, or the various
encodings of
Unicode
is
still trivial, as no arithmetic operations are
required. The extra
storage
requirements are usually
offset
by
the need for the accuracy
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.
[
edit
] Fixed-
point packed decimal
Fixed-
point
decimal numbers are supported by
some programming languages (such as
COBOL
and
PL/I
), and provide an
implicit decimal point in front of one
of the digits. For example, a packed decimal value
encoded with the bytes
12 34 56 7C
represents the fixed-point value +1,234.567 when
the implied decimal point is located between the
4th
and 5th digits.
12 34 56
7C
12 34.56 7+
[
edit
] Higher-
density encodings
If
a
decimal
digit
requires
four
bits,
then
three
decimal
digits
require
12
bits.
However,
since
2
10
(1,024)
is
greater
than
10
3
(1,000), if three
decimal digits are encoded together, only 10 bits
are needed. Two such encodings are
Chen-Ho encoding
and
Densely Packed Decimal
. The
latter has the advantage that subsets of the
encoding encode two
digits in the
optimal 7 bits and one digit in 4 bits, as in
regular BCD.
[
edit
] Zoned
decimal
Some implementations (notably
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
digit. EBCDIC systems use a zone value
of 1111 (hex F); this yields bytes in the range F0
to F9 (hex), which are
the
EBCDIC
codes for the
characters
ASCII
systems use
a zone value of 0011 (hex 3),
giving
character codes 30 to 39 (hex).
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). Thus a zoned decimal value encoded
as the hex bytes F1 F2 D3 represents
the signed decimal value
?
123:
F1 F2 D3
1 2
?
3
[
edit
] EBCDIC
zoned decimal conversion table
BCD
Digit
0+
1+
2+
3+
4+
5+
6+
7+
8+
9+
0
?
1
?
2
?
3
?
4
?
5
?
6
?
7
?
8
?
{
(*)
A
B
C
D
E
F
G
H
I
} (*)
J
K
L
M
N
O
P
Q
EBCDIC Character
~ (*)
s
t
u
v
w
x
y
z
^ (*)
(*)
S
T
U
V
W
X
Y
Z
C0
C1
C2
C3
C4
C5
C6
C7
C8
C9
D0
D1
D2
D3
D4
D5
D6
D7
D8
Hexadecimal
A0
A1
A2
A3
A4
A5
A6
A7
A8
A9
B0
B1
B2
B3
B4
B5
B6
B7
B8
E0
E1
E2
E3
E4
E5
E6
E7
E8
E9