Page 404 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 404
Appendix 1
Number Representations and Binary Arithmetic
This appendix contains material needed for the rest of the book that is usually found in
an introductory course on logic design. The two topics are the representation of integers
with different bases and binary arithmetic with unsigned and two's-complement numbers,
Al.l Number Representations
If b and m are positive integers, and if N is a nonnegative integer less than bm, then N
can be expressed uniquely with a finite series
N = Cm_i * b^i + Cnv_ 2 * b^ + + CQ * b 0 (1)
where 0 s c i < b for 0 s i =s m - 1. The integer b is called the base or radix and
the sequence c m_] ... CQ is called a base-b representation of N. If b = 2, the digits c m _j
. . . CQ are called bits, and the sequence c m _i . . . CQ is called an m-bit binary
representation of N. Binary, octal (base 8) and hexadecimal (base 16) representations, as
well as the ordinary decimal representation, are the ones used when discussing computers
with hexadecimal being particularly useful with microcontrollers. When the hexadecimal
representation is used, the numbers 10 through 15 are replaced by the letters A through
F, respectively, so that hexadecimal sequences such as 112 will be unambiguous without
the use of commas (e.g., without commas, 112 could be interpreted as 1,1,2, or 11,2, or
1,12, which are the decimal numbers 274, 178, or 28, respectively). Unless stated
otherwise, all numbers will be given in decimal and, when confusion is possible, a
binary sequence will be preceded by a % and a hexadecimal sequence by a $. For
example, 110 denotes the integer one hundred and ten, %110 denotes the integer six, and
$110 denotes the integer two hundred seventy-two.
To go from a base-b representation of N to its decimal representation, one has only
to use (1). To go from decimal to base b, notice that
1 11 1
N = C 0 + ci * b + ... + Cm_i * b *-
= GO + b * (GI + b * (02 + ... ) ... )
so that dividing N by b yields a remainder CQ- Dividing the quotient by b again yields a
remainder equal to GI and so on. Although this is a fairly convenient method with a
calculator, we shall see later that there is a more computationally efficient way to do it
with an 8-bit microprocessor.
To go from binary to hexadecimal or octal, one only needs to generalize from the
example:
%1101 0011 1011 = $D3B
Thus, to go from binary to hexadecimal, one first partitions the binary representation
into groups of four Os and Is from right to left, adding leading Os to get an exact
multiple of four Os and Is, and then represents each group of four Os and Is by its
hexadecimal equivalent. To go from hexadecimal to binary is just the reverse of this
process.
381

