Number Systems
Number Systems
and
DATA
REPRESENTATION
for
COMPUTERS
05 March 2008
Number Systems and Data Representation 2
Table of Contents
Table of Contents .............................................................................................................2
Prologue .............................................................................................................................8
Number System Bases Introduction............................................................................12
Base 60 Number System ..........................................................................................12
Base 12 Number System ..........................................................................................14
Base 6 (Senary) Number System ............................................................................15
Base 3 (Trinary) Number System ............................................................................16
Base 1 Number System.............................................................................................17
Other Bases.................................................................................................................18
Know Nothing ..............................................................................................................18
Indexes and Subscripts .................................................................................................19
Common Number Systems for Computers ................................................................20
Decimal Numbers .......................................................................................................20
Binary Numbers ..........................................................................................................20
Hexadecimal Numbers ..............................................................................................20
Octal Numbers ............................................................................................................21
Numbers Names .........................................................................................................21
Decimal Numbers 0 through 15 and Equivalent Hexadecimal and Binary
Numbers ..........................................................................................................................23
Memorization...............................................................................................................23
Pattern Recognition Method. ....................................................................................24
Position Value Addition..............................................................................................24
Counting Method ........................................................................................................24
Binary Addition ........................................................................................................25
Sample Binary Addition Problems....................................................................25
Abstract Counting in Base 4 ..................................................................................27
Conversion Between Binary and Hexadecimal .........................................................29
Data Representation......................................................................................................32
A Little Byte of History................................................................................................32
Character Codes.........................................................................................................33
EBCDIC, US-ASCII, and UNICODE Character Codes ........................................36
Jubilation Code and Gran Zeff Code.......................................................................38
Additional Information About Codes........................................................................41
Positional Number Systems .........................................................................................42
Decimal Number System...........................................................................................42
Algebra Review of Exponents ..................................................................................43
Positional Number System General Concept ........................................................43
Binary Number System..............................................................................................44
Hexadecimal Number System..................................................................................46
Octal Number System................................................................................................48
Fun Questions .............................................................................................................48
Sexagesimal or Sexagenary System ......................................................................48
Nothing Matters...........................................................................................................49
Conversion to Decimal ..................................................................................................50
Number Systems and Data Representation 3
Overflow..................................................................................................................120
Underflow ...............................................................................................................120
Arithmetic Examples ................................................................................................120
International Notation Systems for Arithmetic......................................................122
Manual Multiplication in Germany ......................................................................122
Manual Division in Germany ...............................................................................127
Complement Arithmetic ...............................................................................................129
Introduction to Complement Arithmetic.................................................................129
Base 10 Complement Arithmetic ...........................................................................129
Base 16 (Hexadecimal) Complement Arithmetic.................................................131
Base 2 (Binary) Complement Arithmetic...............................................................133
Complement Arithmetic Problems ......................................................................134
Fast Two’s Complement ......................................................................................139
Fast Two’s Complement Problems ....................................................................139
Binary Coded Decimal Excess-3 Notation...............................................................141
Addition with BCD XS-3...........................................................................................141
D=3 .............................................................................................................................142
Code Overflow ..........................................................................................................142
V=13 ...........................................................................................................................142
Code Overflow ...............................................................................................142
BCD XS-3 Addition Examples with Input Carry = 0 .........................................143
BCD XS-3 Addition Examples with Input Carry = 1 .........................................145
Subtraction with BCD XS-3.....................................................................................146
BCD XS-3 Subtraction Example Same Sign and |X| = |Y| .............................148
BCD XS-3 Subtraction Example Same Sign and |X| < |Y| .............................148
BCD XS-3 Subtraction Example Same Sign and |X| > |Y| .............................149
Binary Multiplication .....................................................................................................151
Binary Multiplication .................................................................................................153
Hexadecimal Multiplication.........................................................................................154
Hexadecimal Multiplication Problem .....................................................................155
Fixed Point Binary Division .........................................................................................156
Fixed Point Binary Division Problems ...................................................................156
Common Fractions in Different Bases ......................................................................157
Prime Numbers and Number System Base .............................................................159
Prime Number Problems .........................................................................................163
Floating Point Arithmetic .............................................................................................164
Fixed Point Numbers with Fractions ......................................................................164
Scientific Notation.....................................................................................................164
Scientific Notation Conversion Problems ..........................................................165
Scientific Notation Multiplication .........................................................................165
Scientific Notation Multiplication Problems ...................................................166
Scientific Notation Normalization........................................................................166
Scientific Notation Division ..................................................................................166
Scientific Notation Addition and Subtraction.....................................................166
Engineering Notation ...............................................................................................167
Floating Point Notation ............................................................................................167
Number Systems and Data Representation 6
Prologue
This tutorial is an introduction to number systems and representation of data for
computers. The original work by Mr. Bob Ralph of Fayetteville Technical Community
College served soldiers at Ft. Bragg and airmen at Pope AFB. Drew McCall III
motivated the first expansion of this work (originally 13 pages) through his 2002 High
School Science Fair project on BCD arithmetic and prime numbers.
It is important how we represent our data, whether it is a message in words,
sound, an image, color, smell, numbers, or something else. The representation we choose
affects our ability to store and manipulate ideas efficiently with an acceptably accurate
approximation. The representation we choose can be a catalyst for new interpretation and
discovery.
The primary context for this tutorial is the binary digital computer. Some
discussion is included to spark the imagination for those who may want to go beyond this
immediate context.
A binary digital computer must represent all data internally as patterns of zeros
and ones. Any data that has only two values of interest can be represented by a single
binary digit. Examples:
Table 1. Representing Two States as a Binary Number
Numbers: {0, 1}
Sign of a Number: {Plus, Minus}
Sex: {Male, Female}
Directions: {Right, Left}, {Up, Down}, {In, Out}
Decision, Logic: {Yes, No}, {True, False}
Switch Position: {On, Off}
Voltages in a Computer:
{+5 V DC, 0 V DC}, {High, Low}
CD-ROM {Pit, Land}
Pit = low intensity reflection = 0,
Land = high intensity reflection = 1
Spin Direction: {Clockwise, Counterclockwise}
Magnetic Domain Orientation
{North-South, South-North}
The choice of representation on a particular computer is a shared responsibility
between the hardware engineer and the programmer. Standards have been established to
make data files and programs portable, and results of computations predictable. This
tutorial presents basic concepts at an applied level.
People in Information Science and Computer Engineering need to know about
data representation to understand compatibility issues of data from different sources.
This is necessary both to know what to expect on a particular machine for performance,
and also to know what to expect when importing or exporting data.
Number Systems and Data Representation 9
Most serious data processing is done by business on IBM computers which use
EBCDIC format for character data. Other computers usually use ASCII, which is not
directly compatible with EBCDIC. ASCII has been extended to accommodate other
European languages. UNICODE is being defined to accommodate many additional
character sets. The most important goal was to represent languages such as Japanese,
Korean, Chinese, Hebrew, Greek, Arabic, and symbols used in mathematics, science, and
engineering. Ancient languages and localized languages are now also being added to
UNICODE.
Differences exist in number formats between processors. This is most
dramatically seen in the variety of floating point formats. IBM pioneered the
Hexadecimal Floating Point (HFP) format with Excess notation. Non-IBM processors
adopted a simpler Binary Floating Point (BFP) format. As memory costs decreased and
processor speeds increased, the innovation of Hexadecimal Floating Point format became
less important. The lower cost of non-IBM workstations for scientific and engineering
work led to standardization efforts by the Institute of Electrical and Electronics Engineers
(IEEE). To remain competitive, IBM has implemented both the Hexadecimal and the
IEEE Binary Floating Point formats on its IBM System/390.
Not only are data formats different, the order of storage of multi-byte data is
different. This is a function of how a processor is designed to fetch a block of data from
memory most efficiently. The two primary methods are called Big Endian and Little
Endian. Machines with Big Endian (Big End In) architecture store the Most Significant
Byte (MSB) in the lowest address in memory. Little Endian (Little End In) machines
store the Least Significant Byte (LSB) in the lowest address in memory. There are other
methods. One is called Middle Endian. This affects importing and exporting data
between computers, protocol numbers and port numbers on the Internet, design of display
device interfaces and drivers, character code representation standards, and storage
formats for integers and floating point numbers. For data, this affects operations of
comparisons, selection, searching, sorting, and other operations. When you grow from a
small to a medium size company and switch to an IBM mainframe, these are issues that
even the non-technical people need to know exist so that conversion efforts can be
planned, scheduled, funded, and staffed to make the conversion go smoothly.
We have long had to live with considering the impact of working with different
units of measure and systems of units. Anyone who has spent time in a kitchen knows
this. Working with a variety of units of measure has also been the bane of scientists,
engineers, and technicians. The surveyor and the sailor have historically had to cope with
a variety of units of measure in practical life just to do a basic job to survive. (It is
actually a conspiracy just to make life hard for students, but do not let the secret out.)
The different ways of measuring an angle illustrate that we already are used to
representing the same data in different ways. Measurement of angles is important to
sailors, explorers, space flight, surveyors, artillerymen, engineers, and students.
Number Systems and Data Representation 10
demanding than in other fields. Consequently, special hardware features have been
designed to make these computations faster, at additional cost of the hardware. By
reducing the time required for doing computations, special hardware reduces the overall
cost of running these programs and makes software responsive enough to accomplish
tasks that could not be done in years past. The Institute of Electrical and Electronics
Engineers (IEEE) has defined standards for representing numerical data and
computations. These standards promote portability and predictability of computations
between different computers and computer languages.
If you find the concepts in this tutorial fascinating, your options for future study
could lead you to computer science, computer engineering, electrical engineering,
numerical analysis, number theory, abstract algebra, and cryptology. An article by
Dunne that includes history of numbers and aids for calculation can be found on the
Internet. 1
For the serious computer science major, an extraordinary reference is Donald E. Knuth’s
Volume 2, “Seminumerical Algorithms” of The Art of Computer Programming.2
1
Paul E. Dunne, “Mechanical Aids to Computation and the Development of Algorithms”,
http://www.csc.liv.ac.uk/~ped/teachadmin/histsci/htmlform/lect2.html visited 31 December 2007
2
Donald E. Knuth, “Seminumerical Algorithms”, Vol. 2 of The Art of Computer Programming, Addis on
Wesley (1971).
Number Systems and Data Representation 12
When we write base 60 numbers, we write the coefficient using our decimal
numbers, followed by a symbol for the place value. For example, we write 127° 18’ 23”
to record 127 degrees, 18 minutes, and 23 seconds. The small circle for degrees, the
apostrophe for minutes, and the quote symbol for seconds, perform the function of
identifying place value in base 60 arithmetic. We are limited by not having 60 different
number symbols.
In the Babylonian system of weights and coinage, 1 talent = 60 minae, 1 minae =
60 shekels. 3 The Oxford English Dictionary gives a different account, stating that it was
the Greek system that used 1 talent = 60 minae. 4 There 60 drops to a teaspoon in liquid
measure used in cooking. 5 More history on the introduction of the sexagesimal (base 60)
system by Babylon, 1900 BC – 1800 BC, can be found on the Internet. 6
The Chinese calendar names years in a manner that produces a 60 year cycle,
called a “Sexagenary cycle”. The following is Table 6.1.1 from Explanatory Supplement
to the Astronomical Almanac.7 For each new year, advance from the old year by one
Celestial Stem and by one Earthly Branch.
Table 4. Chinese Sexagenary Cycle of Days and Years
3
Karl Menninger, Number Words and Number Symbols: A Cultural History of Numbers, page 162, Dover
Publications (1992).
4
Oxford English Dictionary, Second Edition (1989), CD-ROM Version 3.00, Oxford University Press
(2002).
5
Irma S. Rombauer and Marion Rombauer Becker, Joy of Cooking, page 546, Bobbs-Merrill Company
(1964).
6
J J O'Connor and E F Robertson, “Babylonian numerals”, School of Mathematics and Statistics,
University of St. Andrews, Scotland (December 2000).
http://www-groups.dcs.st-and.ac.uk/~history/HistTopics/Babylonian_numerals.html, visited 17 December
2007.
7
Explanatory Supplement to the Astronomical Almanac, P. Kenneth Seidelmann, editor, University
Science Books, Sausalito, CA 94965, as quoted by L. E. Doggett, “Calendars”,
http://astro.nmsu.edu/~lhuber/leaphist.html, 12 April 2002.
Number Systems and Data Representation 14
Year Names
1. jia-zi 2. yi-chou 3. bing-yin 4. ding-mao 5. wu-chen 6. ji-si
7. geng-wu 8. xin-wei 9. ren-shen 10. gui-you 11. jia-xu 12. yi-hai
13. bing-zi 14. ding-chou 15. wu-yin 16. ji-mao 17. geng-chen 18. xin-si
19. ren-wu 20. gui-wei 21. jia-shen 22. yi-you 23. bing-xu 24. ding-hai
25. wu-zi 26. ji-chou 27. geng-yin 28. xin-mao 29. ren-chen 30. gui-si
31. jia-wu 32. yi-wei 33. bing-shen 34. ding-you 35. wu-xu 36. ji-hai
37. geng-zi 38. xin-chou 39. ren-yin 40. gui-mao 41. jia-chen 42. yi-si
43. bing-wu 44. ding-wei 45. wu-shen 46. ji-you 47. geng-xu 48. xin-hai
49. ren-zi 50. gui-chou 51. jia-yin 52. yi-mao 53. bing-chen 54. ding-si
55. wu-wu 56. ji-wei 57. geng-shen 58. xin-you 59. ren-xu 60. gui-hai
Most people have 4 fingers and 1 thumb on each hand. Each finger has 3
knuckles and three bones. If you use your thumb as a pointer, you can count to 12 on
each hand by pointing either to knuckles or bones. With two hands, you can count to 24,
the number of hours per day.
The number 12 is evenly divisible by 2, 3, 4, 6, and 12. Common fractions are
exactly represented in base 12.
In Western music of 2007 A.D., the “equal temperament” scale has 12 divisions
(called “half-steps”) per octave (doubling of frequency). These half-steps are equally
spaced logarithmically. In this system, the frequency multipliers within an octave of
n
some reference frequency A takes the form of ( A x 2 12 ) where 0 ≤ n ≤ 11 . The
symbols assigned to the different notes within an octave are {A, A#, B, C, C#, D, D#, E,
F, F#, G, G# }. The reference frequency for A is 440 Hertz (abbreviated by “Hz”). The
vertical position of a note on a music score establishes the frequency value of the note.
Brilliant musicians understood the goal of assigning notes before the
mathematical concepts caught up. Consequently, the musicians approximated the exact
intervals by simpler rational numbers, from which terminology for notes was established.
Table 6. Common Fractions in Base 12
1/2 0.6
1/3 0.4
1/4 0.3
1/6 0.2
1/8 0.12
1/12 0.1
N O P Q R S T U V W X Y Z
10 111 0110 1101 010 000 1 001 0001 011 1001 1011 1100
Let the space between symbols in Morse Code be represented by the number 2.
Then the message, “International Morse Code” is given by
“0021021202010210201212002111210201201002211211120102000202210102111210020” .
Morse Code was designed for fast transmission using American English. The most
frequently used letters take the shortest time to send. The 0 represents a very short
transmission, called a “dit”. The 1 represents a longer transmission, called a “dah”.
Base 3 is used in the construction of the “Cantor set”. 9 Begin with the closed unit
interval [0,1]. By “closed”, we mean that the end points of the interval are included in
the interval. The Cantor set is constructed by iteratively partitioning each closed interval
into three equal pieces and removing the middle piece, leaving the two end pieces as
closed sets. The end points of each interval can be written as fractions using base 3. The
first 4 intervals are shown below.
8
J.R.Newman, The World of Mathematics, volume 1., cited by Alexander Bogomolny, “Addition and
Multiplication Tables in Various Bases” from Interactive Mathematics Miscellany and Puzzles (2002).
http://www.cut-the-knot.org/blue/SysTable.shtml visited 04 June 2002.
9
Adrian Ocneanu, Construction of the Cantor Set, Math 401 Class Notes, The Pennsylvania State
University (06 October 1986).
Number Systems and Data Representation 17
Aside: Observing that when the digit “1” appears, it only occurs as the right- most
non-zero digit. This allows substitution of “1” with “02” where “2” repeats infinitely to
the right. This transforms the base 3 representation to a base 2 representation. You will
see this trick used in Math class when studying the properties of Real numbers.
Base 1 Number System
Yes! There is a base 1 number system. A base 1 system uses only one symbol.
You can choose any symbol.
Suppose you choose a pebble as your symbol. A shepherd can count the number
of sheep entering the coral at night by putting a pebble in a leather bag for each sheep as
it enters the coral. The shepherd can determine if any sheep are missing in the morning
as they depart the coral by removing a pebble from the leather bag for each sheep that
exits. The number of missing sheep is the number of pebbles remaining in the leather
bag after all the sheep have departed.
For a more enduring record, you can cut a line into a piece of wood or even a
flattened piece of bark rather than put a pebble into a bag. For example,
in base 1 is equal to 7 in base 10. If you want a copy of the record, slice the bark in half.
10
“Irrational-Base Number System”, everything2 (a discussion forum on the Internet), (15 June 2000)
http://everything2.com/index.pl?node_id=604415 vis ited 17 December 2007
Number Systems and Data Representation 19
A0 = 6
A1 = 9
A2 = 4
Then A2 A1 A0 = 496.
Number Systems and Data Representation 20
Sometimes, these labels take on special meanings beyond just being an index or
locator. In mathematics, it is common for a superscript in position w to represent a
power. For example , A3 means 3 copies of A are multiplied together. A3 = A A A.
When A = 2, then 23 = 2 x 2 x 2 = 8. We will use this meaning.
of copying a long binary number, it is much easier to write the number using
hexadecimal. It uses less space, it is easier to remember, and you make fewer mistakes.
You may sometimes see hexadecimal numbers flash before your eyes shortly after you
turn your computer on and the computer performs its power-on self test.
The electronic computer is not the first beneficiary of the hexadecimal system.
The English system of weights used the Roman ounce: 1 pound = 16 ounces (weight), 1
ounce (weight) = 16 drams, 1 dram = 3 scruples. 1 gallon = 16 cups, 1 cup = 16
tablespoons. 1 pint (liquid measure) = 16 fluid ounces. 1 quart = quarter of a gallon = 4
cups. 1 peck = 16 pints (dry measure). 1 ounce (liquid measure) of water at standard
temperature and pressure weighs 1 ounce (weight). 11 (Rombauer, pg. 546) A pint, a
pound, the world ‘round.
An hexadecimal number is represented with a subscript of 16 after the
hexadecimal number, the letters H or h after the hexadecimal number, or the symbols Ox
before the hexadecimal number. The “O” in “Ox” is the capital letter O (Oh, not zero).
The non-subscript forms are useful for high-speed display. It is useful to leave a space
between every 4th hexadecimal digit, starting from the right end.
Examples: B4AD16 B4ADH B4ADh OxB4AD
Octal Numbers
Programmers and technicians sometimes use a coding sys tem called octal (with a
base of 8). To remember that octal means 8, think of an octopus, which has 8 legs, or an
octogon which has 8 sides (3 more than the Pentagon). The digits are 0, 1, 2, 3, 4, 5, 6,
and 7. An octal number takes exactly 3 bits to represent. Octal is found on special
purpose computing systems, including some numerically controlled machines and
embedded processors.
An octal number is represented with a subscript of 8 or the capital letter Q (for
Quaternion) after the octal number, or the capital letter O (Oh, not zero) before the octal
number. The non-subscript form is useful for high-speed display. It is useful to leave a
space between every 4th digit, starting from the right end.
Examples: 42738 4273Q O4273
A Quaternion is a generalization of a complex number, introduced by Hamilton.
We do not use H as a label for these because H is used for Hexadecimal. It is useful in
physics and engineering. Mathematicians, physicists, astronomers, and nuclear engineers
know of these, and electrical engineers would profit from using them. One simple theory
now connects thermal, electric, and magnetic phenomena. 12
Numbers Names
We have a special vocabulary for saying numbers in the decimal system. Some
number names that are peculiar to the decimal system are in the table below. When we
use any of these names, we automatically imply that the number we are saying belongs to
the base 10 (decimal) number system. These number names are absolutely meaningless
11
Irma S. Rombauer and Marion Rombauer Becker, Joy of Cooking, Bobbs-Merrill Company (1964).
12
Peter Michael Jack, Hypercomplex Systems, “The Quaternion Electromagnetic Equations”,
http://www.hypercomplex.com/ (2006), visited 03 February 2008.
Number Systems and Data Representation 22
in any number system except base 10 and are not to be used except when referring to
decimal numbers.
For numbers from any other base number system, the digits are read individually
from left to right. For example, in binary, the number 10112 is read as “one-zero-one-
one, base 2”. In hexadecimal, the number F2A16 is read as “F-two-A, base 16”. When
the context is not clear, the base of the number system must be explicitly stated.
Position Value 8 4 2 1
13 = 1 1 0 1
8 + 4 +0+ 1 = 13, so the corresponding binary number is 11012 .
Counting Method
Use the table for binary addition to add “1” to each row to get to the next row,
with row #0 containing the number “0000”. Before looking at the addition table for
Number Systems and Data Representation 25
binary, briefly review addition for decimal numbers. Suppose we want to add the
decimal numbers 879 and 964.
Table 14. Adding Decimal Numbers With Carry
Carry 1 1 1
8 7 9
Add 9 6 4
Answer 1 8 4 3
When we add 9 + 4, the answer is 3, with a carry of 1 to the next column to the
left. When we add 7 + 6 + 1, the answer is 4, with a carry of 1 to the next column to the
left. When we add 8 + 9 + 1, the answer is 8 with a carry of 1 to the next column to the
left. When we run out of single-digit numbers, we must generate a carry to the next
column to the left.
Binary Addition
We do the same procedure in binary, except that only two numbers can be used (0
and 1). The addition table for binary is as follows.
Table 15. Binary Addition Table
Carry 1 1 1 1
1 1 0 1
Add 0 0 1 1
Answer 1 0 0 0 0
Carry 0 1 0 0
0 1 1 0
Add 0 1 0 1
Answer 1 0 1 1
Number Systems and Data Representation 26
To create the binary table from 0 to 15 decimal, begin with 0000 in row #0. To
get to each additional row, add 1 to the current row using the binary addition table.
Carry 0 0 0 0
Row #0 0 0 0 0
Add 0 0 0 1
Row #1 0 0 0 1
Carry 0 0 0 1
Row #1 0 0 0 1
Add 0 0 0 1
Row #2 0 0 1 0
Carry 0 0 0 0
Row #2 0 0 1 0
Add 0 0 0 1
Row #3 0 0 1 1
Carry 0 0 1 1
Row #3 0 0 1 1
Add 0 0 0 1
Row #4 0 1 0 0
Carry 0 0 0 0
Row #4 0 1 0 0
Add 0 0 0 1
Row #5 0 1 0 1
Carry 0 0 0 1
Row #5 0 1 0 1
Add 0 0 0 1
Row #6 0 1 1 0
Carry 0 0 0 0
Row #6 0 1 1 0
Add 0 0 0 1
Row #7 0 1 1 1
Carry 0 1 1 1
Row #7 0 1 1 1
Add 0 0 0 1
Row #8 1 0 0 0
Number Systems and Data Representation 27
Carry 0 0 0 0
Row #8 1 0 0 0
Add 0 0 0 1
Row #9 1 0 0 1
Carry 0 0 0 1
Row #9 1 0 0 1
Add 0 0 0 1
Row #10 1 0 1 0
10 A
12 C
14 Even
A16 is 1010 , C16 is 1210 , and E16 is 1410 in decimal.
Problems: Fill in all the missing values. Convert the single digit hexadecimal numbers
to 4-bit binary numbers.
Table 18. Simple Conversions by Inspection
Use table lookup in the opposite direction to convert a 4-bit binary number
to hex. Example: Convert 10102 and 01012 to hexadecimal.
10102 = A16 = AH = Ah = OxA
01012 = 516 = 5H = Ah = Ox5
To convert a long string of binary digits to hexadecimal, the first step is to group
bits into groups of four, starting from the right hand side. What is important is to start
from the right hand side when doing the grouping. What is important? It is important is
to start the grouping from the right hand side. One more time... What is important?
(Your turn.) Look at an example.
Convert 111010010010011110000000012 to hexadecimal.
We being grouping by starting at the position zero number.
Table 19. Grouping Bits for Conversion from Binary to Hexadecimal
Start
Here
ß ß ß ß ß ß ?
11 :1010 :0100 :1001 :1110 :0000 :0001
The left- most group has only two bits, so we pad on the left end with zeros to
get
Table 20. Zero Padding on the Left
Data Representation
A Little Byte of History
Computers were first used for computational tasks. The application of using
computers to process non-numeric data came at a slightly later time.
The approach to representation of character data was to represent each character
by a unique code. This is a logical approach for alphabet-based languages. It is not the
only logical approach. Another approach is to represent line strokes by a code, and
require characters to be formed by a combination of line strokes. This approach is a
reasonable way of representing ideographs. It requires the ability of the display device to
overlay images.
An early character code by IBM was Binary Coded Decimal (BCD). Each
character was represented by 6 bits (a pair of octal numbers). This was enough to
represent base-10 digits, the Latin alphabet with capital letters, and a few punctuation
characters. The implementation of character handling internally depended upon the
machine. In the early 1960s, the IBM 7040, a scientific computer, had a 36-bit word.
The IBM 7040 fetched words from memory 36 bits at a time as a unit. Special
instructions were implemented to permit extraction of 6-bit bytes from this 36-bit word
after the whole word was in the accumulator. The IBM 7040 was the descendent of the
IBM 704.
Another scientific computer was the much smaller IBM 1620. It fetched and
operated on data one character at a time. It did addition and multiplication by decimal
table look-up, and was used at University of South Carolina to control scientific
experiments. The corresponding business machine was the IBM 1401. It was the
ubiquitous machine of its day. It also was a character oriented machine with 6-bits for a
BCD alphanumeric character, plus two more bits (parity and field definition). Its word
length was variable. It used a simple language for business data processing, RPG.
Digital Equipment Corporation produced a minicomputer in 1965, the PDP-8.
This was the first computer sold retail, at a price that small organizations could purchase.
It sold for approximately $10,000 of 1965 U.S. dollars (about 15 month’s salary of a new
college graduate in a technical field). It filled a big gap in the market at a time when
other machines were only leased, and required large dedicated staffs.
The basic memory configuration of the PDP-8 was 4 kB of 12-bit words. The
PDP-8 assumed a character set of 7-bit ASCII, with the 8th bit set to 1. Description of the
7-bit ASCII code is available on the Internet. 13 This gave the ability to only represent
upper case letters. The programmer could use other character sets. Commonly, a
stripped 7-bit ASCII code was used by ANDing an ASCII character with octal 77. This
permitted storing two characters per 12-bit word. 14
13
Roman Czyborra, “Good ole' ASCII” (1998). http://czyborra.com/charsets/iso646.html visited 01
January 2008.
14
Douglas Jones, “Frequently Asked Questions About the DEC PDP-8 Computer”
ftp://rtfm.mit.edu/pub/usenet/alt.sys.pdp8/PDP-8_Frequently_Asked_Questions
Number Systems and Data Representation 33
IBM introduced the 8-bit EBCDIC (Extended Binary Coded Decimal Interchange
Code) character set which includes codes for lower case letters and additional special
characters. The IBM 360 accessed memory at an 8-bit byte level. This was a key
development in the design of machines for business data processing. It also implemented
the concept of microinstructions. Prior to the IBM 360, machine language instructions
were loaded into instruction registers, and the bit pattern was used directly to select the
hardware logic to perform the desired operation. The logic circuits required multiple
clock cycles to complete one operation. The microcode approach was to execute a
sequence of microinstructions, each requiring one clock cycle, for each macroinstruction
loaded into the instruction register. This gave IBM the ability to add to the power of its
machines by modifying its microcode. It also made hardware diagnostics easier. IBM
established dominance in the business world with the IBM 360 series. A nice
introduction to microprogramming by Mark Smotherman is available on the Internet. 15
UNICODE is an attempt to create a single international code for information
interchange. It includes a version of ASCII as a subset.
Character Codes
In today’s digital computers, data is represented in computer memory as binary
digits. Each character is represented as a unique binary code in memory. The number of
binary digits required to represent one character is called a byte. There are three primary
character codes, ASCII, EBCDIC, and UNICODE.
US-ASCII (American Standard Code for Information Interchange) is an 8-bit
code used on almost all computers worldwide except for IBM mainframes. ASCII was
originally introduced as a 7-bit code in 1968 as ANSI X3.4. The 8-bit variations are
extensions of ASCII. Jim Price has a nice description and history for ASCII on the
Internet. 16 ASCII has been extended to several 8-bit codes. Another nice, brief history of
ASCII is by Roman Czyborra. 17 This includes images of several ASCII character sets.
EBCDIC (Extended Binary Coded Decimal Interchange Code) is an 8-bit code
used on IBM mainframes. EBCDIC is proprietary to IBM. Prior to the Internet,
EBCDIC was the predominant character code in use. The Internet and the ubiquitous
presence of the personal computer have made ASCII the most commonly used code.
EBCDIC remains important in the mainframe world of commercial data
processing. See Multilingual Support in Internet/IT Applications, Trans-European
Research and Education Networking Association. 18 Kosta Kostis has provided EBCDIC
code tables. 19 20
15
Mark Smotherman, “A Brief History of Microprogramming” (March 1999)
16
Jim Price, “ASCII Chart” (28 October 2001). Visited 02 November 2001.
17
Roman Czyborra, “The ISO 8859 Alphabet Soup” (12 January 1998). Visited 02 November 2001.
18
Trans-European Research and Education Networking Association, http://www.terena.org/. Visited 01
January 2008.
19
Kosta Kostis, “EBCDIC Codepage 037”, http://www.kostis.net/charsets/ebc037.htm (17 February 2001).
Visited 02 November 2001.
20
Kosta Kostis, “EBCDIC Codepage 1047”, http://www.kostis.net/charsets/ebc1047.htm (17 February
2001). Visited 02 November 2001.
Number Systems and Data Representation 34
21
Unicode Home Page, http://www.unicode.org/. Visited 01 January 2008.
22
Greenview Data, Inc., “VEdit”, http://www.vedit.com/index.html (2007). Visited 01 January 2008.
Number Systems and Data Representation 35
Example: Use Table 22 on page 36 to code the following characters first to hexadecimal,
and then to binary.
Character 2 C A T S
EBCDIC F2 40 C3 C1 E3 E2
Binary 1111:0010 0100:0000 1100:0011 1100:0001 1110:0011 1110:0010
Character 2 C A T S
ASCII 32 20 43 41 54 53
Binary 0011:0010 0010:0000 0100:0011 0100:0001 0101:0100 0101:0011
Character 2 C
UNICODE 0032 0020 0043
Binary 0000:0000:0011:0010 0000:0000:0010:0000 0000:0000:0100:0011
Character A T S
UNICODE 0041 0054 0053
Binary 0000:0000:0100:0001 0000:0000:0101:0100 0000:0000:0101:0011
Character 2 Z o o s
ASCII
Binary ____:____ ____:____ ____:____ ____:____ ____:____ ____:____
Character 1 B i l l
ASCII
Binary ____:____ ____:____ ____:____ ____:____ ____:____ ____:____
Number Systems and Data Representation 36
Character H i H o
EBCDIC
Binary : : : : : :
ASCII
Binary ____:____ ____:____ ____:____ ____:____ ____:____ ____:____
Character 1 0 l O ?
EBCDIC
Binary : : : : : :
ASCII
Binary ____:____ ____:____ ____:____ ____:____ ____:____ ____:____
Character 2 Z S 5 0 O
EBCDIC
Binary : : : : : :
ASCII
Binary ____:____ ____:____ ____:____ ____:____ ____:____ ____:____
Character o 1 l i .
EBCDIC
Binary : : : : : :
ASCII
Binary ____:____ ____:____ ____:____ ____:____ ____:____ ____:____
Number Systems and Data Representation 38
Problem A5. Code the following in hexadecimal using ASCII, and then convert to
binary. Oven Hot
Character O v e n
ASCII hex
Binary
Character H o t
ASCII hex
Binary
Problem A6. Code the following in hexadecimal using EBCDIC, and then convert to
binary. 10 is lo
Character 1 0 i
EBCDIC hex
Binary
Character s l o
EBCDIC
Binary
Problem A7. Convert the following to hexadecimal, and then to characters using
EBCDIC code.
Example 1: Code the word δεκα (ten) from SECRET SYMBOL to JUBILATION
CODE hexadecimal, GRAN ZEFF CODE hexadecimal, and UNICODE hexadecimal
representations.
Secret Symbol δ ε κ α
Jubilation Code A4 B1 C2 A1
Gran Zeff Code 48 55 66 45
Unicode 03B4 03B5 03BA 03B1
Example 2: Code the GRAN ZEFF CODE binary into GRAN ZEFF CODE
hexadecimal, and then to SECRET SYMBOL. (man)
45 75 58 85 99 78 77 86
α ν θ ρ ω π ο s
Number Systems and Data Representation 40
Example 3: Code the UNICODE binary into UNICODE hexadecimal, and then into
SECRET SYMBOL. (word).
0000:0011:1011:1011 03BB λ
0000:0011:1011:1111 03BF ο
0000:0011:1011:0011 03B3 γ
0000:0011:1011:1111 03BF ο
0000:0011:1100:0010 03C2 s
Problem B1: Code the word αδελφοs (brother) from SECRET SYMBOL to
JUBILATION CODE hexadecimal, and then to JUBILATION CODE binary.
α δ ε λ ϕ ο s
Problem B2: Code the word δωρον (gift) from SECRET SYMBOL to GRAN ZEFF
CODE hexadecimal, and then to GRAN ZEFF CODE binary.
δ ω ρ ο ν
Problem B3: Code the word σοφοs (wise) from SECRET SYMBOL to UNICODE
hexadecimal, and then to UNICODE binary.
σ ο ϕ ο s
Problem B4: Code the word καρδια (heart) from SECRET SYMBOL to UNICODE
hexadecimal, and then to UNICODE binary.
κ α ρ δ ι α
Number Systems and Data Representation 41
Problem B5: Code the JUBILATION CODE binary into JUBILATION CODE
hexadecimal, and then to SECRET SYMBOL. (death)
Problem B6: Code the GRAN ZEFF CODE binary into GRAN ZEFF CODE
hexadecimal, and then to SECRET SYMBOL. (wicked)
0111:1000 0111:0111 0111:0101 0101:0111 1000:0101 0111:0111 1000:0110
Problem B7: Code the UNICODE binary into UNICODE hexadecimal, and then into
SECRET SYMBOL. (nation)
23
Steven J. Searle, “A Brief History of Character Codes in North America, Europe, and East Asia”,
Sakamura Laboratory, University Museum, University of Tokyo (1999),
http://tronweb.super-nova.co.jp/characcodehist.html, Visited 02 Nov 2001.
24
Jukka Korpela, “A tutorial on character code issues”, Tampere University of Technology, Finland,
http://www.cs.tut.fi/~jkorpela/chars.html, (24 October 2001). Visited 02 Nov 2001.
25
“Character Codes”, OSdata.com, http://www.osdata.com/system/physical/charcode.htm Visited 01
January 2008.
26
“Gray code”, National Institute of Standards and Technology
http://www.nist.gov/dads/HTML/graycode.html Visited 01 January 2008.
Number Systems and Data Representation 42
You can find codes for most any type of data. For example, HTML has
established accepted hexadecimal codes for colors.
9 8 7 4 is the same as
( 9 x 1000 ) + ( 8 x 100 ) + ( 7 x 10 ) + ( 4 x 1 )
This is the sum of the digits times their place values.
We also know that the number 009874 is the same number as 9874. We can write
as many zeros to the left end of a number as we want and the number does not change
value. This is true for all numbers in a positional number system.
27
Encyclopaedia Britannica 2003, Deluxe Edition CD-ROM
Number Systems and Data Representation 43
Banfill has a nice web site for review of decimal place values. 28
Algebra Review of Exponents
To understand the positional number system, we need a few basic rules of algebra
about powers or exponents.
1. Any number raised to the power zero is equal to one.
30 = 1, 270 = 1 a0 = 1 x0 = 1
3. Any number raised to a positive integer power N that is greater than one is
equal to N copies of the number multiplied together.
35 = 3 x 3 x 3 x 3 x 3 = 243
a3 = a x a x a
1
4. X − M = Notice the minus sign in the left hand term.
XM
Positional Number System General Concept
Some people like the general concept of an idea first, and then look at details.
Other people like detailed examples first, and then the general concept. If you like details
first, skip to the topic on the Binary Number System. Come back to this topic after
studying the hexadecimal system.
In a positional number system, the position of a digit within a number determines
its value. Another term for “digit” is “coefficient”. Position is numbered from a
reference point, which is called the decimal point for a base 10 number. For whole
numbers without the refe rence point explicitly represented, this is the right side of the
number. The right- most digit of a whole number is in position zero.
Let ck be the coefficient in position k, where the position is identified by the
subscript. Let the letter b stand for the base of the system. The general form of a number
in a positional number system is
…c4 c3 c2 c1 c0 . c-1 c-2 … The period between coefficients c0 . c-1 is called the
“radix point”. In base 10, this is the decimal point.
In the decimal number 84693, c4 = 8, c3 = 4, c2 = 6, c1 = 9, c0 = 3
The value of this general number c4 c3 c2 c1 c0 . c-1 c-2 … is given by:
(c4 x b ) + (c3 x b3 ) + (c2 x b2 ) + (c1 x b1 ) + (c0 x b0 ) + (c-1 x b-1 ) + (c-2 x b-2 )
4
28
J. Banfill, “Place Value – Lessons”, AAA Math, (2004). http://www.aaamath.com/B/plc.htm. Visited 01
January 2008.
Number Systems and Data Representation 44
Notice the minus sign in the powers to the right of b0 . The radix point is located
between the term with b0 and the term with b-1 . We will concentrate on whole numbers.
In the decimal number 84693, the base is 10, and we have
(8 x 104 ) + (4 x 103 ) + (6 x 102 ) + (9 x 101 ) + (3 x 100 )
= 80,000 + 4,000 + 600 + 90 + 3
Binary Number System
In the binary (base 2) number system, b = 2. The general binary form is
…c4 c3 c2 c1 c0 = … (c4 x 24 ) + (c3 x 23 ) + (c2 x 22 ) + (c1 x 21 ) + (c0 x 20 )
= … (c4 x 16) + (c3 x 8) + (c2 x 4) + (c1 x 2) + (c0 x 1)
The meaning of the binary number 10112 is shown in the table below. Recall that
3
2 = 2 x 2 x 2 = 8.
23 22 21 20 ß place values as powers of 2
8 4 2 1 ß place values
1 0 1 12 ß coefficients
In the binary number sys tem, each place value is double the previous place value,
moving from right to left.
1 0 1 12 is the same as
(1 x 8 ) + ( 0 x 4 ) + ( 1 x 2 ) + ( 1 x 1 ) This is the sum of the digits multiplied
by the place value,
= 8 + 0 + 2 + 1
= 11 in the decimal number system.
The place values of the four right- most bits are 8 4 2 1. Recall that any decimal
number from 0 to 15 can be represented by 4 bits. To do this by computation, determine
what combination of 8, 4, 2, and 1 will sum to that number. Begin with 8 and work to
smaller numbers. Add a number if the current sum plus the number is still less than or
equal to the goal. Record a “1” if you use that number. Record a “0” if you do not use
that number.
Example, 14 = 8 + 4 + 2. The number 14 can be represented by a 4-bit code by turning
on (setting to 1) only the bits in positions representing 8, 4, and 2.
8421 ß Place values.
Therefore 14 = 8 + 4 + 2 = 11102 = 1110B = %1110
Other examples:
0= 00002
7=4+2+1= 01112
15 = 8 + 4 + 2 + 1 = 11112
Number Systems and Data Representation 45
Hex
0 1 2 3 4 5 6 7 8 9 A B C D E F
Digit
Decimal
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Value
Table 24. Counting in Hexadecimal
ACE your knowledge of hexadecimal. If you memorize A, C, and E are 10, 12,
and 14, you can mentally determine B is 11, D is 13, and F is 15.
Recall that 163 = 16 x 16 x 16 = 4096.
Number Systems and Data Representation 47
Compute the sum of products from the “place value in decimal” and the
“decimal coefficients” rows.
Example 2: Convert the hexadecimal number 4A2D16 to decimal.
3 2 1 0 ß position number
163 162 161 160 ß place values as
powers of 16
4,096 256 16 1 ß place value in
decimal
4 A 2 D16 ß hexadecimal
coefficients
4 10 2 13 ß decimal
coefficients
( 4 x 4,096 ) + ( 10 x 256 ) + ( 2 x 16 ) + ( 13 x 1 ) ß sum of products
16,384 +2560 + 32 +13 ß perform
multiplication
ANSWER = 18,989
Number Systems and Data Representation 48
If you see a digit greater than 7 in a number, you know it cannot be an octal
number.
Fun Questions
How many symbols are in a base 3 system?
What is the answer to “2 + 2 = ?” in base 3? (You may do the arithmetic in base
10 and convert the ans wer to base 3.)
Sexagesimal or Sexagenary System
The sexagesimal or sexagenary system is a base 60 positional number system.
We use it for time, direction, and position. We apply it in navigation and astronomy.
Our implementation is a variant of a system first used by the early Sumerians, where we
allow decimal numbers as coefficients. To ensure we treat the number as base 60, we use
Number Systems and Data Representation 49
special symbols to indicate the place value. The place values are degrees, minutes, and
seconds. We allow the coefficient for degrees to exceed 60.
3 2 1 0 ß position number
° ‘ “ ß place value symbol
degrees minutes seconds ß place value name
602 601 600 ß place values as powers of 60
3600 60 1 ß place value in base 10
179 59 59 ß coefficients in base 10
+ ( 179 x + ( 59 x + ( 59 x 1 ) ß sum of products
3600 ) 60 )
644400 + 3540 + 59 ß perform multiplication
ANSWER = 647999” = 179° 59‘ 59“
Nothing Matters
The symbol for “nothing”, which is 0 in the Hindu-Arabic notation, is necessary
for efficient recording of numbers in a positional number system. Look again at the
general form for a number written in positional notation.
c4 c3 c2 c1 c0 . c-1 c-2 …
Suppose that there is no symbol for “nothing” and you have a number without any
coefficient for b2 . You would have to omit the term c2 from your record of that number.
You can still have a number system using position to write a number in a
canonical form, but you would also need to write each digit with its corresponding place
value, or use different number symbols as coefficients of different place values.
(c4 x b4 ) + (c3 x b3 ) + (c2 x b2 ) + (c1 x b1 ) + (c0 x b0 ) + (c-1 x b-1 ) + (c-2 x b-2 )
Explicitly writing the place value with the coefficient does not require an ordering
of terms, although it does not prohibit ordering them. The number above has the same
value as
(c4 x b4 ) + (c2 x b2 ) + (c3 x b3 ) + (c1 x b1 ) + (c0 x b0 ) + (c-1 x b-1 ) + (c-2 x b-2 )
where (c3 x b3 ) + (c2 x b2 ) were exchanged.
A system using different symbols for different place-values was used in Attic
numerals described by Herodianus. Hebrew also used a system of assigning numerical
values to letters of the alphabet. 29 Use of a finite set of different symbols as coefficients
of different place values limits the number of numbers that can be represented.
29
D. E. Smith, “Numerals”, Volume 16, page 612, Encyclopaedia Britannica (1965)
Number Systems and Data Representation 50
Conversion to Decimal
To convert a binary or hexadecimal number to decimal
1) Multiply each digit by its place value.
2) Sum the products.
3) The result is the decimal equivalent.
= (x x 46 ) + (w x 45 ) + (y x 44 ) + (w x 43 ) + ( z x 42 ) + ( y x 41 ) + ( x x 40 )
= (1 x 4096 ) + (0 x 1024 ) + (2 x 256 ) + (0 x 64 ) + (3 x 16 ) + (2 x 4 ) + (1 x 1 )
= 40,960 + 0 + 512 + 0 + 48 + 8 + 1
= 4,66510
Problem A7. Convert 30114 to decimal.
Number Systems and Data Representation 51
Note: Processors on many calculators are 4-bit processors. A unit of memory consisting
of 4-bits is a “nibble”, which is half a byte. 30
Problem A8. Convert 30178 (octal = base 8) to decimal.
30
John J. Donovan, Systems Programming, “Machine Structure – 360 and 370”, page 25, McGraw-Hill
(1972).
31
J. Banfill, “Dividing a 4-digit by 2-digit numbers”, AAA Math (2004).
http://www.aaamath.com/B/div55_x2.htm Visited 14 January 2008.
Number Systems and Data Representation 52
Step 2 1 0 = Q3 à Step 3 5 = Q4
Base = 2 2 1 = Q2 Base = 2 1 0 = Q3
2 1 0
0 1 0 = R3
0
1 = R2
Step 4 2 = Q5 à Step 5 1 = Q6
Base = 2 5 = Q4 Base = 2 2 = Q5
4 2
1 = R4 0 = R5
Record the answer using remainders , starting in the column on the right end.
6 5 4 3 2 1 0 Position Number
R6 R5 R4 R3 R2 R1 R0 ß Record remainders from right to left.
1 0 1 0 1 1 1 Final Answer
Step 2 1 2 = Q3 à Step 3 6 = Q4
Base = 2 2 5 = Q2 Base = 2 1 2 = Q3
2 1 2
0 5 0 = R3
4
1 = R2
Step 4 3 = Q5 à Step 5 1 = Q6
Base = 2 6 = Q4 Base = 2 3 = Q5
6 2
0 = R4 1 = R5
Record the answer using remainders , starting in the column on the right end.
6 5 4 3 2 1 0 Position Number
R6 R5 R4 R3 R2 R1 R0 ß Record remainders from right to left.
1 1 0 0 1 0 1 Final Answer
2 1 0 Position Number
R2 R1 R0 ß Record remainders from right to left.
12 2 13 Remainders in decimal.
C 2 D Final answer. Remainders in hexadecimal.
2 1 0 Position Number
R2 R1 R0 ß Record remainders from right to left.
E 8 F Final answer. Remainders in hexadecimal.
2 1 0 Position Number
R2 R1 R0 ß Record remainders from right to left.
E C A Final answer. Remainders in hexadecimal.
2 1 0 Position Number
R2 R1 R0 ß Record remainders from right to left.
A B C Final answer. Remainders in hexadecimal.
Step 2 6 = Q3 à Step 4 0 = Q4
Base = 8 4 8 = Q2 Base = 8 6 = Q3
4 8 0
0 = R2 6 = R3
Step 2 4 8 = Q3 à Step 3 1 2 = Q4
Base = 4 1 9 4 = Q2 Base = 4 4 8 = Q3
1 6 4
3 4 0 8
3 2 8
2 = R2 0 = R3
= ∆4 = ∃4
Step 4 3 = Q5 à Step 5 0 = Q6
Base = 4 1 2 = Q4 Base = 4 3 = Q5
1 2 0
0 = R4 3 = R5
= ∃4 = Σ4
Stop only when the Quotient is zero. Record the answer using remainders, starting in the
column on the right end.
5 4 3 2 1 0 Position Number
R5 R4 R3 R2 R1 R0 ß Record remainders from right to left.
Σ ∃ ∃ ∆ Σ ∇ 4 Final answer.
Checklist for Converting from Decimal to Another Base Number System
• Select correct base.
• Divide by base.
• Remainder becomes a digit.
• Convert the remainder to a digit of the target number system base.
• Divide into new quotient. Do not divide into remainder.
• Don’t stop dividing until quotient is zero.
• Build the final answer with the position-zero remainder at the right end.
• Identify number system base with correct notation. Allow any of the styles.
o Binary: % nnnn nnnn2 nnnnB nnnnb
o Octal: Ommmm mmmm8 mmmmQ
o Hexadecimal: OxNNNN NNNN16 NNNNH NNNNh
• Final answer correct.
You can check you answer by converting the answer back to decimal.
Number Systems and Data Representation 59
Problem B4. Use table look-up to from hexadecimal to binary. Summarize your
answers in Table 27 below.
a. Convert the hexadecimal number found in column B, row 5 to binary using
table look-up. Record the answer in column A, row 5.
b. Convert the hexadecimal number found in column B, row 6 to binary using
table look-up. Record the answer in column A, row 6.
c. Convert the hexadecimal number found in column B, row 7 to binary using
table look-up. Record the answer in column A, row 7.
d. Convert the hexadecimal number found in column B, row 8 to binary using
table look-up. Record the answer in column A, row 8.
Problem B5. Convert from decimal to base 4. Summarize your answers in Table 27
below.
a. Convert 31 from decimal to base 4. Record the answer in column C, row 1.
b. Convert 99 from decimal to base 4. Record the answer in column C, row 2.
c. Convert 165 from decimal to base 4. Record the answer in column C, row 3.
d. Convert 169 from decimal to base 4. Record the answer in column C, row 4.
Problem B6. Convert from decimal to octal (base 8). Summarize your answers in Table
27 below.
a. Convert 31 from decimal to octal. Record the answer in column D, row 1.
b. Convert 99 from decimal to octal. Record the answer in column D, row 2.
c. Convert 165 from decimal to octal. Record the answer in column D, row 3.
d. Convert 169 from decimal to octal. Record the answer in column D, row 4.
Conversion Between Base 4, Binary, and Hexadecimal
Counting in base 4 is like counting in other number systems, except you have
only 4 digits to count with, {0, 1, 2, 3}.
• Each base-4 digit corresponds exactly to two binary digits.
• Each hexadecimal digit corresponds exactly to two base-4 digits.
Do conversions by inspection (table look-up).
Number Systems and Data Representation 61
Problem B8. Each group of three binary digits is exactly one octal digit. Summarize
your answers in Table 27 below.
a. Convert the binary number in column A, row 5, to octal by inspection.
Record the answer in column D, row 5.
b. Convert the binary number in column A, row 6, to octal by inspection.
Record the answer in column D, row 6.
c. Convert the binary number in column A, row 7, to octal by inspection.
Record the answer in column D, row 7.
d. Convert the binary number in column A, row 8, to octal by inspection.
Record the answer in column D, row 8.
Table 27. Decimal Conversion Solutions
Part à A B C D
Row Decimal Binary Hexadecimal Base 4 Octal
1 31
2 99
3 165
4 169
5 257
6 445
7 479
8 708
Strings
A string is an ordered sequence of characters. Call the first character of a string
the head, and the last character of a string the tail. (This terminology is not standardized.)
One property of a string is its length. For example, the le ngth of the string, “My favorite
string?” is 19. Spaces count as characters. A very special string is the “null string”,
which is a string of zero length, such as “”. Notice that there are no characters between
the quotation marks.
A string can be eithe r a fixed length string or a variable length string. A fixed
length string is always the same length, even if all its characters are blanks. Fixed length
strings are efficiently treated as an array, and they are easy to sort.
Variable length strings are handled in one of two ways. One way is to record the
string length as one field of the string. The other way is to designate one character as a
“null character” that can be used to identify the end of a string. The internal code used is
often just the byte consisting of all zero bits, which is the null character. The null
character does not count as one of the characters when computing the length of a string.
A string that consists only of the null character is a string of zero length, and it is called
the “null string”.
For a string that includes a “length” field, that field is not shown to the outside
world. The length field is the first field retrieved from memory when working with that
string. This method allows use of a block transfer instruction from memory to the CPU,
which is a fast way of moving data. The number is placed into a register and is used as a
counter. The maximum length allowed is limited by the largest number that can fit into
the length field. For many applications, this is very efficient. It is common for text fields
in a data base to be limited to 255 characters. This leaves one byte out of 256 to be used
as a length, and a single byte can hold numbers in the range [0,255]. It is not a useful
string format if you need very long strings, such as the length of a book, or the source
code for a computer program.
Example 1: “This string is here.” The first field of this string contains the length
of the string, which is “20” for this example. The symbol is the customary symbol
used to identify a blank when it must be graphically displayed.
20 T h i s s t r i n g i s h e r e .
Example 2: “This string is here.” This is a string of length 20. The last symbol is
the lower case Greek letter lambda (λ), which corresponds to the lower case Latin letter
“l” (el). It is customary to use this symbol to represent the null character when
documenting the end of a string. The actual code stored usually is a byte of all zero bits,
not the character code for λ.
T h i s s t r i n g i s h e r e . λ
Number Systems and Data Representation 65
A language using this convention can store a sequence of strings, separating them
by λ. For example: Go Navy! Beat Army! (If you are Army, I expect you to reverse the
example.)
G o N a v y ! λ B e a t A r m y ! λ
String Operations
Operations upon strings, and symbols used to represent these operations, are
defined by the programming language being used. At the hardware level, operations that
affect strings are fetch, store, block move, comparison.
Length
The length operator gives the length of a string, not including the null character (if
it has one). Often, this operation is abbreviated as LEN. The result is an integer value.
Example 1:
x = H o n o r λ
LEN(x) = 5
Example 2:
y = L o v e y o u r n e i g h b o r λ
LEN(y) = 18
Concatenation
Concatenation is the operation of taking two separate strings, x and y, and making
one string z by appending the second string to the first string. Example:
x = B o y λ
y = S c o u t s λ
Then z = x & y is
z = B o y S c o u t s λ
Truncation
Truncation is the operation of taking only the first N characters of a string,
discarding all remaining characters. The operation requires specifying the length N. If
the string is shorter than N to begin with, the string is unaltered. When N is counted from
the left side, the operation is often called LEFT. When N is counted from the right side,
the operation is often called RIGHT.
Number Systems and Data Representation 66
LEFT Truncation
Example 1: Let
x = r a i n i n g λ
Let y = LEFT(x,4)
y = r a i n λ
Example 2: Let
x = E l i j a h λ
Let y = LEFT(x,8)
y = E l i j a h λ
Since x is shorter than the truncation length, 8, nothing is taken away from x. Therefore,
y = x.
RIGHT Truncation
Example 1: Consider the palindrome
x = M A D A M I M A D A M λ
Let y = RIGHT(x,4)
y = A D A M λ
Example 2: Let
x = C a t λ
Let y = RIGHT(x,4)
y = C a t λ
Since x < 4, no truncation takes place. y = x.
Extraction
Extraction fetches N characters from a string, beginning with a specified
beginning point k. The function is often called MID, for “middle” of a string. An error
condition exists if the operation cannot be performed.
Example 1: Let the MID function have the format
MID(x=string,k=start,N=size), and let
x = M A D A M I M A D A M λ
Let y = MID(x,6,2). Then
y = I M λ
Number Systems and Data Representation 67
Comparison
Strings are compared one character at a time. The primitive comparison operators
are “equal” ( = ) and “not equal” ( ≠ ). For two strings to be equal, they must have the
same length, and each character must be equal. A common format is:
“If x = y”
The outcome of a test is “True” or “False”.
Many computer languages permit unequal comparisons:
Table 28. Comparison Operators
String Direction
Sometimes the notion of string direction is meaningful, such as strings containing
human language. The direction of a string is context dependent, such as in a crossword
puzzle. European languages are normally read from left to right. Some languages, such
as Hebrew and Arabic, are read from right to left. Some languages may be written from
top to bottom, such as Chinese and Japanese. String direction is not usually stored with a
string, but this can be included in the data structure if necessary.
Arabic influence is also seen in the way numbers are written, which have the
reference point for whole numbers on the right end. In English, we determine the size
and partitioning of the number by first scanning the number starting at the radix (decimal)
point, and we proceed from right to left in order to select the proper words to describe the
number.
Inequality Comparisons
The inequality comparisons are used for searching and sorting. Most sorting in
business data processing is done on character data.
Example 1:
x = B l u e λ
y = B l e w λ
If x > y
The last comparison performed is x=u versus y=e. Conclude: x > y. Therefore, the
answer is “True”.
Number Systems and Data Representation 68
Example 2:
x = S w i m λ
y = S w i m m i n g λ
If x > y
Only 4 comparisons are made. Conclude: x < y because x and y are equal fo r the first 4
characters and LEN(x) < LEN(y). The answer is “False”.
String Search
The string search is a special case of the comparison. This operation searches
string x to see if string y is contained in it. If the string is found, the starting position in
string x is reported. This is sometimes called the FIND operation. The search stops with
the first match found. Some versions allow specification of a search starting point, N.
For example, k = FIND(x,y,N). If the search string is not found, the returned value
depends on the language. Often, the value returned is a negative number.
Example 1: Let
x = B l e s s e d a r e t h e p o o r λ
y = l e s s λ
k = FIND(x,y)
The result is k = 2.
Some search programs permit restriction to locating only whole words. If that
restriction were applied in the above example, the result would be k = -1, indicating that
the search string was not found in the target string. Restricting a search to whole words is
more difficult. After locating a candidate match, the target must be examined to
determine if the matched string was merely contained in a longer word, or was a
complete word. What are indicators that a whole word was found?
Table 29. Detecting a Whole Word in a String
This is not an exhaustive list. The search is further complicated by other factors:
• exclude or skip over non-printing control codes, such as used to control the font.
• permit matching to upper and lower case letters.
• permit use of wild card characters in the search string. A wild card character can
specify an exact unknown character position (often indicated in the search string
by a question mark, ?), or an indefinite number of unknown characters at a
particular position (often indicated in the search string by an asterisk, *).
Example 2: Let
x = B l e s s e d a r e t h e p o o r λ
y = e λ
k = FIND(x,y,13)
The result is k = 15
Example 3: Let
x = B l e s s e d a r e t h e p o o r λ
y = i s λ
k = FIND(x,y)
The result is k = -1
Compound Expressions
It is common to use the results of one operation as a parameter or string argument of
another expression. This is composition of functions. As in evaluation of algebra
expressions, evaluate the innermost function(s) first.
Example 1: Find Again and extract.
x = B e b r a v e . B e s t r o n g .
B e r e a d y . B e a l e r t . λ
y = B e λ
Find:
z = MID( x, FIND(x, y, FIND(x,y) + LEN(y) ) + LEN(y) + 1, 6 )
z =
Solution:
n = FIND(x, y, FIND(x,y) + LEN(y) )
j = FIND(x,y) = 1
k = LEN(y) = 2
n = FIND(x, y, j + k )
n = FIND(x, y, 1 + 2) = FIND(x, y, 3) = 11
Number Systems and Data Representation 70
z = MID( x, 11 + LEN(y) + 1, 6 )
z = MID( x, 11 + 2 + 1, 6 ) = MID( x, 14, 6 )
z = s t r o n g λ
Find k = FIND(x,y)
Problem 8. Search.
x = t i d i n g s . Y o u s h a l l λ
y = y o u λ
Search only for whole words; permit upper or lower case.
Find k = FIND(x,y)
Problem 9. Compound Function
x = B l e s s e d a r e t h e p o o r λ
y = P r e p a r e d λ
Find:
z = LEFT(x,5) & MID(x,13,3) & y
z =
Problem 10. Compound Function
x = B e b r a v e . B e s t r o n g .
B e r e a d y . B e a l e r t . λ
y = B e λ
Find:
z = RIGHT(x, LEN(x)–LEN(y)–FIND(x,y, FIND(x,y, FIND(x,y)+LEN(y))+LEN(y)))
z =
Number Systems and Data Representation 72
Logic Operations
Logic operations accept one or two binary inputs and produce one binary output.
Each operation is defined through use of a table. The table is called a truth table because
a zero (0) is called a False (F) value, and a one (1) is called a True (T) value. Logic
operations are also called switching operations, combinational logic, Boolean logic, or
Boolean algebra. The rules for logic were developed by English mathematician George
Boole (1815 – 1864).
The logic operations are used in Boolean algebra, logic, and set theory. Boolean
algebra is used by binary computers. Logic is used in law, philosophy, and mathematics.
Set theory is used in statistics and mathematics. Each of these disciplines has its own
names and symbols for these operations. The rules of algebra are the same.
The most basic logic operations are the AND, OR, and NOT functions. When
these functions are implemented in electronics, they are called “gates”. All other
functions can be synthesized from these. These are already familiar to you through your
use of every day language.
For multi-bit variables, the operation is done for each bit position, independently
of all other bit positions. It is important to right-align variables before beginning an
operation. There are not carries or borrows, unlike arithmetic operations.
The symbols for AND, OR, and NOT which are given below are used in
equations. The logical AND function in Boolean logic corresponds abstractly to the
multiplication function in algebra. The lo gical OR function in Boolean logic corresponds
abstractly to the addition function in algebra. The logical NOT function is shown by a
bar above the variable. American National Standards Institute (ANSI) Circuit symbols
from OrCAD Capture are shown. 32
AND Operation
Suppose your friend says he is going swimming and flying. That tells you he is
going to do both activities. If you ask for a peanut butter and jelly sandwich, you want
peanut butter, and you also want jelly. If you are asked if you got peanut butter and jelly,
and you actually got both, the answer is True. If you did not actually receive both, the
answer is False. The AND operation is summarized in a truth table. Let peanut butter be
represented by the variable X, and let jelly be represented by the variable Y.
Table 31. Truth Table for AND Operator, Version 1
32
OrCAD Capture Demonstration 9.10.93 (26 April 1999).
Number Systems and Data Representation 74
Equation 1. x AND y = x • y = xy
The ANSI standard circuit symbol for the AND function is: The
inputs are applied at pins 1 and 2 on the left side, as the symbol is presently oriented.
The output is taken at point 3 on the right side. Note the straight edge on the input end of
the symbol. The output end is rounded.
In this truth table, the inputs to the AND operator are given in the top row and the
left column. The outcome is given in the othe r cells.
There is another version of this same table.
Table 32. Truth Table for AND Operator, Version 2
Inputs Output
X Y X AND Y
0 0 0
0 1 0
1 0 0
1 1 1
There is one more version of this same table. This version will be extended with other
operators.
Table 33. Truth Table for AND Operator, Version 3
Inputs
Function XY = 00 XY = 01 XY = 10 XY = 11
X AND Y 0 0 0 1
Example: Perform the operation 1100 AND 1010.
X 1 1 0 0
Y 1 0 1 0
X AND Y 1 0 0 0
AND Applications
The AND operation is used as a mask to isolate one or more bits from a larger
collection of bits, such as extracting a subnet address in a network router application, or
obtaining the binary value of a decimal digit represented by its ASCII, EBCDIC, or
UNICODE character. It is also used to clear one or more bits in a larger collection of
bits, as might be done to convert a lower case ASCII or UNICODE character to upper
case, converting an upper case EBCDIC character to lower case, to clear a bit to zero in a
compact data record, or to clear the sign bit to zero.
Example: Convert EBCDIC decimal character to binary.
Observe that the binary value is contained in the lower four bits of the EBCDIC
code. For example, the EBCDIC code for the character printed as “9” is F9H. In binary,
this code is 11111001B. To obtain the binary value of 9, do the AND operation using
the mask of 00001111B = 0FH. The operation F9H AND 0FH = 09H in binary is
computed bit by bit:
Number Systems and Data Representation 75
“9” = F9H = 1 1 1 1 1 0 0 1
Mask = 0FH = 0 0 0 0 1 1 1 1
Binary 9 = 09H = 0 0 0 0 1 0 0 1
OR Operation
The operation “x OR y” is True if either x or y is True. This includes the case
that x and y are both True. If you asked for red or blue crayons, this would be satisfied
by getting either a red crayon, a blue crayon, or both a red and a blue crayon. If you are
really thirsty, you will take Pepsi OR Coke. The truth table is:
Table 34. Truth Table for OR Operator, Version 1
OR X=F=0 X=T=1
Y=F=0 F=0 T=1
Y=T=1 T=1 T=1
The logic symbol for the OR function is the arithmetic addition symbol. The
following are equivalent:
Equation 2. x OR y = x + y
The ANSI standard circuit symbol for the OR function is: The inputs are
applied at pins 1 and 2 on the left side, as the symbol is presently oriented. The output is
taken at point 3 on the right side. Note the concave curve on the input side of the symbol.
The output end looks like a bullet with a blunt point.
An alternate form for this same truth table is:
Table 35. Truth Table for OR Operator, Versi on 2
Inputs
Function XY = 00 XY = 01 XY = 10 XY = 11
X OR Y 0 1 1 1
Number Systems and Data Representation 76
X 1 1 0 0
Y 1 0 1 0
X OR Y 1 1 1 0
OR Applications
The OR operation is used as a mask to set one or more bits in a collection of bits,
such as obtaining the decimal ASCII, EBCDIC, or UNICODE cha racter of a binary value
between 0 and 9. It is also used to set one or more bits in a larger collection of bits, as
might be done to convert an upper case ASCII or UNICODE character to lower case,
converting an lower case EBCDIC character to upper case, to set a bit to one in a
compact data record, or to set the sign bit to one.
Example: Convert the binary value 00000111B to the ASCII code for the
character “7”, which is 37H.
To do this, use the OR operation to insert the code 0011 in the upper half of the
byte, using the mask 30H. 07H OR 30H = 37H.
710 = 0 0 0 0 0 1 1 1
Mask = 30H = 0 0 1 1 0 0 0 0
ASCII “7” = 37H = 0 0 1 1 0 1 1 1
NOT Operation
The NOT operator uses only one variable as an input, and is therefore called a
unary operator. Another name for this is the complement operator. Whatever value the
input has, the output is just the opposite. When applied to a full byte, all the zeros of the
byte get changed to ones, and all the previous ones get changed to zeros. When a kid
says, “I love school … NOT!”, the kid is negating the sentence just spoken.
The truth table for the NOT operator is:
Table 36. Truth Table for NOT Operator
OR X=F=0 X=T=1
NOT X T=1 F=0
Number Systems and Data Representation 77
There are three logic symbols used for the NOT operator. One logic symbol for the
NOT function is a bar over the variable. Another is an apostrophe following the variable.
The third is a horizontal bar with the right end bent down, like a golf club. The following
are equivalent:
Equation 3. NOT x = x = x ' = ¬x
The ANSI standard circuit symbol for the NOT function is: The input is
applied at pin 1 on the left side, as the symbol is presently oriented. The output is taken
at point 2 on the right side. Note the small circle on the output side of the symbol. The
body of the symbol is an equilateral triangle.
Example: Perform the operation NOT 1011.
X 1 0 1 1
NOT X 0 1 0 0
NOT Applications
The NOT operator can be used to treat a bit as a toggle switch. Suppose a push
button light switch is “off”. Pushing the switch changes it to “on”. Pushing the switch
again changes it back to “off”. Let a bit represent the state of the switch. Let “off” be
represented by zero, and let “on” be represented by one. The NOT operator toggles the
bit between zero and one.
Applying the NOT operator to an integer word results in the one’s
complement of that word. It may be useful to convert a hexadecimal
representation to binary before taking the complement, and then convert the
result back to hexadecimal.
Example: Find the one’s complement of A0F3H. NOT A0F3H = 5F0CH.
A0F3H = 1 0 1 0 0 0 0 0 1 1 1 1 0 0 1 1
NOT A0F3H = 0 1 0 1 1 1 1 1 0 0 0 0 1 1 0 0
Logic Operation Problems
Perform the following operations.
Problem 1. D6H AND ADH
Problem 2. A5H OR 61H
Problem 3. NOT 0FH
Boolean Algebra Theorems
Properties of Boolean algebra are summarized in the below table from Sloan. 33
33
Martha E. Sloan, Computer Hardware and Organization: An Introduction, Second Edition, Science
Research Associates, Inc (1983). pp 26-27.
Number Systems and Data Representation 78
DeMorgan’s
x + y = x•y x• y = x + y
Laws
DeMorgan’s
Laws
( x • z ) + ( y • z ) = (x + z ) • (y + z ) ( x + z ) • ( y + z ) = (x • z ) + ( y • z )
Exclusive OR (XOR) Operation
The Exclusive OR operation x XOR y is True if either x or y, but not both x and
y, are True. The word “or” in “I will eat with chopsticks or a fork” probably means
XOR. The language of logical operations is unambiguous, unlike English. The truth
table is
Table 38. Truth Table for XOR Operator, Version 1
The ANSI standard circuit symbol for the XOR function is: This is
similar to the OR symbol. The inputs are applied at pins 1 and 2 on the left side, as the
symbol is presently oriented. The output is taken at point 3 on the right side. Note the
concave double curve on the input side of the symbol.
Number Systems and Data Representation 79
Inputs
Function XY = 00 XY = 01 XY = 10 XY = 11
X XOR Y 0 1 1 0
Contrast the XOR operator with the OR operator. The difference between the two is in
the outcome when X=1 and Y=1.
Example: Perform the operation 1100 XOR 1010.
X 1 1 0 0
Y 1 0 1 0
X XOR Y 0 1 1 0
Real Logic Components
It is easier to manufacture another family of logic components, the NAND and
NOR functions. The NAND is the complement of the AND function, which is the NOT
AND function. The NOR is the complement of the OR function, which is the NOT OR
function. The NOT function can be constructed from either the NAND function or the
NOR function. The circuit symbols are like the AND and OR functions, with the
addition of a small circle at the output end. All logic circuits can be made with NAND
and NOR gates. This will be demonstrated. In general, it is easier to learn the theory in
terms of AND, OR, and NOT. It is easier to build chips using NAND and NOR.
NAND Operation
The operation “x NAND y” is True if either x or y is False. The truth table is:
Table 40. Truth Table for NAND Operator, Version 1
The logic symbol for the NAND function is the up arrow, . The following are
equivalent:
Equation 5. x NAND y = x ↑ y = x • y
The ANSI standard circuit symbol for the NAND function is: This is like
the AND function with a circle at the output end. The inputs are applied at pins 1 and 2
on the left side, as the symbol is presently oriented. The output is taken at point 3 on the
right side.
Number Systems and Data Representation 80
Inputs
Function XY = 00 XY = 01 XY = 10 XY = 11
X NAND Y 1 1 1 0
Notice that the NAND gate can be used to construct the NOT function because
1 NAND y = NOT y.
Example: Perform the operation 1100 NAND 1010.
X 1 1 0 0
Y 1 0 1 0
X NAND Y 0 1 1 1
NOR Operation
The operation “x NOR y” is True if both x and y are False. The truth table is:
The logic symbol for the NOR function is the “down” arrow, . The following
are equivalent:
Equation 6. x NOR y = x ↓ y = x + y
The ANSI standard circuit symbol for the NOR function is: This is like
the OR function with a circle at the output end. The inputs are applied at pins 2 and 3 on
the left side, as the symbol is presently oriented. The output is taken at point 1 on the
right side.
An alternate form for this same truth table is:
Table 43. Truth Table for NOR Operator, Version 2
Inputs
Function XY = 00 XY = 01 XY = 10 XY = 11
X NOR Y 1 0 0 0
Notice that the NOR gate can be used to construct the NOT function because
0 NOR y = NOT y.
Example: Perform the operation 1100 NOR 1010.
X 1 1 0 0
Y 1 0 1 0
X NOR Y 0 0 0 1
Number Systems and Data Representation 81
Output for
XY Inputs
X X
Boolean Logic Set Theory XY XY
Y Y
Operator Symbol Operation Symbol Operator Symbol 00 01 10 11
ZERO 0 Null 0 0 0 0
AND • Conjunction X ∧Y Intersection X ∩Y 0 0 0 1
ONLY X Inhibit 0 0 1 0
X X 0 0 1 1
ONLY Y Inhibit 0 1 0 0
Y Y 0 1 0 1
Mod 2
XOR ⊕ 0 1 1 0
Addition
OR + Disjunction X ∨Y Union X ∪Y 0 1 1 1
NOR 1 0 0 0
Á
Equivalence,
COINCIDENCE
Tautology X ⇔Y 1 0 0 1
Special definitions:
In the imply operation, X ⇒ Y , the variable on the left side is called the
antecedent, and the variable on the right side is called the consequent.
Tautology: X ⇔ Y Statement whose truth values are always true. This is
equivalent to ( X ⇒ Y ) ∧ (Y ⇒ X ) , or equivalently ( X ⇒ Y ) ∧ ( X ⇐ Y ) where the
implication arrow direction and order of variables are reversed in the second term.
Number Systems and Data Representation 82
Hexadecimal Addition
Hexadecimal addition is done just like decimal addition. The only difference is
that the addition table is larger.
Table 45. Hexadecimal Addition Table
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 1 2 3 4 5 6 7 8 9 A B C D E F
1 1 2 3 4 5 6 7 8 9 A B C D E F 10
2 2 3 4 5 6 7 8 9 A B C D E F 10 11
3 3 4 5 6 7 8 9 A B C D E F 10 11 12
4 4 5 6 7 8 9 A B C D E F 10 11 12 13
5 5 6 7 8 9 A B C D E F 10 11 12 13 14
6 6 7 8 9 A B C D E F 10 11 12 13 14 15
7 7 8 9 A B C D E F 10 11 12 13 14 15 16
8 8 9 A B C D E F 10 11 12 13 14 15 16 17
9 9 A B C D E F 10 11 12 13 14 15 16 17 18
A A B C D E F 10 11 12 13 14 15 16 17 18 19
B B C D E F 10 11 12 13 14 15 16 17 18 19 1A
C C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B
D D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C
E E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D
F F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E
In the addition table above, the left digit of a two digit answer is a carry to the
next column for addition. Addition is only defined for pairs of numbers.
Example 1. Add the hexadecimal numbers 7FAC and FBD using a 16-bit
word. In decimal, this is 32684 + 4029 = 36713.
“Throw
Away” Carry
Column
1 1 1
Original
7 F A C
Number
Radix Add à
F B D
Complement
Final
8 F 6 9
Result
Number Systems and Data Representation 83
Do the addition beginning from the right hand side, in column 0. That is
important. Find C and D in the column and row headings of the addition table. Find
the entry in the table where those rows intersect. The result is 19. Record the 9 at the
bottom of the calculation table in the result row. Record the 1 at the top in the Carry
row.
Move left to column 1. Find A and B in the addition table column headings.
Find the entry in the table where those rows intersect. The result is 15. Record the 1 in
the carry row of column 2. Add the 5 to the carry (1) at the top of column 1. Record
the resulting 6 at the bottom in the result row.
Move left to column 2. Find F and F in the addition table column headings.
Find the entry in the table where those rows intersect. The result is 1E. Write the 1 in
the carry row of column 3. Add the E to the carry in column 2. The result is F.
Record the resulting F at the bottom in the result row.
Move left to column 3. Only 7 is available. Add 7 to the carry in column 3.
The result is 8. Record the resulting 8 at the bottom in the result row.
The answer is 8F6916 = 8F69H = Ox8F69. In decimal, this answer is 36713.
If any carry is generated when adding numbers in column 3, the carry goes to the
“Throw Away” column. This is because we assumed our word is only 16 bits long.
Anything beyond that is lost. This is called an “overflow” condition.
Example 2. Add FFFF + 0001 using a 16 bit word. In decimal, this is 255 + 1
= 256.
“Throw
Away” Carry
Column
1 1 1 1
Original
F F F F
Number
Radix Add à
1
Complement
Final
0 0 0 0
Result
Not every group of 4 binary digits is a BCD digit. All hexadecimal digits above 9
are not BCD codes.
Example 3. Convert 1100 1010 1011 from BCD to decimal.
Answer: The groups of 4 bits given in this problem do not represent decimal
numbers in BCD.
One of the attractive features of BCD for business applications is that conversion
from ASCII or EBCDIC character code format to BCD is much simpler than from
character code format to binary. This is important to business data processing which
spends most of its time doing input, output, sorting, and storage, and comparatively little
time doing computation. ASCII and EBCDIC are 8-bit codes with the numerical digit in
the right nibble (4-bit group). Obtain the BCD representation by discarding the left
nibble. Technically, do a "logical AND" of the ASCII or EBCDIC code with "0F"
(hexadecimal) [ 00001111 (binary) ] to obtain the BCD code. Using the ability to shift
register contents right and left, you can pack BCD digits. This is called "packed BCD" in
technical literature by people that work at the machine language level of programming a
processor. Therefore, if you have the decimal number "937", you get
"0000:1001:0011:0111".
Table 47. Codes for Decimal Numbers in ASCII, EBCDIC and BCD
Decimal 0 1 2 3 4 5 6 7 8 9
ASCII
30 31 32 33 34 35 36 37 38 39
Hex
ASCII 0011: 0011: 0011: 0011: 0011: 0011: 0011: 0011: 0011: 0011:
Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
EBCDIC
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9
Hex
EBCDIC 1111: 1111: 1111: 1111: 1111: 1111: 1111: 1111: 1111: 1111:
Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
BCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Hardware for performing BCD arithmetic was quickly introduced for computers
designed for business data processing. The IBM 1401 had BCD hardware. The IBM 360
included BCD arithmetic implemented in microcode. When microprocessors were
introduced, the Z80 processor included BCD arithmetic. The calculator is a natural target
for using BCD arithmetic, as the person using a calculator is most likely to be thinking in
base 10. Beginning with the 80486 processor, Intel processors have BCD and binary
integer arithmetic support included in the x87 Floating Point Unit (FPU).
IBM Binary Coded Decimal (BCD)
IBM binary coded decimal is used only to represent integers. Any scaling for a
fractional part must be done by the programmer.
IBM provides two forms, Packed BCD and Unpacked BCD. Packed BCD is
more compact for storage and is the only BCD format that can be used in computations.
This is an appropriate format for accounting data. Unpacked BCD is faster to use when a
number is not required for numerical computations.
Number Systems and Data Representation 87
The codes used for plus and minus signs depend upon the operating environment
used to generate the data. The codes for the sign are different than the codes for
numbers. By placing the sign nibble or byte at the end a number, the sign can therefore
be used to identify the end of a BCD number. See the SAS Language Reference
Dictionary for a discussion. 34
Table 48. IBM BCD Sign Codes
Zero is normally accompanied with a plus sign, although a minus sign with zero is
also valid.
34
SAS Institute, Inc., “Working with Packed Decimal and Zoned Decimal Data”, SAS Language
Reference: Dictionary (1999).
http://www.rdg.ac.uk/ITS/Topic/Stats/StGSAS8_01/SAS8/lgref/z1131887.htm, 09 Jun 2002.
Number Systems and Data Representation 88
This is good for transaction processing, which is characterized by much input, sorting,
and much output. Data must be converted to Packed BCD before doing computations.
Z d Z d S d
Problem 2. Convert ASCII -736 to IBM Packed BCD.
d d d S
Number Systems and Data Representation 89
0 Sign 1 15
The signed short integer is a 16-bit, two’s complement integer. The unsigned
short integer is a 16-bit magnitude word.
0 Sign 1 31
The signed long integer is a 32-bit, two’s complement integer. The unsigned long
integer is a 32-bit magnitude word.
Number Systems and Data Representation 90
3 2 1 0 -1 -2 -3 Position
Number
23 22 21 20 2-1 2-2 2-3 Place Value
8 4 2 1 1/2 1/4 1/8 Place Value
as Decimal
Fraction
8 4 2 1 0.5 0.25 0.125 Place Value
in Decimal
1 1 0 1 .1 0 1 Coefficients
+ + + + + Sum of
(1x8) (1x4) (Ox2) (1x1) (1x0.5) (Ox0.25) (1x0.125) Products
8 +4 +0 +1 + 0.5 + 0.25 + 0.125
ANSWER = 13.625
Convert a Decimal Fixed Point Number to Binary
The approach for converting a decimal integer to binary is used for the whole
number portion of the decimal fixed point number. The procedure for conversion of the
fraction part of the decimal fixed point number to binary uses multiplication rather than
division.
Example: Convert the decimal number 21.40625 to binary.
Partition the decimal number into a whole number plus a fraction.
21.40625 = 21 + 0.40625
Convert the whole number portion to binary using the method of successive
division.
Step 0 1 0 = Q1 à Step 1 5 = Q2
Base = 2 2 1 = Q0 Base = 2 1 0 = Q1
2 1 0
0 1 0 0 = R1
0
1 = R0
Step 2 2 = Q3 à Step 3 1 = Q4
Base = 2 5 = Q2 Base = 2 2 = Q3
4 2
1 = R2 0 = R3
Continue repeating the process until the remaining fraction part is zero.
Column
-1 -2 -3 -4
Position Number
Fraction of R-1 0. 8 1 2 5
b 2
Carry 1 0 0 1
0 6 2 4 0
R-2 = 1 1 6 2 5 0
Column
-1 -2 -3
Position Number
Fraction of R-2 0. 6 2 5
b 2
Carry 1 0 1
0 2 4 0
R-3 = 1 1 2 5 0
Column
-1 -2
Position Number
Fraction of R-3 0. 2 5
b 2
Carry 0 1
0 4 0
R-4 = 0 0 5 0
Number Systems and Data Representation 93
Column
-1
Position Number
Fraction of R-4 0. 5
b 2
Carry 1
0 0
R-5 = 1 1 0
The procedure stops when the fraction of the last remainder is zero. Build the
fraction part of the binary number working from left to right, away from the radix point.
The fraction part of the answer is
R –1 R –2 R –3 R –4 R –5 = 0.0110 1
The final answer is the sum of the whole part plus the fraction part.
1 0101 + 0.0110 1 = 1 0101.0110 12 = 21.40625
Convert a Fixed Point Hexadecimal Number to Decimal
The hexadecimal number BAD.CE16 is evaluated as
(B x 162 ) + (A x 161 ) + (D x 160 ) + (C x 16 –1 ) + (E x 16 –2 )
= ( 11 x 256 ) + ( 10 x 16 ) + (13 x 1 ) + (12 x 0.625 ) + (14 x 0.00390625 )
= 2816 + 160 + 13 + 7.5 + 0.0546875 = 2996.554688
2 1 0 -1 -2 Position
Number
162 161 160 16 -1
16 -2
Place Value
256 16 1 1/16 1/256 Place Value
as Decimal
Fraction
4256 16 1 0.625 0.00390625 Place Value
in Decimal
B A D .C E Coefficients
+ + + + Sum of
(11x256) (10x16) (13x1) (12x0.625) (14x0.00390625) Products
+ 2816 + 160 + 13 + 7.5 + 0.0546875
=
2996.554688
Convert a Decimal Fixed Point Number to Hexadecimal
The approach for converting a decimal integer to hexadecimal is used for the
whole number portion of the decimal fixed point number. The procedure for conversion
of the fraction part of the decimal fixed point number to hexadecimal uses multiplication
rather than division.
Number Systems and Data Representation 94
Column
-1 -2 -3
Position Number
Fraction of R-1 0 .8 7 5
b 1 6
Sum Carry 1 2 1
Carry 0 4 4 3 0
Carry 1 0 0 0 0
8 2 0
8 7 5 0
R-2 = 1 4 0 0 0
= E16
The procedure stops when the fraction of the last remainder is zero. Build the
fraction part of the binary number working from left to right, away from the radix point.
The fraction part of the answer is
R –1 R –2 = 0.FE
The final answer is the sum of the whole part plus the fraction part.
CDA + 0.FE = CDA.FE16 = 3290.9921875
Fixed Point Conversion Problems
Problem 1. Convert 11 1011 0101.0011 012 to hexadecimal.
Problem 2. Convert FED.CAB16 to binary.
Problem 3. Convert 10 1100.0112 to decimal.
Problem 4. Convert 107.625 to binary. (110 1011.1012 )
Problem 5. Convert AD.E16 to decimal. (173.875)
Problem 6. Convert 430.7578125 to hexadecimal. (1AE.C2H)
Number Systems and Data Representation 96
= (1 x 32) + (0 x 16) + (0 x 8) + (0 x 4) + (0 x 2) + (0 x 1)
= 32 + 0 + 0 + 0 + 0 + 0
= 32
Convert the right partition 10112 to decimal.
3 2 1 0 ß position number (bit number)
23 22 21 20 ß place value as power of base 2
8 4 2 1 ß place value
1 0 1 12 ß digits
= (1 x 8) + (0 x 4) + (1 x 2) + (1 x 1)
= 8 + 0 + 2 + 1
= 11
Add the decimal values of these partitions. 32 + 11 = 43.
Partition Problems
Problem A1. Partition 1011 01102 between bits number 4 and 5. What are the two
resulting decimal numbers and their sum? Find:
a. Left partition
b. Right partition
c. Sum
Number Systems and Data Representation 97
Problem A2. Partition 0110 11002 between bits number 4 and 5. What are the two
resulting decimal numbers and their sum? Find:
a. Left partition
b. Right partition
c. Sum
Partitioning Internet Protocol (IP) Numbers
Internet Protocol (IP) addresses for Internet Protocol Version 4 (Ipv4, in use
during 2002 A.D.) are 32 bits long, and are written as a dotted decimal number. It is a
sequence of four decimal numbers that represent the value of each octet. The term octet
refers to a group of 8 bits. 35 A typical IP address written in dotted decimal form is:
10.1.127.201 . In the table below, this IP address is converted to binary and hexadecimal.
The second row contains the bit number in the 32-bit word.
10 1 127 201
31 24 23 16 15 8 7 0
0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 0 1 1 0 1 0 0 1
0 A 0 1 7 F 6 9
35
Debra Littlejohn Shinder, Computer Networking Essentials, page 233, Cisco Systems, Inc. (2001)
Number Systems and Data Representation 98
The portion coded with “x” is the address for a subnet. The portion coded with
“y” is the address for a specific host machine on that subnet.
A necessary task is to identify the numbers that correspond to a chosen partition.
Subnet Mask
A subnet mask has 1 in all the positions in the network and subnet portion of
the address field. The dotted decimal notation is 255.255.240.0 . When this is ANDed
with an incoming IP address, the result is the address for the target subnet.
255 255 240 0
31 24 23 16 15 8 7 0
1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
F F F F F 0 0 0
Subnet Problems
Problem B1. Convert the dotted decimal for IP address 17.10.127.5 to binary and
hexadecimal, using the table below.
31 24 23 16 15 8 7 0
Problem B2. How many subnet addresses are possible in the subnet mask shown
below?
31 24 23 16 15 8 7 0
0 0 0 0 1 0 1 0 x x x x x x x x x y y y y y y y y y y y y y y y
Problem B3. How many host machine addresses are possible for each subnet in the
subnet mask shown below?
31 24 23 16 15 8 7 0
0 0 0 0 1 0 1 0 x x x x x x x x x y y y y y y y y y y y y y y y
Problem B4. How many bits in the below mask need to be set aside to make sure each
subnet can have at least 283 host machines?
31 24 23 16 15 8 7 0
0 0 0 0 1 0 1 0 z z z z z z z z z z z z z z z z z z z z z z z z
Problem B5. How many subnet addresses are possible in the below mask if enough bits
are set aside to make sure each subnet can have at least 283 host machines?
31 24 23 16 15 8 7 0
0 0 0 0 1 0 1 0 z z z z z z z z z z z z z z z z z z z z z z z z
Problem B6. What is the dotted decimal notation for the subnet mask needed for
ensuring enough bits are set aside to ensure each subnet can have at least 283 host
machines?
255 255 254 0
31 24 23 16 15 8 7 0
Number Systems and Data Representation 99
X is used for 10. V is the upper half of X, and is used for 5. L is used for 50. C
is used for 100. D is 500. M is 1000. [The Romans used (|), not M.] 36 A dash line
above a letter multiplies the base value by 1000. There is no zero. Sometimes, a pair of
horizontal bars, above and below these symbols, are used to distinguish these from mere
alphabet letters.
While there is a pattern to the meaning of numbers depending on their position, it
is very different from the simple pattern used with the decimal number system. As you
might guess, arithmetic using the Roman numeral system is not as easy as with the
decimal system. The Romans apparently did not do computation with their numerals, but
rather used a counting board or abacus. A 1st century A.D. gravestone shows a person
with an abacus in his hand. 37
The Roman numeral system applied meaning to the position of numbers, but not
in the same way our present day positional number system does. It evolved from a tally
system. The basic tally system uses vertical lines, one line for each object counted. The
next modification is a grouping operation that is applied in steps of 5. After
accumulating 4 vertical marks, such as |||| , a grouping is made by placing a diagonal
slash through the group of 4 vertical marks.
The Roman numeral system is the next level of sophistication. Instead of having
5 marks, the symbol V is substituted. It took less space to write IV for 4 than it did to
write |||| . Writing materials were precious. When the printing press was invented, word
spellings and number formats were modified to further reduce the space required.
Roman numerals are created from left-to-right and evaluated from right-to- left.
The object is to express the quantity using the fewest possible symbols.
Parsing Rule. The pattern for evaluation that developed is as follows. Group
adjacent identical Roman Numerals before evaluating the number. Consider a pair of
adjacent Roman numerals. 38 Beginning with the right end, if the numeral to the left is less
in value to the numeral on the right,(just the numeral immediately to the right, not the
whole number to the right), the numeral on the left is subtracted from the numeral on the
right. If the numeral on the left is greater than or equal to the numeral on the right, the
numeral on the left is added to the numeral on the right. Gerard Schildberger has
published a list of numbers from 1 to 3999. 39
The following examples of years (except 1944, 1999, 2002) are from the
Government Printing Office Style Manual. 40
36
Karl Menninger, Number Words and Number Symbols: A Cultural History of Numbers, page 281, Dover
Publications (1992).
37
Karl Menninger, Number Words and Number Symbols: A Cultural History of Numbers, page 306, Dover
Publications (1992).
38
Gerard Schildberger, “Roman Numerals are Nonassociative”, email. (23 June 2002)
39
Gerard Schildberger, “The numbers from 1 to 3999 expressed as Roman numerals”,
http://www.research.att.com/~njas/sequences/a006968.txt visited 04 April 2001.
40
United States Government Printing Office Style Manual 2000, U.S. Government Printing Office (2000),
pg 189. http://www.access.gpo.gov/styleman/2000/style001.html, 26 June 2002
Number Systems and Data Representation 101
Table 50. Roman Numeral Years in the U.S. Government Printing Office Style Manual
You can sometimes reduce the number of symbols appearing in a Roman numeral
by the following approach. If the same symbol appears on both the right side and the left
side of a higher value symbol, these symbols constitute a matched pair. You can remove
both symbols without changing the value of the number. Examples:
• IXI à X
• ICMLI à CML
A problem with Roman numerals is ambiguity, as illustrated with the following
example. The parentheses indicate the grouping used in evaluating the example. Note
that this kind of ambiguity is impossible with our own positional number system.
Problem 1. Write 390 in Roman numerals. (Celts defeated Rome on 18 July 390 BC,
and sacked Rome 3 days later. 41 )
Problem 2. Write 407 AD in Roman numerals. (Constantine withdrew from Britain
that year. 42 )
Problem 3. Convert 255 to Roman numerals.
Problem 4. Add the Roman numerals XI and XXXII. Convert the result to decimal
after adding.
Problem 5. What time is XXV O’clock? (Humorous answer.)
Problem 6. Write zero in Roman numerals. (Trick question.)
Problem 7. Divide XXX by VI .
Problem 8. There is a number on the base of the pyramid shown on the back side of
the paper One Dollar Federal Reserve Note, MDCCLXXVI. Copy that number given in
Roman Numerals and translate it into Indo-Arabic numbers (the number system we use in
commerce today).
Problem 9. Evaluate the numeral I(VL) , evaluating inside the parentheses first.
Problem 10. Evaluate the numeral (IV)L , evaluating inside the parentheses first.
41
Encyclopaedia Britannica (1965)
42
Encyclopaedia Britannica (1965)
Number Systems and Data Representation 103
Fibonacci Bases
Another example of a number system that is not a strict positional number system
like the decimal system is a Fibonacci base. If you have a twisted mind, or really enjoy
looking at the world in ways different than most people, this is the number system for
you. A really nice discussion, complete with online calculators, is given by Knott. 43
Fibonacci introduced the sequence that now bears his name in Liber abaci (1202)
a problem he stated: “How many pairs of rabbits can be produced from a single pair in
one year if it is assumed that every month each pair begets a new pair which from the
second month becomes productive?”44
Fibonacci Numbers
The first few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … The
first two numbers of the sequence are established by definition as F0 = 0, F1 = 1. The rest
of the numbers are defined by the recursion formula Fn = Fn-1 + Fn-2 where n is a variable
index that takes on values 2, 3, 4, 5, … .
The sequence of Fibonacci numbers led to other questions in number theory. A
short list of examples of the sequence seen in nature is listed in Encyclopaedia
Britannica. Generalized Fibonacci numbers are useful in the design of efficient methods
of sorting very large sets of data. 45
Fibonacci Base Representation
The Fibonacci numbers are used as the basis of the number system in a place-
value notation. A number written in the Fibonacci Base Representation has the form
an Fn + an-1 Fn-1 +…+ a4 F4 + a3 F3 + a2 F2
where the coefficient an is an integer multiplier of the corresponding Fibonacci number
Fn . For example, the Fibonacci Base Representation number 95013 Fib is converted to a
decimal number by:
95013Fib = (9 x F6 ) + (5 x F5 ) + (0 x F4 ) + (1 x F3 ) + (3 x F2 )
= (9 x 8) + (5 x 5) + (0 x 3) + (1 x 2) + (3 x 1)
= 72 + 25 + 0 + 2 + 3
= 10210
Notice that the lowest subscript on the Fibonacci number and the corresponding
coefficient is 2 because F2 = 1. F1 = 1, and F0 = 0. While it would be possible to extend
the notation down to F0 , there is nothing to be gained by having two unit bases F2 and F1 ,
43
Ron Knott, “Using Fibonacci Numbers to Represent Whole Numbers” (19 January 2007).
http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibrep.html visited 15 December 2007.
44
Encyclopaedia Britannica 2003, Deluxe Edition CD-ROM
45
Donald E. Knuth, The Art of Computer Programming, Volume 3, Searching and Sorting, Addison-
Wesley (1973).
Number Systems and Data Representation 104
and pointless to include F0 since anything multiplied by zero (F0) is zero, and does not
change the value of the number being represented.
Extended Fibonacci Base Representation
The Fibonacci series can be extended to negative indexes. This is useful in other
work related to Fibonacci numbers. The extension is done by rearranging the equation
to: Fn-1 = Fn+1 – Fn .
n 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8
Fn 21 13 8 5 3 2 1 1 0 1 -1 2 -3 5 -8 13 -21
46
Ron Knott, “Using Fibonacci Numbers to Represent Whole Numbers” (19 January 2007).
http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibrep.html visited 15 December 2007.
Number Systems and Data Representation 105
The number in the third row below, labeled “Answer”, is the result of an addition
of two numbers in Zeckendorf representation. In each subsequent row, one of the rules is
applied to “simplify” the result. Repeated application of the rules eventually will
transfo rm the answer to Zeckendorf representation. The final result is in the last row.
The answer in Zeckendorf representation is: 1 0000 0001 0001 Fib = (377+8+1)10 = 38610
Fibonacci Base Representation Problems
Problem 1. Convert 5010 to Minimal Fibonacci Base (Zeckendorf) Representation.
50 Remainder R
F14 F13 F12 F11 F10 F9 F8 F7 F6 F5 F4 F3 F2 ß Fibonacci
Number
377 233 144 89 55 34 21 13 8 5 3 2 1 ß place
values
ß digits
ß decimal
values
Remainder R
53 Remainder R
F14 F13 F12 F11 F10 F9 F8 F7 F6 F5 F4 F3 F2 ß Fibonacci
Number
377 233 144 89 55 34 21 13 8 5 3 2 1 ß place
values
ß digits
ß decimal
values
Remainder R
Number Systems and Data Representation 109
where the coefficient cn is some multiplier (possibly zero) of the nth power of Φ.
Numbers are expressed compactly in a manner similar to decimal notation by writing
only the constant coefficients and placing a radix point to the left of the coefficient c-1 .
When some coefficients are positive and others are negative, the negative coefficients
47
Ron Knott, “Using Powers of Phi to represent Integers (Base Phi)” (22 December 2007).
http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/phigits.html visited 24 December 2007.
Number Systems and Data Representation 111
must be identified. One method is to enclose the coefficient with a minus sign inside
parenthesis.
Integers in Phi Base Representation
All integers can be represented in Phi Base Representation for suitable choices of
the various coefficients. It is useful to look at a few examples.
Φ 0 = F1+F0ϕ = 1
Φ 1 = F2+F1ϕ = 1+ϕ Φ -1 = F0+F-1ϕ = ϕ L1 = Φ 1 - Φ -1 = 1
Φ 2 = F3+F2ϕ = 2+ϕ Φ -2 = F-1+F-2ϕ = 1-ϕ L2 = Φ 2 + Φ -2 = 3
Φ 1 + Φ -2 = 2
Φ 3 = F4+F3ϕ = 3+2ϕ Φ -3 = F-2+F-3ϕ = -1+2ϕ L3 = Φ 3 - Φ -3 = 4
Φ 4 = F5+F4ϕ = 5+3ϕ Φ -4 = F-3+F-4ϕ = 2-3ϕ L4 = Φ 4 + Φ -4 = 7
Φ 5 = F6+F5ϕ = 8+5ϕ Φ -5 = F-4+F-5ϕ = -3+5ϕ L5 = Φ 5 - Φ -5 = 11
Φ 6 = F7+F6ϕ = 13+8ϕ Φ -6 = F-5+F-6ϕ = 5-8ϕ L6 = Φ 6 + Φ -6 = 18
Notice that the sequence Ln = Φ n +(-1)nΦ -n for n > 0 of results form a series
similar to the Fibonacci series: Ln = Ln-1 + Ln-2 where L1 = 1 and L2 = 3 . The numbers
in the set {…, 1, 3, 4, 7, 11, 18, …} are called Lucas numbers.
Of course, all integers can be expressed merely as an integer multiple of Φ 0 , such
as 13 = 13Φ 0 . This certainly would be a unique representation, but it would miss the
point that we can represent integers by combinations of numbers using an irrational base.
The combinations used to express a particular integer are not necessarily unique. For
example, 5 = 1+4 = 2+3.
Integers Equation Phi Base
Representations
5 = 5Φ 0 5.0
= 5( Φ 1 - Φ -1 ) 50.(-5)
4+1 = ( Φ 3 - Φ -3 ) + ( Φ 1 - Φ - 1010.(-1)0(-1)
1
)
2+2+1 = 2( Φ 1 + Φ -2 ) + Φ 0 21.02
3+2 = ( Φ 2 - Φ -2 ) + ( Φ 1 + 110.0
Φ -2 )
= 3Φ 0 + ( Φ 1 + Φ -2 ) 13.01
= ( Φ 2 - Φ -2 ) + 2Φ 0 102.0(-1)
• No two consecutive Phi Base numbers can be used in the same sum. This means
the digit 1 cannot appear adjacent to another 1.
Digits written in minimal Phi base representation are known as “Phigits ”. 48
Converting Phi Base Representation to Minimal Phi Base Representation
It may be necessary to rewrite a number written in Phi Base Representation to
Minimal Phi Base Representation. Two rules can be helpful. Both implement the
recursion Φ n+1 = Φn + Φ n-1 The primary purpose is to have a systematic method of
applying the rules until no opportunity is available to apply them again. You can modify
the rules to start scanning from the opposite end. You will eventually get the same result.
• Rule 1: Start from the right end of the row and proceed to the left to find the next
number greater than “1”. If column Φ n contains a number greater than 1, subtract
1 from column Φ n , add 1 to column Φ n-1 , and add 1 to column Φ n-2 .
• Rule 2: Start from the left end of the row and proceed to the right. If two
adjacent columns, Φ n and Φ n-1, contain numbers greater than zero, then subtract 1
from columns Φ n and Φ n-1 and add 1 to column Φ n+1 .
Example 1: Write 2( Φ 1 +Φ-2 ) , written as 20.02Phi ,. and convert it to minimal phi base
representation
Step Φ3 Φ2 Φ1 Φ0 • Φ -1 Φ -2 Φ -3 Φ -4
1 Start 2 0 0 2
2 R#1 2 0 0 1 1 1
3 R#1 1 1 1 1 1 1
4 R#2 1 0 0 1 1 1 1
5 R#2 1 0 1 0 0 1 1
6 R#2 1 0 1 0 1 0 0
The minimal phi base representation is Φ 2 + Φ 0 + Φ-2 written as . 101.01Phi .
When there is a nega tive coefficient, one approach is to propagate positive
coefficients from the left to the right by reversing Rule 2. The goal is to produce a
coefficient of the same power of Φ that is the same in magnitude, and opposite in sign,
and then do the subtraction.
48
Jose Glez-Regueral, Professor at Madrid, cited by Ron Knott, “Other names for Base Phi”, in “Using
Powers of Phi to represent Integers” (22 December 2007).
http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/phigits.html visited 24 December 2007.
Number Systems and Data Representation 113
While this answer is correct, it is not in standard (canonical) form. Rules were
applied to transform this result to the minimal Phi base representation.
The answer is 1000.1001Phi = 1Φ3 + 0Φ 2 + 0Φ1 + 0Φ 0 + 1Φ -1 + 0Φ -2 + 0Φ -3 + 1Φ -4
This can be expanded: 1Φ 3 + 1Φ -1 + 1Φ-4 = 3+2ϕ + ϕ + 2-3ϕ = 5.
Number Systems and Data Representation 114
Continued Fractions
Another type of notation that is positional is that of continued fractions. It is
useful for expressing some very important constants which have a non-repeating decimal
form, such as e and 2 . This is a topic that is sometimes taught in high schools as an
extra topic. A continued fraction has the general form
b1
X = a0 +
b2
a1 +
b3
a2 +
b4
a3 +
b5
a4 +
a5 +L
where the constants can be written in the more compact form of
b1 b2 b3 b4 b5
a 0 , a , a , a , a , a ,L
1 2 3 4 5
Every real number can be written as a unique continued fraction, and every
rational number can be written as a continued fraction with a finite number of terms. 49
Using this notation, we get very regular expressions for some important transcendental
numbers. Here are two important examples.
2 = [1, 2, 2, 2, L] ≈ 1.414215K
2 3 4 5
e = 1 + 1, , , , ,L ≈ 2.718281828459045K
2 3 4 5
A procedure that is repeated is called iterative. Iterative calculation makes it easy
to repeat the calculation until the answer is accurate enough for your purposes. The
tendency of the computation to stabilize near a final answer is called convergence. The
difference between the computed answer and the ideal value is called error. When you
do not know the number of steps needed for convergence, iterative methods can be very
useful.
A procedure that calls itself is called recursive. Recursive methods reveal
fundamental properties of a problem that might otherwise escape notice. Recursive
procedures often take less code to implement than an iterative procedure that
accomplishes the same end goal. This simplicity is very attractive when doing
49
Mark Herkommer, “Continued Fractions”, Number Theory: A Programmer’s Guide, McGraw-Hill
(1998).
Number Systems and Data Representation 116
calculations manually on a calculator. D'Souza has a nice tutorial for C++ programmers
on recursion. 50
Some very important applications of recursion occur in computer science and
mathematics. Recursive methods are sometimes the most efficient approach to
implementing translators. Another important application of recursion is in the topic of
fractals. God seems to have a special love of recursion in applying fractals in nature,
such as the growth of trees or the shape of a shore line.
The apparent ease of recursive calculation is beguiling. Recursive methods are
usually not as efficient as alternative methods of computing the same quantity, when
measured by time required for convergence. For a more detailed discussion, see Acton. 51
Example 1: Evaluate the Square Root of 2.
2 = [1,2,2,2,L]
1 1 1
2 = 1+ ≈1 + = 1+
1 1 1
2+ 2+ 2+
1 1 1
2+ 2+ 2+
1 1 1
2+ 2+ 2+
1 1 2.5
2+ 2+
2 + ... 2
1 1 1
=1+ = 1+ =1+ = 1.414012739
1 1 2.415384615
2+ 2+
1 2.407407407
2+
2.454545455
-1
This evaluation is very easy using a calculator with an reciprocal function, x .
On the TI-83 calculator, perform the following steps:
50
Erwin D'Souza, “Recursion Programming”, CoolComps Inc.,
http://www11.brinkster.com/erwnerve/recursion.htm (2000). 01 July 2002.
51
Forman S. Acton, Numerical Methods that Work, Harper and Row (1970). pp 356-357.
Number Systems and Data Representation 117
(1) 2 ENTER
(2) x –1 + 2 ENTER
(3) Repeat step (2) until the variation of answer from one iteration to the ne xt
iteration is smaller than your stopping criteria.
(4) x –1 + 1 ENTER
2 3 4 5 2 2
e = 1 + 1, , , , ,L = 1 + 1 + ≅ 1 +1+
2 3 4 5 2+
3
2+
3
4 4
3+ 3+
5 5
4+ 4+
6 6
5+ 5+
6 +L 6
2 2 2 2
= 1 +1+ =1 +1+ =1 +1+ ≈ 1 +1+
3 3 3 3
2+ 2+ 2+ 2+
4 4 4 3.827586207
3+ 3+ 3+
5 5 4.833K
4+ 4+
5 +1 6
2
≈ 1 +1+ ≈ 2.71844602
2. 783783784
This evaluation requires that you choose the number of steps in advance. It is
very easy using a calculator with an reciprocal function, x -1 . For N = 6, perform the
following steps:
(1) N=6
(2) N ENTER
(3) x –1 * ( N – 1 ) + ( N – 2 ) ENTER
(4) N=N–1
(5) Repeat steps (3) and (4) until N = 2 (that is, until N – 2 = 0).
(6) + 1 ENTER
Unfortunately, the usual arithmetic operations of addition, subtraction,
multiplication, and division are not easy for continued fractions. Some tutorials on
Number Systems and Data Representation 118
continued fractions are: Cut-the-Knot 52 , Doctor Luis 53 , Edward G. Dunne 54 , and Mark A.
Herkommer 55 .
Continued Fraction Problem
Problem 1. The golden ratio is the solution to the equation r2 = r + 1. Divide both
sides by r to get the equation r = 1 + 1/r . Substitute (1 + 1/r ) for r in the
denominator. Continue this forever. The equation r = 1 + 1/r is called a recursive
equation. With the substitutions done, it looks like
1
X = 1+
1
1+
1
1+
1
1+
1
1+
1 +L
1
X = 1+
1
1+
1
1+
1
1+
1
1+
1
52
Cut-the-Knot, http://www.cut-the-knot.com/do_you_know/fraction.shtml
53
Doctor Luis, “Continued Fractions”, The Math Forum, Drexel University (2002).
http://www.mathforum.org/dr.math, 11 JUN 2002.
54
Edward G. Dunne, “Primes and Continued Fractions”, American Mathematical Society.
http://www.research.alt.com/~njas/sequences/DUNNE/TEMPERMENT.HTML.
55
Mark Herkommer, “Continued Fractions”, Number Theory: A Programmer’s Guide, McGraw-Hill
(1998).
Number Systems and Data Representation 119
Approximation
There are gaps between numbers that we represent exactly. For example, we
cannot represent exactly the number 1/3 in base two. If the number we need cannot be
represented exactly, the selection of a number we can represent results in an
approximation error.
Evaluation of functions require time and resources. Many important functions
cannot usually be computed exactly, such as square root, trigonometric functions, and
logarithms. Engineers and scientists also use other Special Functions important to their
work which require approximation. Such functions are evaluated using numerical
methods or tabled values.
Number Systems and Data Representation 120
Round Off
Round off error is the difference between the exact number desired and the closest
number that can be represented using selected code for the finite length word. The usual
result is to increase the variance of the error as computations proceed.
Truncation
Truncation is a special case of approximation error that occurs by discarding bits
without rounding to the closest approximation. This is the “Floor” operator. The usual
result is to produce an answer biased closer to zero.
Overflow
Overflow is the condition of producing a result larger than can be represented
with the finite length word.
Overflow can occur by adding or multiplying two very large numbers to produce
a number even larger that cannot be contained within the word. A more common cause is
to divide a large number by a number very close to zero.
Underflow
Underflow is the condition of producing a result that should be non-zero that is
closer to zero than can be represented by the word using the selected code.
Underflow can occur by dividing a small number by a large number.
Arithmetic Examples
Strategies for performing arithmetic with finite length of computer words has
been to work with integers (fixed point), an exponential notation (floating point)
analogous to scientific notation, or significance arithmetic. 56 Fixed point arithmetic
places the burden on the programmer of keeping track of which part of the word
represents whole numbers, and which part represents fractions. Further coding and work
is done if the range of numbers includes negative numbers. Significance Arithmetic
explicitly includes quantifying accuracy and precision of an operation.
The purpose of the examples below is to get you thinking in terms of what has to
be considered when working with only a fixed number of digits. These examples assume
a fixed point notation using decimal arithmetic.
Suppose we are allowed a 5 digit number, with three digits to the left of the
decimal point and two digits to the right of the decimal point. We consider addition,
multiplication, and division. Traditional subtraction is not affected. (Think about why
this is true.)
56
Weisstein, Eric W. "Significance Arithmetic.", MathWorld--A Wolfram Web Resource (10 February
2008). http://mathworld.wolfram.com/SignificanceArithmetic.html visited 12 February 2008.
Number Systems and Data Representation 121
Division example: Some processors place the dividend (numerator) initially into
a register combination that is twice the length of the word being used for storage in main
memory. For our 5 digit example, the dividend would be placed into a 10 digit register
pair. The result consists of a quotient and a remainder, each in a storage- length word.
The notation below is similar to the format for doing division by hand in
elementary schools in Italy. 57 It is a convenient notation for making explicit which
quotient digit is associated with which multiplication. This results in fewer mistakes
when done by hand.
Col Col Col Col Col Col Col Col Col Col
2 1 0 –1 –2 –3 –4 –5 –6 –7
Divisor Divi-
dend
127.26) 8 1 7 .4 5 0 0 0 0 0 Quotient
Multiply 8 9 0 .6 2 (7
Subtract 2 6 .8 3 0 0 0 0 0 (.
Multiply 2 5 4 5 2 (2
Subtract 1 .3 7 8 0 0 0 0
Multiply 1 2 7 2 6 (1
Subtract .1 0 5 4 0 0 0
Multiply 0 0 0 0 0 (0
Subtract .1 0 5 4 0 0 0
Multiply 8 9 7 4 5 (7
Subtract 1 5 6 5 5 0
compact version as it would normally be done. The second version goes step by
miniature step.
Step 5
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 2 2
Multiply 4·6 4
Step 6
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 2·6 2 4
Step 7
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 3·6 8 2 4
Longer Variation A:
Step 8A
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 3·6 8 2 4
Subtotal 2 1 9 4 4
Step 9A
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 8 2 4
Subtotal 2 1 9 4 4
Carry
Multiply 4·1 4
Number Systems and Data Representation 125
Step 10A
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 8 2 4
Subtotal 2 1 9 4 4
Carry
Multiply 2·1 2 4
Step 11A
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 8 2 4
Subtotal 2 1 9 4 4
Carry
Multiply 3·1 3 2 4
Step 12A
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 8 2 4
Subtotal 2 1 9 4 4
Carry
Multiply 3·1 3 2 4
Subtotal 3 3 2 4
Number Systems and Data Representation 126
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 3·6 8 2 4
Subtotal 2 3 1 1 0 4
Carry
Multiply 4·1 4
Step 9B.
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 3·6 8 2 4
Subtotal 2 3 1 1 0 4
Carry
Multiply 2·1 2 4
Number Systems and Data Representation 127
Step 10B.
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 3·6 8 2 4
Subtotal 2 3 1 1 0 4
Carry
Multiply 3·1 3 2 4
Step 11B. In this version, the next total includes the subtotal immediately above
it.
Multiplicand.multiplier 3 2 4 · 9 6 1
Carry 2 1 3
Multiply 7 8 6
Subtotal 1 2 9 1 6
Carry 1 1 2
Multiply 3·6 8 2 4
Subtotal 2 3 1 1 0 4
Carry
Multiply 3·1 3 2 4
TOTAL 3 1 1 3 6 4
2 4 5 : 2 3 = 10.652173
2 3
1 5 0
1 3 8
1 2 0
1 1 5
5 0
4 6
4 0
2 3
1 7 0
1 6 1
9 0
6 9
Number Systems and Data Representation 129
Complement Arithmetic
Introduction to Complement Arithmetic
Complement arithmetic is an efficient way to perform subtraction involving
numbers represented by a finite length word. A major benefit is that it permits subtraction
without having to think about borrowing from adjacent columns when you use finite
length arithmetic. Binary digital computers use “One’s Complement” or “Two’s
Complement” arithmetic internally. It is very useful for computer professionals to be able
to do this in hexadecimal or octal for tracing through memory dumps to examine the
performance of a program for errors.
From a computer’s point of view, the advantage is that subtraction can be
implemented as a combination of complement and addition. No extra subtraction
hardware is required. The logical complement operation is very quick to do.
If you declare that numbers having a one in the most significant bit are negative,
and those with zero in the most significant bit are positive, then you can use two’s
complement notation to signify negative numbers in binary. With this convention, radix
complement (two’s complement in binary) provides a unique representation of the
number zero. Without this convention, you cannot tell a positive number from a negative
number by just looking at the bit pattern.
Base 10 Complement Arithmetic
We will begin with an illustration in base ten. We learned base 10 arithmetic
because most humans have 10 digits (8 fingers, 2 thumbs)… the original digital
calculator. If we want to perform the subtraction 724 - 538, we would do the following.
After 6 11 14
Borrow
7 2 4
- 5 3 8
Result 1 8 6
The problem with this approach is that you must determine if you need to borrow
from the column immediately to the left. If so, you add the amount borrowed from the
current column and subtract it from the left-adjacent column. After completing the
borrow operation, you then perform the subtraction. Humans can do this without much
additional attention when we work with base 10 because we have practiced it for years.
It is not quite as efficient as we would like for implementation on a computer. We can
make this easier.
Suppose we are only allowed to use 3-place numbers, 000 through 999. We are
required to represent all positive and negative numbers we work with by using these
Number Systems and Data Representation 130
numbers. To represent a number using complement arithmetic using base 10, do the
following.
9 9 9
Subtract 5 3 8
Intermediate 4 6 1
Result
Add 0 0 1
Complement 4 6 2
The top line consists of a finite length word filled with our highest digit in our
base 10 number system, which is 9. Recall, the digits in base 10 run from 0 through 9.
The next line is our traditional negative number. If our negative number were only 2
digits, it would be treated as having leading zeros to the left. The number in the second
row is subtracted from the top row. Notice that no borrows are required from adjacent
columns. The third row is the result of this subtraction. In base 10, this number is the
“Nine’s Complement”.
The final operation is to add 1 to the nine’s complement. This produces our
“Radix Complement”, where the radix of the number system is 10. This is our negative
number. To complete the subtraction, add this radix complement to the original number.
Throw away any digits that carry beyond the third column.
“Throw
Away”
Column
Original
7 2 4
Number
Radix Add à
4 6 2
Complement
Final
1 1 8 6
Result
7 D 4
- 5 C E
Result 2 0 6
F F F
- 5 C E
Result A 3 1
A 3 1
Add 1
Radix A 3 2
Complement
“Throw
Away”
Column
Carry 1 1
Original 7 D 4
Number
Radix A 3 2
Complement
Result 1 2 0 6
1 F 3
Subtract - 4 6 7
Result
Hmm… How do we do this? The negative number is bigger than the positive number!
Let’s try.
Find the radix complement of 467H.
F F F
- 4 6 7
B 9 8
+ 1
Radix B 9 9
Complement
Number Systems and Data Representation 133
Carry 1
1 F 3
+ B 9 9
D 8 C
F F F
- D 8 C
2 7 3
2 7 4
1 1 1 1
- 0 1 1 1
1 0 0 0
+ 1
Radix 1 0 0 1
Complement
“Throw
Away”
Column
Carry
Original 1 1 0 0
Number
Add 1 0 0 1
Final 1 0 1 0 1
Result
12 - 7 = 510 = 01012 .
Oops… it worked! J
9 9 9 “Throw
Away”
Column
Subtract 1 5 Original 3 3
Number
Add 0 0 1 Final
Result
Radix
Complement
Number Systems and Data Representation 135
9 9 9 “Throw
Away”
Column
Subtract 4 5 Original 3 3
Number
Add 0 0 1 Final
Result
Radix
Complement
9 9 9
Subtract
Intermediate
Result
Add 0 0 1
Final Result
F F F “Throw
Away”
Column
Subtract D A 1 Original F A D
Number
Add 0 0 1 Final
Result
Complement
F F F “Throw
Away”
Column
Subtract F E A Original F A D
Number
Add 0 0 1 Result
Radix
Complement
Number Systems and Data Representation 137
“Throw F F F
Away”
Column
Subtract
Intermediate
Result
Add 0 0 1
Final Result
Notice that all the numbers used in computation are positive numbers. The
purpose of complement arithmetic is to handle negative numbers without using signed
arithmetic. It is possible since we are using finite sized words to represent numbers.
We attach the minus sign to the result as a notation that the number is a negative
number. Notice that FFF (as a positive number) is greater than FC3 (as a positive
number), so FFF – F3C > 0. Therefore 03C + 1 > 0. We attach a minus sign to 03D
because we started with FAD – FEA. We note that FAD < FEA, so FAD – FEA < 0 .
Problem 5. 8-digit Binary. Subtract 11 01102 – 1 11012
“Throw 1 1 1 1 1 1 1 1
Away”
Column
Subtract 1 1 1 0 1
Intermediate
Result
Add 0 0 0 0 0 0 0 1
Radix
Complement
Number Systems and Data Representation 138
“Throw
Away”
Column
Original
1 1 0 1 1 0
Number
Radix Add à
Complement
Final
Result
1 1 1 1 1 1 1 1
Subtract 1 0 1 1 1 1 0 1
Intermediate
Result
Add 0 0 0 0 0 0 0 1
Radix Complement
“Throw
Away”
Column
Original
1 1 0 1 1 0
Number
Radix Add à
Complement
Result
Number Systems and Data Representation 139
1 1 1 1 1 1 1 1
Subtract
Intermediate
Result
Add 0 0 0 0 0 0 0 1
Final Result
Again, notice that all the numbers used in computations are positive numbers.
We attach a minus sign to the result to tag it as a negative number since our original
problem is 11 01102 – 1011 11012 where 11 01102 < 1011 11012 .
See if your answers are correct.
Bit Position 7 6 5 4 3 2 1 0
Example: 1 1 0 0 1 0 0 0
To take the two’s complement of this example, leave bits in positions 3, 2, 1, and
0 alone. Take the one’s complement of bits in positions 7, 6, 5, and 4. The answer for
this example is:
Bit Position 7 6 5 4 3 2 1 0
Example: 0 0 1 1 1 0 0 0
In general, find the position of the right-most 1-bit. Take the one’s complement
of all the higher order bits. This approach for finding two’s complement is good for
hardware design.
Decimal 0 1 2 3 4 5 6 7 8 9
Complement
of Decimal 9 8 7 6 5 4 3 2 1 0
(9 – x)
BCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Complement of
1111 1110 1101 1100 1011 1010 1001 1000 0111 0110
BCD
BCD XS-3 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100
Complement of
1100 1011 1010 1001 1000 0111 0110 0101 0100 0011
BCD XS-3
Notice that the complement of BCD XS-3 produces the BCD XS-3 code for
the decimal complement. For example, the BCD XS-3 code for decimal 5 is
binary 1000. The complement is 0111, which is the BCD XS-3 code for
decimal 4. The complement of decimal 5 is 4 (ie, 9 – 5 = 4 ). The BCD XS-3
code produces a useful result.
On the other hand, the BCD code (not BCD XS-3 code) for decimal 5 is
binary 0101. The complement is 1010, which is not a code for any decimal
digit in the BCD (not XS-3) code.
Addition with BCD XS-3
The concept of addition using BCD Excess-3 is simple. Implementation is
easier to handle in hardware than BCD without Excess-3 coding because of
the symmetric placement of the plain text within the code.
The details that need attention are how to handle situa tions that result in a
sum exceeding the code. There are two ways for the sum to overflow: code
overflow and word overflow. In both cases, a carry bit C is set to 1. The
value of C is added to the next higher significant digit.
59
Paul E. Ceruzzi , “Building the Complex Number Computer”, Reckoners: The Prehistory of the Digital
Computer, from Relays to the Stored Program Concept, 1935-1945, Chapter 4: Number, Please: Computers
at Bell Labs, pages 80 – 84, Greenwood Press (1983). http://ed-thelen.org/comp -hist/Reckoners-ch-4.html
visited 05 February 2008.
Number Systems and Data Representation 142
X=4
D=3 P=7
Decimal 0 1 2 3 4
Code 0 1 2 3 4 5 6 7
Binary 0000 0001 0010 0011 0100 0101 0110 0111
Code Overflow
Y=5
Q=8 V=13
Decimal 5 6 7 8 9
Code 8 9 10 11 12 13 14 15
Binary 1000 1001 1010 1011 1100 1101 1110 1111
Code Overflow
Let X and Y be two single digit decimal numbers. Let D be the Displaceme nt of
the Excess code, which is 3 for this system. The BCD XS-3 codes are
• P = X + 3 = X + Excess
• Q = Y + 3 = Y + Excess
To get the coded sum, S = (P + Q) – Excess = (P + Q) – D = (P + Q) – 3 = (X + Y) +
Excess. If the value of S exceeds the valid code, a carry value C must be set to 1 and the
value of S adjusted. There are several special cases as illustrated in the table below.
Table 53. Code and Word Overflow Carry Zones for BCD Excess-3 Addition
0 1 2 3 4 5 6 7 8 9
0 00 01 02 03 04 05 06 07 08 09
1 01 02 03 04 05 06 07 08 09 10
2 02 03 04 05 06 07 08 09 10 11
3 03 04 05 06 07 08 09 10 11 12
4 04 05 06 07 08 09 10 11 12 13
5 05 06 07 08 09 10 11 12 13 14
6 06 07 08 09 10 11 12 13 14 15
7 07 08 09 10 11 12 13 14 15 16
8 08 09 10 11 12 13 14 15 16 17
9 09 10 11 12 13 14 15 16 17 18
Number Systems and Data Representation 143
In the addition table, the right digit is the coded sum, the left digit is the carry
digit to be added to the next higher significant digit. Since the arithmetic is being done in
binary, the carry digit is either a zero or one, and is a single binary digit (bit).
The sum of the BCD XS-3 values ((P – D) + Q) may result in an overflow
condition which produces a wrong answer prior to adjustment. There is no code or word
overflow for the upper left corner. The band that includes (5 + 5) has code overflow but
no word overflow. The band that includes (6 + 7) has both word and code overflow. The
bottom right corner that includes (8 + 8) has word overflow, but no code overflow. When
either overflow condition occurs, the carry bit C is set to 1.
In the (Nassi-Schneiderman) flow chart for BCD Excess-3, the final Sum is S.
Variables E and R are intermediate temporary variables. The value of E incorporates the
value of the carry bit, which allows this addition procedure to apply for higher order
positions of a number. V is the first code position past the decimal table. The quantity
(D – V) adjusts the intermediate sum Result R to wrap around the fixed length table to
obtain the correct BCD XS-3 Sum.
Table 54. Example Set 1 of BCD XS-3 Addition with Input Carry C = 0
Table 55. Example Set 2 of BCD XS-3 Addition with Input Carry C = 0
Table 57. Example Set 2 of BCD XS-3 Addition with Input Carry C = 1
BCD numbers are stored in signed magnitude format rather than storing negative
numbers in radix complement format. This approach is useful for variable length
numbers. This requires determination of the sign of the result.
Even if negative BCD numbers are stored in radix complement format, a sign is
required. Recall for negative binary numbers in complement form, the range of numbers
Number Systems and Data Representation 147
is almost evenly split between positive and negative numbers. The most significant bit
serves as a sign bit. The problem for BCD is that 10 numbers are coded into a 16 number
code. At least a 19 number code is required to represent 9 positive and 9 negative
numbers, and a signless zero. A sign or flag is needed to identify a number
Complement arithmetic can still be used for subtraction. One approach is to
always prepare both BCD numbers in radix complement format before performing the
operation. A hybrid approach tests signs and magnitudes, and take radix complements
only if necessary. This is shown in the modified Nassi-Schneiderman chart below.
BCD XS-3 Subtraction Example Same Sign and |X| < |Y|
Let X = 302 and Y = 608. Find Z = X – Y = – 306. Let the finite word length be
3 decimal digits long.
Let the internal BCD XS-3 code be P = X + 3, Q = Y + 3. Let the BCD XS-3 code
for the sum be S = Z + 3. Let T = Radix Complement, and W = T in BCD XS-3 Code.
Find the radix complement of X.
Decimal BCD XS-3 Binary
X 302 P= 0110 0011 0101BCD XS-3
X 697 1001 1100 1010BCD XS-3
T= X + 1 698 W= 1001 1100 1011BCD XS-3
BCD XS-3 Subtraction Example Same Sign and |X| > |Y|
Let X = – 461 and Y = – 320. Find Z = X – Y = – 141. Let the finite word
length be 3 decimal digits long.
Let the internal BCD XS-3 code be P = |X| + 3, Q = |Y| + 3. Let the BCD XS-3
code for the sum be S = Z + 3. Let T = Radix Complement, and W = T in BCD XS-3
Code.
Find the radix complement of Y.
Decimal BCD XS-3 Binary
|Y| 320 Q= 0110 0101 0011BCD XS-3
Y 679 Q = 1001 1010 1100BCD XS-3
Concentrate on the right- most digit. Apply the rules for BCD XS-3 Addition for the case
of Code Overflow to complete the process of obtaining the radix complement of Y.
Table 61. BCD XS-3 Subtraction for Same Sign and |X|>|Y|
Decimal
|X| 461 P2 0111 P1 1001 P0 0100
Displacement 1– D 1110 –D 1101 –D 1101
|X| |X|2 10101 |X|1 10110 |X|0 10001
T= Y +1 680 W W2 1001 W1 1011 W0 0011
Using a finite length word of 3 decimal digits, discard the overflow digit for Z.
Sign(Z) ßSign(X) = negative = –.
Z = – 141.
Number Systems and Data Representation 151
Binary Multiplication
Multiplication in binary follows the same procedural rules as multiplication in
base ten. It is simpler because the multiplication table is much smaller. Remember how
long it took to memorize the multiplication table? The binary table is much easier! The
inputs are X and Y.
Multiply X
0 1
0 0 0
Y
1 0 1
Example 1: Multiply 110102 x 101112 = 26 x 23 = 598. In the example
below, reserve the row immediately below the multiplier to hold the carry when adding
the columns. Label the columns with the position number, beginning from the right hand
side. The row labeled “Column 0” is the result of multiplying X by the digit found in
Column 0 of Y. The row labeled “Column 1” is the result of multiplying X by the digit
found in Column 1 of Y. After multiplying X by each digit of Y, add the resulting
rows. Be sure to account for any carries from the previous column. The number of digits
in the answer will be the total number of digits in the individual multipliers.
Column
4 3 2 1 0
Position Number
X 1 1 0 1 0
Y 1 0 1 1 1
Carry 1 1 1 10 1 1 0 0 0
Column 0 1 1 0 1 0
Column 1 1 1 0 1 0
Column 2 1 1 0 1 0
Column 3 0 0 0 0 0
Column 4 1 1 0 1 0
Sum 1 0 0 1 0 1 0 1 1 0
Check the answer by converting 10010101102 to decimal.
Place Value 512 256 128 64 32 16 8 4 2 1
Sum 1 0 0 1 0 1 0 1 1 0
The answer is 598.
Number Systems and Data Representation 152
60
Yap Siong Chua, CDA 4101 class notes, U. North Florida, Jacksonville, FL (1981).
61
Israel Koren, “Booth’s Algorithm”, (09 December 2005).
http://www.ecs.umass.edu/ece/koren/arith/simulator/Booth/ Visited 04 January 2008.
Number Systems and Data Representation 153
Binary Multiplication
Multiply 111012 by 101102 . = 29 x 22. Show all your work. The answer is 638.
Column
4 3 2 1 0
Position Number
X 1 1 1 0 1
Y 1 0 1 1 0
Carry
Column 0
Column 1
Column 2
Column 3
Column 4
Sum
Check the answer by converting it to decimal.
Place Value 512 256 128 64 32 16 8 4 2 1
Sum
Answer =
Number Systems and Data Representation 154
Hexadecimal Multiplication
Remember memorizing the times-ten table? You can do the same for
hexadecimal. The multiplication table for hexadecimal is given below. Use this for
hexadecimal multiplication just as you use the decimal multiplication table for decimal
multiplication.
Table 62. Hexadecimal Multiplication Table
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 2 3 4 5 6 7 8 9 A B C D E F
2 0 2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E
3 0 3 6 9 C F 12 15 18 1B 1E 21 24 27 2A 2D
4 0 4 8 C 10 14 18 1C 20 24 28 2C 30 34 38 3C
5 0 5 A F 14 19 1E 23 28 2D 32 37 3C 41 46 4B
6 0 6 C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A
7 0 7 E 15 1C 23 2A 31 38 3F 46 4D 54 5B 62 69
8 0 8 10 18 20 28 30 38 40 48 50 58 60 68 70 78
9 0 9 12 1B 24 2D 36 3F 48 51 5A 63 6C 75 7E 87
A 0 A 14 1E 28 32 3C 46 50 5A 64 6E 78 82 8C 96
B 0 B 16 21 2C 37 42 4D 58 63 6E 79 84 8F 9A A5
C 0 C 18 24 30 3C 48 54 60 6C 78 84 90 9C A8 B4
D 0 D 1A 27 34 41 4E 5B 68 75 82 8F 9C A9 B6 C3
E 0 E 1C 2A 38 46 54 62 70 7E 8C 9A A8 B6 C4 D2
F 0 F 1E 2D 3C 4B 5A 69 78 87 96 A5 B4 C3 D2 E1
Example: Multiply X = OxA4B2 times Y = C316 .
Sum Carry 1 1
Carry term 0 1 2
Carry term 1 7 3 8 1
X A 4 B 2
times Y C 3
E C 1 6
8 0 4 8
Answer 7 D 7 3 9 6
The answer is 7D7396H.
In the above example, “Carry term 0” contains the carry terms from multiplying
by 3. “Carry term 1” contains the carry terms from multiplying by C. “Sum Carry”
contains the carry terms from addition in the adjacent right column.
Number Systems and Data Representation 155
Answer
Answer =
Number Systems and Data Representation 156
1 0 1. 1 1
11011011 1 0 0 1 1 1 0 1 0 1 1. 0 1
1 1 0 1 1 0 1 1
1 0 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1
1 0 1 0 0 1 0 0 0
1 1 0 1 1 0 1 1
1 1 0 1 1 0 1 1
1 1 0 1 1 0 1 1
0 0 0 0 0 0 0 0
The answer is 101.112 . This result is consistent with Example 2 of fixed point
multiplication.
.
110101 1 0 1 1 1 0 0 .1 1
Number Systems and Data Representation 157
Decimal Fractions
Fraction Base 2 Base 3 Base 5 Base 7 Base 10 Base 12 Base 16
1/10 0.00011 0.0022 0.02 0.0462 0.1 0.12497 0.19
2/10 0.0011 0.0121 0.1 0.1254 0.2 0.2497 0.3
Failure to account for the inability to exactly represent decimal fractions using
fixed length binary words in the design of the Patriot missile software resulted in the loss
of 28 lives in a Scud attack on 25 FEB 1991 on the American barracks in Dharan, Saudi
Arabia during the Gulf War. See the article by Arnold. 62
Beware of thinking that base 10 is necessarily the best based upon observing that
so many of the fractions above can be expressed exactly with a finite length number. If
God had given us 7 digits, instead of 10, at the end of our upper appendages, we would
be more inclined to prefer the fractions 1/7, 2/7. 3/7, 4/7, 5/7, 6/7, and 1 = 7/7.
62
Douglas N. Arnold, “The Patriot Missile Failure” (23 AUG 2000).
http://www.ima.umn.edu/~arnold/disasters/patriot.html Visited 04 JUN 2002.
Number Systems and Data Representation 159
63
Chris K. Caldwell, “Euclid's Proof of the Infinitude of Primes (c. 300 BC)”, The University of Tennessee
at Martin. http://primes.utm.edu/notes/proofs/infinite/euclids.html Visited 04 January 2008.
64
Aine Donovan, David E. Johnson, George R. Lucas, Jr., and Paul E. Roush (eds.), Ethics for Military
Leaders, Simon and Schuster (1998).
65
J J O'Connor and E F Robertson, “Euclid of Alexandria”, School of Mathematics and Statistics,
University of St. Andrews, Scotland (January 1999).
http://www-groups.dcs.st-and.ac.uk/~history/Mathematicians/Euclid.html, 13 September 2002.
Number Systems and Data Representation 160
Divide by
5 7
1 1 1 0
101 1 0 0 1 1 111 1 0 0 1 1
1 0 1 1 1 1
1 0 0 1 1 0 1
1 0 1 0
R2= 1 0 0 R2= 1 0 1
R10 =4 R10 =5
Number Systems and Data Representation 161
11 13
1 1
1011 1 0 0 1 1 1101 1 0 0 1 1
1 0 1 1 1 1 0 1
R2= 1 0 0 0 R2= 1 1 0
R10 =8 R10 =6
17 19
1 1
10001 1 0 0 1 1 10011 1 0 0 1 1
1 0 0 0 1 1 0 0 1 1
R2= 1 0 R2= 0
R10 =2 R10 =0
The first divisor not equal to one that results in a remainder of zero is
100112 = 1910 . Therefore, this is a prime number.
Base 3 Demonstration that 19 is prime.
Divide by
2 3
1 0 0 2 0
2 2 0 1 10 2 0 1
2 2 0
1 1
0 0
R3= 1 R3= 1
R10 =1 R10 =1
Divide by
5 7
1 0 2
12 2 0 1 21 2 0 1
1 2 1 0 2
1 1 R3= 1 2
0 R10 =5
R3= 1 1
R10 =4
Number Systems and Data Representation 162
11 13
1
102 2 0 1 111 2 0 1
1 0 2 1 1 1
R3= 2 2 R2= 2 0
R10 =8 R10 =6
17 19
1 1
122 2 0 1 201 2 0 1
1 2 2 2 0 1
R2= 2 R2= 0
R10 =2 R10 =0
The first divisor not equal to one that results in a remainder of zero is 2013 = 1910 .
Therefore, this is a prime number.
Base 5 Demonstration that 19 is prime.
Divide by
2 3 5
1 4 1 1 3
2 3 4 3 3 4 5 3 4
2 3 3 0
1 4 0 4 R3= 4
1 3 3 R10 =4
R5= 1 R3= 1
R10 =1 R10 =1
Divide by
7 11
2 1
12 3 4 21 3 4
2 4 2 1
R3= 1 0 R3= 1 3
R10 =5 R10 =8
Number Systems and Data Representation 163
Divide by
13 17 19
1 1 1
23 3 4 32 3 4 34 3 4
2 3 3 2 3 4
R3= 1 1 R3= 2 R3= 0
R10 =6 R10 =2 R10 =0
The first divisor not equal to one that results in a remainder of zero is 345 = 1910 .
Therefore, this is a prime number.
Prime Number Problems
Problem 1. a. Show that 7 is a prime number using base 2.
b. Show that 7 is a prime number using base 3.
c. Compare the results.
Number Systems and Data Representation 164
66
Mitchell N. Charity, “An Exponential Notation Meta Page” (10 August 2003).
http://www.vendian.org/mncharity/export1/exponential_notation_meta/ Visited 04 January 2008.
67
Handbook of Mathematical Functions With Formulas, Graphs, and Mathematical Tables, Applied
Mathematics Series, AMS-55, Milton Abramowitz and Irene A. Stegun (eds.), National Bureau of
Standards, U.S. Department of Commerce, U.S. Government Printing Office (June 1964).
68
“Unit of Amount of Substance (Mole)”, The International System of Units (SI), Section 2.1.1.6:, Page 8,
NIST Special Publication 330, 2001 Edition, Barry N. Taylor (editor), National Institute of Standards and
Technology (July 2001).
Number Systems and Data Representation 165
mantissa of a number written in scientific notation has one non-zero number to the left of
the decimal point.
The “23” portion of the number is called the characteristic or exponent. The
exponent can be a positive or negative integer. When the exponent is zero, the number is
written without the exponent term. The number “10” is the exponent base.
Avagadro’s number is
6022 16900 00000 00000 00000 = 6.02216 9 x 10 23
Very small numbers written in scientific notation have negative powers of the
base, such as the gravitational constant,
G = 6.6732 x 10 –11 N m2 / kg2
The number
– q = - 1.60219 17 x 10 –19 C
is the elementary charge of an electron, measured in Coulombs. The mantissa is
“-1.6021917”. The characteristic is “-19”. The charge of an electron is negative, as
indicated by the minus sign of the mantissa. The value of the charge of a single electron
is very much less than one, as indicated by minus sign in the characteristic. Written in
fixed point notation, this number is
– 0. 00000 00000 00000 00016 02191 7
3.0 • 10 5
3.0 2
69
Encyclopedia.com, “Light Year”, http://www.encyclopedia.com. 01 July 2002.
70
George Goth, “The Magnitudes of Physics”, Skyline College, San Bruno CA (08 Aug 2001)
http://www.smccd.net/accounts/goth/MainPages/magphys.htm, 01 July 2002.
Number Systems and Data Representation 167
1 1 3
0.112 = 2 −1 + 2 −2 = + = = 0.75
2 4 4
The binary exponent 1102 equals decimal 6. The value of the exponent term is
2 6 = 6410
The decimal value of the original number is
0.75 x 64 = 48
On a different processor, this same number might have a floating point
representation as
1.1 x 2 109
Number Systems and Data Representation 168
=
0.1001 1001−101
• 2
0.1101
0.1101 0. 1 0 0 1 0 0 0 0 0 0 0 0
The first step is to shift the radix point in both terms to the right by the same number of
places until the divisor is an integer. In this example, shift the radix point by 4 places. In
fractional notation, this is
0.1001 1001
=
0.1101 1101
.1 0 1 1 0 0 0 1 0 0 1 0 0 1
1101 1 0 0 1 .0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 1
1 0 1 0 0
1 1 0 1
1 1 1 0
1 1 0 1
1 0 0 0 0
1 1 0 1
1 1 0 0 0
1 1 0 1
1 1 0 0 0
The answer is
0.1011 0001 0010 01 x 2100
= 0.1011 0001 x 2100
= 1011.0001
Notes:
0.1001 x 21001 = 1001 x 2101 = 9 x 25 = 9 x 32 = 288
0.1101 x 2101 = 1101 x 21 = 13 x 2 = 26
288/26 = 11.076923
Number Systems and Data Representation 170
0.101 0. 1 1 1 0 1 0 0 0 0 0 0 0
Shift the radix point in both terms to the right by the same number of places until
the divisor is an integer. In this example, shift the radix point by 3 places. Perform the
division.
1 .0 1 1 1 0 0 1
101 1 1 1 .0 1 0 0 0 0 0
1 0 1
1 0 0 1
1 0 1
1 0 0 0
1 0 1
1 1 0
1 0 1
1 0 0 0
1 0 1
1 1 1
The result of the division is 1.0111 00 x 2 –101 . Normalize the resulting floating
point notation:
1.0111 00 x 2 –101 = ( 0.1011 100 x 2 1 ) x 2 –101
= 0.1011 100 x ( 2 1 x 2 –101 )
= 0.1011 100 x 2 – 100
= 0.0000 1011 100
Number Systems and Data Representation 171
0.11 0 .1 1 1 0 0 0 0 0 0 0 0 0
Shift the radix point in both terms to the right by the same number of places until
the divisor is an integer. In this example, shift the radix point by 2 places. Perform the
division.
0 .1 1 1 1 0 1 0 1
11 1 0 .1 1 1 0 0 0 0 0
1 1
1 0 1
1 1
1 0 1
1 1
1 0 0
1 1
1 0 0
1 1
1 0 0
The answer in binary is
0.1111 01 x 21001
= 1 1110 1010.10
= 256 + 128 + 64 + 32 + 8 + 2 + (2/3) = 490.6
Several technical references for the following discussion are valuable for future
computer scientists and computer engineers. 71 72 73 74
Negative Numbers
In the discussion that follows, mention will occasionally be made that some
methods of representing numbers result in two representations for zero, +0 and –0. For
testing equality, you usually want a test that +0 = -0 to be true, and it would seem that a
unique value for zero would always be desirable. Alas, such is not always the case.
Distinguishing between these is useful when dealing with something called “branch
cuts”, which is a topic in complex variables. 75 Scientists, engineers, and mathematicians
worry about such things. Computer scientists that do not work with engineers and
scientists, and people involved in business data processing, do not need to consider this.
More will be said about this when discussing IEEE formats for floating point numbers.
How do you provide for negative numbers? There are three major ways of doing
this.
• signed magnitude
• complement
• bias or Excess (XS)
For purpose of discussion, pretend that we are restricted to a 4-bit word. It is
important to start numbering bits from the right hand side, starting with zero. The left-
most bit is called the Most Significant Bit (MSB), and the right-most bit is called the
Least Significant Bit (LSB). For example:
3 2 1 0
MSB LSB
Signed Magnitude
The sign bit approach is to reserve one bit for use to indicate the sign of the
number. When we compare two numbers, a positive number should appear to be larger
than a negative number. It makes sense to assign the most significant bit to be the sign
bit. If a sign bit equal to zero represents a negative sign, and a sign bit equal to one
represents a positive sign, we get the right outcome when we do a comparison.
71
P. H. Abbott, D. G. Brush, C. W. Clark III, C. J. Crone, J. R. Ehrman, G. W. Ewart, C. A. Goodrich, M.
Hack, J. S. Kapernick, B. J. Minchau, W. C. Shepard, R. M. Smith, Sr., R. Tallman, S. Walkowiak, A.
Watanabe, and W. R. White, “Architecture and software support in IBM S/390 Parallel Enterprise Servers
for IEEE Floating-Point arithmetic” IBM Journal of Research and Development, Volume 43, Numbers 5/6
(1999), http://www.ibm.com, 08 June 2002
72
ESA/390 Principles of Operation, International Business Machines, Document Number SA22-7201-06
(1999), http://publib.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/DZ9AR006/CCONTENTS , 10 Jun 2002.
73
Intel® ItaniumTM Architecture Software Developer’s Manual, Volume 1, Application Architecture,
Revision 2.0 (December 2001).
74
David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”,
Computing Surveys, Association for Computing Machinery (March 1991).
75
W. Kahan, “Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit”;
Chapter 7 in The State of the Art in Numerical Analysis ed. by M. Powell and A. Iserles (1987) Oxford.
Number Systems and Data Representation 173
± 2 1 0
MSB LSB
A complication to using the sign bit approach is that there are two possible
representations of the number zero: a plus- zero, and a minus-zero. The trichotomy test
classifies a number as being
• less than zero
• equal to zero
• greater than zero
When there are two possible representations for zero, the test becomes more
complicated. You must test for both a positive and a negative zero. Two representations
for zero means that one combination of bits is being wasted that could be used to
represent another number. For our example signed nibble, we can represent only 14
unique numbers.
Complement
Negative numbers can be represented in binary using either one’s complement or
two’s complement. Complement arithmetic has the advantage of using positive integer
addition for adding positive and negative numbers. Like the signed magnitude approach,
one’s complement representation has the disadvantage of having two representations for
the number zero.
Two’s complement (radix complement) arithmetic does not have the disadvantage
of ambiguous representations of zero. If you declare that numbers having a one in the
most significant bit is a negative number, you can use two’s complement to
unambiguously represent negative numbers. If you do not agree to that convention, you
have ambiguity. Consider the bit pattern in an example nibble:
0 1 1 0
Is the intended number plus six, or minus ten?
Bias or Excess
Rather than interpreting the most significant bit as an indicator of sign, the bias
approach is to declare that the B smallest numbers are negative numbers. Let X be the
value we want to represent. The number Y is the stored value, which is computed as
Y = X + B. In our 4-bit word, suppose we declare a bias of 4. We have 16 possible
numbers, with the range [-4, 11].
0 0 1 0 is – 2
while the pattern
0 1 1 0 is + 2
and
1 1 1 1 is + 11
This approach allows you to partition your set of numbers in an arbitrary fashion.
Bias representation imposes additional procedural requirements on arithmetic operations.
Number Systems and Data Representation 174
Suppose you want to add -1 plus -1 using a bias of 4. Naïve addition gives us
-1+4=3 0 0 1 1
+ 0 0 1 1
Sum = 6 0 1 1 0
The sum of 6 corresponds to +2, which is not what we want. We want to compute
X1 + X2 = (-1) + (-1) = -2. To get the desired answer, add the two numbers and
subtract the bias. The required computation is
Yanswer = Y1 + Y2 - B
Addition is commutative; you can exchange order of addition without changing
the result. Subtracting the bias before doing the addition can preserve precision in the
result at intermediate steps when using a finite length word for arithmetic, such as when
using a real computer.
Yanswer = Y1 - B + Y2
Bias representation of negative numbers turns out to be very useful when using
exponents, with a desire to limit the number of places reserved for fractions (negative
exponents). The amount of bias is designated in the name. The notation above would be
called XS-4 notation, where “XS” means “Excess”.
It is common on binary computers to allocate half the range of numbers that can
be represented to be negative numbers, though you could design a system differently.
The bias is usually set to be some power of two, or just one less than a power of two,
depending on the processor.
IEEE excess notation declares that when the most significant bit is on, the
represented number is a positive integer. The bit pattern of the excess is all ones, except
for the most significant bit which is zero. When this is done, the most significant bit can
be interpreted as a sign bit, with a negative number having the sign bit turned off. For an
8-bit number, this is called XS-127. The range of possible numbers for this situatio n is
[ -127, 128 ]. A zero characteristic is represented by 0111 11112 .
IBM hexadecimal floating point excess notation declares that when the most
significant bit is turned on, the represented number is a positive integer or zero. The bit
pattern of the excess is all zeros, except for the most significant bit which is a one. For a
7-bit number (which IBM uses), this is called XS-64. The range of possible numbers for
this situation is [ -64, 63 ]. A zero characteristic is represented by 100 00002 .
IEEE IBM
# Bits Decimal Hex Decimal Hex Equivalent
Excess Excess Excess Excess Binary XS
7 64 80 256
8 127 7F
11 1023 7FF
15 16383 7FFF
17 65535 1FFFF
Number Systems and Data Representation 175
Block Normalization
Consider a different normalization rule, Block-2. For each increment of the
characteristic, shift the mantissa 2 places instead of 1. Apply this new rule to the
example bit patterns above. The first one,
7 6 5 4 3 2 1 0
0 1 1 0 0 0 1 1
± Mantissa Characteristic
now has a characteristic of 2 x 3 = 6. The value in binary of 0.11 x 2 6 = 110000.02 .
The number
7 6 5 4 3 2 1 0
0 1 1 0 0 1 0 1
± Mantissa Characteristic
has a two’s complement characteristic of 2 x ( -3 ) = -6 . The value of the floating point
number is 0.11 x 2 – 6 = 0.0000 00112 = 0.0117 1875.
Notice that the range of numbers you can represent has dramatically increased.
You can represent numbers much closer to one, and much farther from one. This also
reduces the frequency of having to do normalization. However, you can still only
represent 255 different numbers. For our 8-bit floating point number, here are some of
the properties for different decisions for normalization. The binary number closest to one
that you can represent is given by
7 6 5 4 3 2 1 0
0 See table below. 1 0 0
± Mantissa Characteristic
The positive binary number farthest from one that you can represent is given by
7 6 5 4 3 2 1 0
0 1 1 1 1 0 1 1
± Mantissa Characteristic
Number Systems and Data Representation 177
IEEE Standards 754 (1985) and 854 for Floating Point Arithmetic
The Institute of Electrical and Electronics Engineers (IEEE) standard for floating
point arithmetic is the industry standard for design. 78 It defines the format for floating
point numbers and the methods for doing computation.
76
Douglas N. Arnold, “The Explosion of the Ariane 5” (23 AUG 2000).
http://www.ima.umn.edu/~arnold/disasters/ariane.html visited 04 JUN 2002.
77
Ron Zucker, “Floating Point Conversion Using Excess Notation”, University of North Florida.
http://www.unf.edu/~rzucker/cot3100dir/excess.html visited 07 January 2008.
78
P. H. Abbott, D. G. Brush, C. W. Clark III, C. J. Crone, J. R. Ehrman, G. W. Ewart, C. A. Goodrich, M.
Hack, J. S. Kapernick, B. J. Minchau, W. C. Shepard, R. M. Smith, Sr., R. Tallman, S. Walkowiak, A.
Watanabe, and W. R. White, “Architecture and software support in IBM S/390 Parallel Enterprise Servers
Number Systems and Data Representation 179
X = ±2 (Characteristic−Excess ) • (1 + Mantissa )
The maximum number that can be represented with this format is approximately
2 x 2127 ≈ 3.4 x 1038 . The smallest normalized number that can be represented is
approximately 2 x 2 – 127 ≈ 1.2 x 10 – 38 . The smallest subnormal number that can be
represented is approximately 2 x 2 – 127 x 2 – 23 = 2 x 2 – 150 ≈ 1.4 x 10 – 45 .
IEEE Double Precision Floating Point
IEEE double precision floating point has an 11-bit characteristic using Excess-
1023 ( 210 – 1 ) notation, and 52-bit mantissa in signed magnitude form, with implicit-bit
binary normalization, where the implicit bit is in the 20 position. The sign bit is the sign
of the mantissa, with 0 indicating positive and 1 indicating negative. Therefore, the
mantissa has 53-bit precision.
X = ±2 (Characteristic−Excess ) • (1 + Mantissa )
The maximum number that can be represented is approximately
2x2 1023
≈ 1.8 x 10308 . The smallest normalized number that can be represented is
for IEEE Floating-Point arithmetic” IBM Journal of Research and Development, Volume 43, Numbers 5/6
(1999), http://www.ibm.com, 08 June 2002.
Number Systems and Data Representation 180
approximately 2 x 2 – 1023 ≈ 2.2 x 10 – 308 . The smallest subnormal number that can be
represented is approximately
2 x 2 – 1023 x 2 – 52 = 2 x 2 – 1075 ≈ 4.9 x 10 – 324 .
79
Intel® ItaniumTM Architecture Software Developer’s Manual, Volume 1, Application Architecture,
Revision 2.0 (December 2001).
80
ESA/390 Principles of Operation, International Business Machines, Document Number SA22-7201-06
(1999). http://publib.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/DZ9AR006/CCONTENTS , Visited 10
Jun 2002.
Number Systems and Data Representation 182
Because the minimum value that can be stored in the low order characteristic field
is zero, the minimum characteristic that can be used in the high order characteristic is 14.
The range of values that can be used in the high order characteristic is [-50, 63].
Remember that these are powers of 16. IBM Hexadecimal Extended Floating Point
therefore cannot represent numbers as close to zero as IBM Hexadecimal Long Floating
Point. To help keep this in perspective, the moment of a Proton is approximately
1.4 x 10 – 26 J/T (Joules / Tesla).
Binary Short Floating Point
The IBM Binary Short Floating Point word conforms to the IEEE Single
Precision Floating Point word specifications.
Binary Long Floating Point
The IBM Binary Long Floating Point word conforms to the IEEE Double
Precision Floating Point word specifications.
Binary Extended Floating Point
81
Chris Impey, “Reacting to the Size and Shape of the Universe”, Mercury Magazine Vol. 30 No. 1
(January/February 2001). http://www.aspsky.org/mercury/mercury/30_01/universe.html Visited 05 July
2002.
Number Systems and Data Representation 184
Mantissa (Continued)
16 20 24 28 31
0 0 1 1 1 1 0 1 0 0 0 1 0 0 0 1
Step 1: Let M = (IEEE Single Precision Floating Point characteristic) – (IEEE Single
Precision Floating Point Excess). M = 145 – 127 = 18.
Note: The IEEE format uses implicit normalization. The radix point is immediately
to the left of the IEEE mantissa most significant bit.
Step 2: Insert the prefix bit pattern 0001 immediately to the left of the radix point.
The appended mantissa is 0001.001 1001 0011 1101 0001 0001
Step 3: Let MM = M MOD 4. This is the remainder obtained by dividing M by 4.
MM = 18 MOD 4 = 2
Step 4: Compute the number of bits (J) to right-shift the mantissa and prefix bit
pattern.
• If MM = 0, then J = 4
• If MM < 0, then J = |MM|. |MM| is the absolute value of MM.
• If MM > 0, then J = 4 – MM = 4 – 2 = 2.
The number of bits of precision in the mantissa lost is J – 1 = 2 – 1 = 1.
Step 5: Copy the shifted bit pattern into the IBM Short HFP mantissa. The pattern
copied is: 01001 1001 0011 1101 0001 0001
Step 6: Compute the IBM HFP characteristic, without excess.
M + 4 18 + 4 22
• K= = = = 5.5 = 5 .
4 4 4
Step 7: Add the IBM HFP excess to K and store it in the HFP characteristic field. K
+ 64 = 5 + 64 = 69 = 4516 = 100 01012 .
Step 8: Copy the sign bit from the IEEE Single Precision FP word to the sign bit of
the IBM HFP word.
Mantissa (Continued)
16 20 24 28 31
1 0 0 1 1 1 1 0 1 0 0 0 1 0 0 0
Number Systems and Data Representation 186
Mantissa (Continued)
16 20 24 28 31
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Mantissa (Continued)
16 20 24 28 31
Problem 2. Convert the following IEEE Single Precision Floating Point word to a
IBM Hexadecimal Short Floating Point word.
Number Systems and Data Representation 188
Mantissa (Continued)
16 20 24 28 31
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Mantissa (Continued)
16 20 24 28 31
Problem 3. Convert the following IEEE Single Precision Floating Point word to a
IBM Hexadecimal Short Floating Point word.
IEEE Single Precision Floating Point
S Characteristic Mantissa
0 1 4 8 9 12 15
1 0 1 1 1 1 1 0 1 0 0 1 0 0 0 0
Mantissa (Continued)
16 20 24 28 31
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Mantissa (Continued)
16 20 24 28 31
Problem 4. Convert the following IBM Hexadecimal Extended Floating Point word to
an IEEE Double Extended Precision Floating Point word.
Number Systems and Data Representation 189
Mantissa
63 60 56 52 48
Number Systems and Data Representation 190
Mantissa (Continuation 1)
47 44 40 36 32
Mantissa (Continuation 2)
31 28 24 20 16
Mantissa (Continuation 3)
15 12 8 4 0
Problem 5. Convert the following IEEE Double Extended Precision Floating Point word
to an IBM Hexadecimal Extended Floating Point word.
IEEE Double Extended Precision Floating Point
S Characteristic
79 78 75 72 71 67 64
1 0 1 1 1 1 1 1 0 1 0 0 1 0 1 0
Mantissa
63 60 56 52 48
0 1 1 1 0 0 1 0 1 1 1 1 1 0 1 1
Mantissa (Continuation 1)
47 44 40 36 32
1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 0
Mantissa (Continuation 2)
31 28 24 20 16
1 1 1 1 1 0 0 0 0 1 1 0 1 1 0 0
Mantissa (Continuation 3)
15 12 8 4 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
Number Systems and Data Representation 191
Problem 6. Convert the following IEEE Double Extended Precision Floating Point word
to an IBM Hexadecimal Extended Floating Point word.
IEEE Double Extended Precision Floating Point
S Characteristic
79 78 75 72 71 67 64
1 0 1 1 1 1 1 1 0 0 0 0 1 0 1 1
Mantissa
63 60 56 52 48
0 1 1 1 0 0 1 0 1 1 1 1 1 0 1 1
Mantissa (Continuation 1)
47 44 40 36 32
1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 0
Mantissa (Continuation 2)
31 28 24 20 16
1 1 1 1 1 0 0 0 0 1 1 0 1 1 0 0
Mantissa (Continuation 3)
15 12 8 4 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
Problem 7. Convert the following IEEE Double Extended Precision Floating Point word
to an IBM Hexadecimal Extended Floating Point word.
IEEE Double Extended Precision Floating Point
S Characteristic
79 78 75 72 71 67 64
1 0 1 1 1 0 1 1 1 0 0 1 0 1 1 0
Mantissa
63 60 56 52 48
0 1 1 1 0 0 1 0 1 1 1 1 1 0 1 1
Mantissa (Continuation 1)
47 44 40 36 32
1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 0
Mantissa (Continuation 2)
31 28 24 20 16
1 1 1 1 1 0 0 0 0 1 1 0 1 1 0 0
Mantissa (Continuation 3)
15 12 8 4 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
Number Systems and Data Representation 194
Numerical Methods
Numerical methods are procedures for solving mathematical problems
numerically, including problems for which no closed form solutions exist. Attention is
given to designing procedures that make efficient use of a computer while controlling
computational errors. Analysis includes quantifying errors, approximation or
convergence to solutions, and sensitivity of solutions to variations of inputs.
Number Systems and Data Representation 195
82
James P. Keener, Principles of applied mathematics: Transformation and approximation, Pages 424 –
428. Addison-Wesley (1988).
83
Weisstein, Eric W. "Approximation Theory.", MathWorld--A Wolfram Web Resource (10 February
2008). http://mathworld.wolfram.com/ApproximationTheory.html visited 12 February 2008.
Number Systems and Data Representation 196
84
Richard B. Katz, “Arithmetic”, NASA Office of Logic Design (15 January 2002).
http://www.klabs.org/DEI/Arithmetic/ Visited 14 January 2008.
Number Systems and Data Representation 197
85
Jim Heckroth (Crystal Semiconductor Corp), “Tutorial on MIDI and Music Synthesis”, MIDI
Manufacturers Association (2001). http://www.midi.org/about-midi/tutorial/tutor.shtml Visited 14 January
2008.
86
Daniel Taupin, MusiXTeX, “Directory: CTAN home / tex-archive/ macros/ musixtex/ taupin “ CTAN:
The Comprehensive TeX Archive Network. http://www.ctan.org/tex-archive/macros/musixtex/taupin/
Visited 14 January 2008.
87
http://cyranosciences.com/technology/onboard.html Visited 2001. Not available on 14 January 2008.
88
Charles Platt, “You’ve Got Smell!”, Wired News (November 1999).
http://www.wired.com/wired/archive/7.11/digiscent_pr.html Visited 14 January 2008.
89
“Amazon To Market Online Smells” (1998). http://www.rayno.com/amasmell.htm Visited 14 January
2008.
Number Systems and Data Representation 198
Obviously, if you are looking at a black and white copy of this, you are not going
to see the colors. You can see the colors by viewing an electronic copy. There are
several very nice web sites with color code charts for use with HTML.
Number Systems and Data Representation 199
Analog Computers
The term “analog” refers to devices that are not binary digital computers. Analog
computers rely on electromechanical, electronic, and other components to represent,
retain, and transform continuous values. These values may be used for subsequent
computation, displayed, or used as control signals to other devices. Analog computers
tend to be special purpose devices, sometimes quite sophisticated.
Digital electronic computers are all analog devices at the most basic level.
Computers that are part digital and part analog are called hybrid computers.
While digital computers have made a tremendous contribution, they are still
limited by the speed at which inputs can be digitized and functions computed. This is a
significant limitation in some applications of data acquisition and signal processing.
Critical issues for analog devices include the accuracy of converting data,
repeatability, and reliability. Such devices are sensitive to its environment: heat,
electromagnetic fields, vibration, humidity, gravity, etc.
We continue to push for extreme performance in computation and storage speed,
small size, low power consumption, and other properties. Scientists and engineers are
pushing the boundaries of physics, materials science and biological sciences to find new
and reliable ways to compute.