Professional Documents
Culture Documents
Introduction
Bit / Byte
The most basic unit of information in a digital computer is called a bit, which is
a contraction of binary digit.
A bit has two numerical values: 0 and 1.
In the concrete sense, a bit is nothing more than a state of “on” or “off ” (or
“high” and “low”) within a computer circuit.
In 1964, the designers of the IBM System/360 computer established a
convention of using groups of 8 bits as the basic unit of addressable computer
storage.
They called this collection of 8 bits a byte.
4
Computer words
Computer words consist of two or more adjacent bytes that are sometimes
addressed and almost always are manipulated collectively.
The word size represents the data size that is handled most efficiently by a
particular architecture.
Words can be 16 bits, 32 bits, 64 bits, or any other size that makes sense in the
context of a computer’s organization (including sizes that are not multiples of
eight).
An 8-bit byte can be divided into two 4-bit halves called nibbles (or nybbles).
Because each bit of a byte has a value within a positional numbering system,
the nibble containing the least-valued binary digit is called the low-order
nibble, and the other half the high-order nibble.
5
Digital systems
Digital systems have such a prominent role in everyday life that we refer to the
present technological period as the digital age.
Digital systems are used in communication, business transactions, traffic
control, medical treatment, etc.
We have digital telephones, digital televisions, digital cameras, digital
computers, etc.
We enjoy music downloaded to our portable media player and other handheld
devices having high-resolution displays and touch-screen graphical user
interfaces (GUIs).
GUIs enable them to execute commands that appear to the user to be simple,
but which, in fact, involve precise execution of a sequence of complex internal
instructions.
Most, if not all, of these devices have a special-purpose digital computer, or
processor, embedded within them.
6
Digital systems
Digital systems
Binary Numbers
However, the convention is to write only the numeric coefficients and, from
their position, deduce the necessary powers of 10, with powers increasing
from right to left. In general, a number with a decimal point is represented by
a series of coefficients: …a5a4a3a2a1a0.a-1a-2a-3...
The coefficients aj are any of the 10 digits (0, 1, 2, ...,9), and the subscript
value j gives the place value and, hence, the power of 10 by which the
coefficient must be multiplied. Thus, the preceding decimal number can be
expressed as:
…105a5 + 104a4 + 103a3 + 102a2 + 101a1 + 100a0 + 10-1a-1 + 10-2a-2 + 10-3a-3…
with a3 = 7, a2 = 3, a1 = 9, and a0 = 2, and the other coefficients equal to zero.
9
Binary Numbers
The radix (base) of a number system determines the number of distinct values
that can be used to represent any arbitrary number.
The decimal number system is said to be of base, or radix, 10 because it uses
10 digits and the coefficients are multiplied by powers of 10.
The binary system is a different number system. The coefficients of the binary
number system have only two possible values: 0 and 1.
Each coefficient aj is multiplied by a power of the radix, for example, 2j, and the
results are added to obtain the decimal equivalent of the number.
The radix point (e.g., the decimal point when 10 is the radix) distinguishes
positive powers of 10 from negative powers of 10.
For example, the decimal equivalent of the binary number 11010.11 is 26.75,
as shown from the multiplication of the coefficients by powers of 2:
1 * 24 + 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 + 1 * 2-1 + 1 * 2-2 = 26.75
10
Binary Numbers
There are many different number systems.
In general, a number expressed in a base-r system has coefficients multiplied
by powers of r: an * rn + an - 1 * rn - 1 + … + a2 * r2 + a1 * r + a0 + a-1 * r-1 + a-2 * r-2 +
… + a-m * r-m
The octal number system is a base-8 system that has eight digits: 0, 1, 2, 3, 4, 5,
6, 7.
An example of an octal number is (127.4)8.
To determine its equivalent decimal value, we expand the number in a power
series with a base of 8: (127.4)8 = 1 * 82 + 2 * 81 + 7 * 80 + 4 * 8-1 = (87.5)10
Note that the digits 8 and 9 cannot appear in an octal number.
11
Binary Numbers
It is customary to borrow the needed r digits for the coefficients from the
decimal system when the base of the number is less than 10.
The letters of the alphabet are used to supplement the 10 decimal digits when
the base of the number is greater than 10.
For example, in the hexadecimal (base-16) number system, the first 10 digits
are borrowed from the decimal system.
The letters A, B, C, D, E, and F are used for the digits 10, 11, 12, 13, 14, and 15,
respectively.
An example of a hexadecimal number is:
(B65F)16 = 11 * 163 + 6 * 162 + 5 * 161 + 15 * 160 = (46 687)10
12
Binary Numbers
• In computer work, 210 is referred to as K (kilo), 220 as M (mega), 230 as G (giga),
and 240 as T (tera).
• Thus, 4K = 212 = 4 096 and 16M = 224 = 16 777 216.
• Computer memory capacity and word size are usually given in bytes.
• A computer hard disk with four gigabytes of storage has a capacity of 4G = 232
bytes (approximately 4 billion bytes).
• A terabyte is 1024 gigabytes, approximately 1 trillion bytes.
13
Binary Numbers
• Arithmetic operations with numbers in base r follow the same rules as for
decimal numbers.
• When a base other than the familiar base 10 is used, one must be careful to
use only the r-allowable digits.
• Examples of different arithmetic operations on two binary numbers are as
follows:
14
Number-Base Conversions
• Representations of a number in a different radix are said to be equivalent if
they have the same decimal representation.
• For example, (0011)8 and (1001)2 are equivalent—both have decimal value 9.
• The conversion of a number in base r to decimal is done by expanding the
number in a power series and adding all the terms as shown previously.
• We now present a general procedure for the reverse operation of converting a
decimal number to a number in base r.
• If the number includes a radix point, it is necessary to separate the number into
an integer part and a fraction part, since each part must be converted
differently.
• The conversion of a decimal integer to a number in base r is done by dividing
the number and all successive quotients by r and accumulating the remainders.
15
Number-Base Conversions
• Example: To convert decimal 41 to binary. First, 41 is divided by 2 to give an
integer quotient of 20 and a remainder of 1. Then the quotient is again divided
by 2 to give a new quotient and remainder. The process is continued until the
integer quotient becomes 0. The coefficients of the desired binary number are
obtained from the remainders as follows:
Number-Base Conversions
• Example: Convert decimal 153 to octal.
• The required base r is 8.
• First, 153 is divided by 8 to give an integer quotient of 19 and a remainder of 1.
• Then 19 is divided by 8 to give an integer quotient of 2 and a remainder of 3.
• Finally, 2 is divided by 8 to give a quotient of 0 and a remainder of 2.
• This process can be conveniently tabulated as follows:
Integer Remainder
17
Number-Base Conversions
• The conversion of a decimal fraction to binary is accomplished by a method
similar to that used for integers. However, multiplication is used instead of
division, and integers instead of remainders are accumulated.
• Example: Convert (0.6875)10 to binary. First, 0.6875 is multiplied by 2 to give an
integer and a fraction. Then the new fraction is multiplied by 2 to give a new
integer and a new fraction. The process is continued until the fraction becomes
0 or until the number of digits has sufficient accuracy. The coefficients of the
binary number are obtained from the integers as follows:
Number-Base Conversions
• To convert a decimal fraction to a number expressed in base r, a similar
procedure is used. However, multiplication is by r instead of 2, and the
coefficients found from the integers may range in value from 0 to r - 1 instead
of 0 and 1.
• Example: Convert (0.513)10 to octal.
...
• (0.513)10 = (0.406517…)8
19
Number-Base Conversions
• The conversion of decimal numbers with both integer and fraction parts is done
by converting the integer and the fraction separately and then combining the
two answers:
(41.6875)10 = (101001.1011)2
(153.513)10 = (231.406517)8
20
Signed Magnitude
• The set of positive and negative integers is referred to as the set of signed
integers.
• The problem with representing signed integers as binary values is the sign.
• Signed-magnitude representation is one method of solving this problem.
• As its name implies, a signed-magnitude number has a sign as its leftmost bit
(also referred to as the high-order bit or the most significant bit) whereas the
remaining bits represent the magnitude (or absolute value) of the numeric
value.
• For example, in an 8-bit word, –1 would be represented as 10000001, and +1 as
00000001.
• In a computer system that uses signed-magnitude representation and 8 bits to
store integers, 7 bits can be used for the actual representation of the
magnitude of the number.
• This means that the largest integer an 8-bit word can represent is 27 – 1, or 127
(a zero in the high-order bit, followed by 7 ones).
• The smallest integer is 8 ones, or –127.
• Therefore, N bits can represent –(2(N–1) – 1) to 2(N–1) – 1.
26
Signed Magnitude
• Computers must be able to perform arithmetic calculations on integers that are
represented using this notation.
• Signed-magnitude arithmetic is carried out using essentially the same methods
that humans use with pencil and paper, but it can get confusing very quickly.
• As an example, consider the rules for addition: (1) If the signs are the same,
add the magnitudes and use that same sign for the result; (2) If the signs differ,
you must determine which operand has the larger magnitude. The sign of the
result is the same as the sign of the operand with the larger magnitude, and
the magnitude must be obtained by subtracting (not adding) the smaller one
from the larger one.
• If you consider these rules carefully, this is the method you use for signed
arithmetic by hand.
• We arrange the operands in a certain way based on their signs, perform the
calculation without regard to the signs, and then supply the sign as appropriate
when the calculation is complete.
• When modeling this idea in an 8-bit word, we must be careful to include only 7
bits in the magnitude of the answer, discarding any carries that take place over
the high-order bit.
27
Signed Magnitude
• Example: Add (01001111)2 to (00100011)2 using signed-magnitude arithmetic.
• The arithmetic proceeds just as in decimal addition, including the carries, until
we get to the seventh bit from the right.
• If there is a carry here, we say that we have an overflow condition and the
carry is discarded, resulting in an incorrect sum.
• There is no overflow in this example.
• Sign bits are segregated because they are relevant only after the addition is
complete.
• In this case, we have the sum of two positive numbers, which is positive.
28
Signed Magnitude
• Example: Add (01001111)2 to (01100011)2 using signed-magnitude arithmetic.
Signed Magnitude
• Signed magnitude representation has two main disadvantages:
1. Two Representations for Zero: In signed magnitude, both +0 and -0 have
distinct binary representations, which can complicate calculations and
comparisons involving zero.
2. Complex Arithmetic Operations: Arithmetic operations like addition and
subtraction can become more complex due to the need to handle the sign
bit and separate magnitude calculations. This complexity can slow down
computations.
• For these reasons, while signed magnitude representation is conceptually
straightforward and intuitive, it is often not the preferred choice in modern
computing systems, where alternatives like two's complement representation
offer more efficient and versatile ways of handling signed numbers.
30
Complement Systems
• Number theorists have known for hundreds of years that one decimal number
can be subtracted from another by adding the difference of the subtrahend
from all nines and adding back a carry.
• This is called taking the nine’s complement of the subtrahend or, more
formally, finding the diminished radix complement of the subtrahend.
• Let’s say we wanted to find 167 – 52.
• Taking the difference of 52 from 999, we have 947.
• Thus, in nine’s complement arithmetic, we have 167 – 52 = 167 + 947 = 1114.
• The “carry” from the hundreds column is added back to the units place, giving
us a correct 167 – 52 = 115.
• This method was commonly called “casting out 9s” and has been extended to
binary operations to simplify computer arithmetic.
• The advantage that complement systems give us over signed magnitude is that
there is no need to process sign bits separately, but we can still easily check the
sign of a number by looking at its high-order bit.
31
One’s Complement
• As illustrated, given a number N in base r having d digits, the diminished radix
complement of N is defined to be: (rd – 1) – N.
• For binary numbers, r = 2. For example, the one’s complement of (0101)2 is
(1111)2 – (0101) 2 = (1010) 2.
• Although we could tediously borrow and subtract, a few experiments will
convince you that forming the one’s complement of a binary number amounts
to nothing more than switching all of the 1s with 0s and vice versa.
• This sort of bit-flipping is very simple to implement in computer hardware.
32
One’s Complement
• It is important to note at this point that although we can find the nine’s
complement of any decimal number or the one’s complement of any binary
number, we are most interested in using complement notation to represent
negative numbers.
• We know that performing a subtraction, such as 10 – 7, can also be thought of
as “adding the opposite,” as in 10 + (–7).
• Complement notation allows us to simplify subtraction by turning it into
addition, but it also gives us a method to represent negative numbers.
• Because we do not wish to use a special bit to represent the sign (as we did in
signed-magnitude representation), we need to remember that if a number is
negative, we should convert it to its complement.
• The result should have a 1 in the leftmost bit position to indicate that the
number is negative.
33
One’s Complement
• Example: Express 23 and –9 in 8-bit binary, assuming a computer is using one's
complement representation.
34
One’s Complement
• Unlike signed magnitude, in one's complement addition there is no need to
maintain the sign bit separate from the other bits. The sign takes care of itself.
• Example: Add (01001111)2 to (00100011)2 using one’s complement addition.
35
One’s Complement
• Example: Suppose we wish to subtract 9 from 23.
• To carry out a one's complement subtraction, we first express the subtrahend
(9) in one's complement, then add it to the minuend (23).
• The high-order bit will have a 1 or a 0 carry, which is added to the low-order bit
of the sum (this is called end carry-around and results from using the
diminished radix complement).
36
One’s Complement
• Example: Add 9 to –23 using one’s complement arithmetic.
Two’s Complement
• Two's complement is an example of a radix complement.
• Given a number N in base r having d digits, the radix complement of N is
defined as rd – N for N ≠ 0 and 0 for N = 0.
• For example, in binary, the two's complement of the 4-bit number (0011)2 is:
24 – (0011)2 = (10000)2 – (0011)2 = (1101)2
• Upon closer examination, you will discover that two's complement is nothing
more than one's complement incremented by 1.
• To find the two's complement of a binary number, simply flip bits and add 1.
This simplifies addition and subtraction as well.
• Because the subtrahend (the number we complement and add) is incremented
at the outset, there is no end carry-around to worry about.
• We simply discard any carries involving the high-order bits.
• As before, positive numbers can be left alone; we only need to complement
negative numbers to get them into their two's complement form.
38
Two’s Complement
• Example: Express 23, –23, and –9 in 8-bit binary, assuming a computer is using
two's complement representation.
39
Two’s Complement
• Because the representation of positive numbers is the same in one's
complement and two's complement (as well as signed-magnitude), the process
of adding two positive binary numbers is the same.
• Example: Add (01001111)2 to (00100011)2 using two's complement addition.
40
Two’s Complement
• Suppose we are given the binary representation for a number and want to
know its decimal equivalent.
• Positive numbers are easy. For example, to convert the two's complement
value of (00010111)2 to decimal, we simply convert this binary number to a
decimal number to get 23.
• However, converting two's complement negative numbers requires a reverse
procedure similar to the conversion from decimal to binary.
• Suppose we are given the two's complement binary value of (11110111)2, and
we want to know the decimal equivalent.
• We know this is a negative number but must remember it is represented using
two's complement.
• We first flip the bits and then add 1 (find the one's complement and add 1).
• This results in the following: 00001000 + 1 = 00001001.
• This is equivalent to the decimal value 9.
• However, the original number we started with was negative, so we end up with
–9 as the decimal equivalent to (11110111)2.
41
Two’s Complement
• Example: Add 9 to –23 using two’s complement arithmetic.
Two’s Complement
• Example: Find the sum of -23 (11101001)2 and -9 (11110111)2 using two's
complement addition.
• Notice that the discarded carries did not cause an erroneous result.
• An overflow occurs if two positive numbers are added and the result is
negative, or if two negative numbers are added and the result is positive.
• It is not possible to have overflow when using two's complement notation if a
positive and a negative number are being added together.
43
Two’s Complement
• A Simple Rule for Detecting an Overflow Condition in Signed Numbers: If the
carry into the sign bit equals the carry out of the bit, no overflow has occurred.
If the carry into the sign bit is different from the carry out of the sign bit,
overflow (and thus an error) has occurred.
• The following example indicates overflow because the carry into the sign bit (a
1 is carried in) is not equal to the carry out of the sign bit (a 0 is carried out).
• Example: Find the sum of 126 and 8 in binary using two's complement
arithmetic.
44
Two’s Complement
• Two's complement is the most popular choice for representing signed numbers. The
algorithm for adding and subtracting is quite easy, has the best representation for 0
(all 0 bits), and is easily extended to larger numbers of bits.
• The biggest drawback is in the asymmetry seen in the range of values that can be
represented by N bits.
• With signed-magnitude numbers, for example, 4 bits allow us to represent the
values –7 through +7.
• However, using two's complement, we can represent the values –8 through +7,
which is often confusing to anyone learning about complement representations.
• To see why +7 is the largest number we can represent using 4-bit two's complement
representation, we need only remember that the first bit must be 0. If the
remaining bits are all 1s (giving us the largest magnitude possible), we have (0111)2,
which is 7.
• An immediate reaction to this is that the smallest negative number should then be
(1111)2, but we can see that (1111)2 is actually –1 (flip the bits, add one, and make
the number negative).
• So how do we represent –8 in two's complement notation using 4 bits? It is
represented as (1000)2. We know this is a negative number. If we flip the bits
(0111), add 1 (to get 1000, which is 8), and make it negative, we get –8.
45
• Using excess-7 notation and given the binary number (1111)2, to find the
decimal value it represents, we simply subtract 7: (1111)2 = 15, and 15 – 7 = 8;
therefore, the value (1111)2, using excess-7 representation, is +8.
47
Fixed-point representation of −2.375: (a) absolute value, (b) sign and magnitude,
(c) two's complement
50
• Note that the leading 1 in the binary fixed-point addition is discarded from the
8-bit result.
51
Floating-Point Representation
• Floating-point numbers are analogous to scientific notation.
• They circumvent the limitation of having a constant number of integer and
fraction bits, allowing the representation of very large and very small numbers.
• Floating-point numbers consist of three parts: ± M × BE
a sign bit,
an exponent (E) part (representing the exponent on a power of 2),
and a fractional part (mantissa (M) or significand)
• For example, 32 bits can be used to represent 1 sign bit, 8 exponent bits, and
23 mantissa bits.
52
Floating-Point Representation
• Example: Show the floating-point representation of the decimal number 228.
• Solution: First convert the decimal number into binary:
(228)10 = (11100100)2 = (1.11001)2 × 27
• The following Figure shows the 32-bit encoding, which will be modified later for
efficiency. The sign bit is positive (0), the 8 exponent bits give the value 7, and
the remaining 23 bits are the mantissa:
• In binary floating-point, the first bit of the mantissa (to the left of the binary
point) is always 1 and therefore need not be stored. It is called the implicit
leading one. The following Figure shows the modified floating-point
representation of 228. The implicit leading one is not included in the 23-bit
mantissa for efficiency. Only the fraction bits are stored. This frees up an extra
bit for useful data:
53
Floating-Point Representation
• We make one final modification to the exponent field.
• The exponent needs to represent both positive and negative exponents.
• To do so, floating-point uses a biased exponent, which is the original exponent
plus a constant bias.
• 32-bit floating-point uses a bias of 127 = 28-1 - 1.
• For example, for the exponent 7, the biased exponent is 7 + 127 = 134 =
(10000110)2. For the exponent −4, the biased exponent is: −4 + 127 = 123 =
(01111011)2.
• The following Figure shows (1.11001)2 × 27 represented in floating-point
notation with an implicit leading one and a biased exponent of 134 (7 + 127).
This notation conforms to the IEEE 754 floating-point standard.
54
Floating-Point Representation
Special Cases: 0, ±∞, and NaN: The IEEE floating-point standard has special cases to
represent numbers such as zero, infinity, and illegal results.
• For example, representing the number zero is problematic in floating-point notation
because of the implicit leading one.
• Special codes with exponents of all 0's or all l's are reserved for these special cases.
NaN is used for numbers that don't exist, such as √-1:
Floating-Point Representation
Single- and Double-Precision Formats: So far, we have examined 32-bit
floating-point numbers. This format is also called single-precision, single, or
float.
The IEEE 754 standard also defines 64-bit double-precision numbers (also
called double) that provide greater precision and greater range:
56
Character Codes
• We have seen how digital computers use the binary system to represent and
manipulate numeric values.
• We have yet to consider how these internal values can be converted to a form
that is meaningful to humans.
• The manner in which this is done depends on both the coding system used by
the computer and how the values are stored and retrieved.
57
ASCII
• Many applications of digital computers require the handling not only of
numbers but also of other characters or symbols, such as the letters of the
alphabet.
• For instance, consider a company with thousands of employees. To represent
the names and other pertinent information, it is necessary to formulate a
binary code for the letters of the alphabet.
• In addition, the same binary code must represent numerals and special
characters (such as $).
• The standard binary code for the alphanumeric characters is the American
Standard Code for Information Interchange (ASCII), which uses seven bits to
code 128 characters.
• The letter A, for example, is represented in ASCII as 1000001 = 65.
62
Unicode
• Both EBCDIC and ASCII were built around the Latin alphabet.
• As such, they are restricted in their abilities to provide data representation for
the non-Latin alphabets used by the majority of the world’s population.
• As all countries began using computers, each was devising codes that would
most effectively represent their native languages.
• None of these was necessarily compatible with any others, placing yet another
barrier in the way of the emerging global economy.
• In 1991, before things got too far out of hand, a consortium of industry and
public leaders was formed to establish a new international information
exchange code called Unicode.
• This group is appropriately called the Unicode Consortium.
• Unicode is a 16-bit alphabet.
• Because the base coding of Unicode is 16 bits, it has the capacity to encode the
majority of characters used in every language of the world.
63
Bibliography