0% found this document useful (0 votes)
14 views54 pages

Chapter2 5

The document discusses number systems, focusing on binary representation and its significance in computer science. It explains binary arithmetic, two's complement for signed integers, and the conversion between binary and hexadecimal notations. Examples illustrate the conversion processes and arithmetic operations in these number systems.

Uploaded by

Sime Purewal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views54 pages

Chapter2 5

The document discusses number systems, focusing on binary representation and its significance in computer science. It explains binary arithmetic, two's complement for signed integers, and the conversion between binary and hexadecimal notations. Examples illustrate the conversion processes and arithmetic operations in these number systems.

Uploaded by

Sime Purewal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Number Systems

Binary Representation of Numbers

2
Binary Representation of Numbers
Any integer greater than 1 can serve as a base for a
number system. In computer science, base 2 notation, or
binary notation, is of special importance because the
signals used in modern electronics are always in one of
only two states. (The Latin root bi means “two.”)

3
Example 2.5.1 – Binary Notation for Integers from 1 to 9

Derive the binary notation for the integers from 1 to 9.

4
Example 2.5.1 – Solution

5
Binary Representation of Numbers
A list of powers of 2 is useful for doing binary-to-decimal
and decimal-to-binary conversions.

Powers of 2
Table 2.5.1

6
Example 2.5.2 – Converting a Binary to a Decimal Number

Represent in decimal notation.

7
Example 2.5.2 – Solution

Alternatively, the schema below may be used.

8
Example 2.5.3 – Converting a Decimal to a Binary Number

Represent 209 in binary notation.

9
Example 2.5.3 – Solution
Use Table 2.5.1 to write 209 as a sum of powers of 2,
starting with the highest power of 2 that is less than 209
and continuing to lower powers.

Powers of 2
Table 2.5.1

Since 209 is between 128 and 256, the highest power of 2


that is less than 209 is 128. Hence

10
Example 2.5.3 – Solution continued

Now 209 − 128 = 81, and 81 is between 64 and 128, so the


highest power of 2 that is less than 81 is 64. Hence

Continuing in this way, you obtain

11
Example 2.5.3 – Solution continued

For each power of 2 that occurs in the sum, there is a 1 in


the corresponding position of the binary number. For each
power of 2 that is missing from the sum, there is a 0 in the
corresponding position of the binary number. Thus

12
Binary Addition and Subtraction

13
Binary Addition and Subtraction
The computational methods of binary arithmetic are
analogous to those of decimal arithmetic. In binary
arithmetic the number 2 (which equals in binary
notation) plays a role similar to that of the number 10 in
decimal arithmetic.

14
Example 2.5.4 – Addition in Binary Notation

Add using binary notation.

15
Example 2.5.4 – Solution
Because the translation of
to binary notation is

It follows that adding two 1’s together


results in a carry of 1 when binary
notation is used. Adding three 1’s
together also results in a carry of 1
since (“one one base two”).

16
Example 2.5.4 – Solution continued

Thus the addition can be performed as follows:

17
Example 2.5.5 – Subtraction in Binary Notation

Subtract using binary notation.

18
Example 2.5.5 – Solution
In decimal subtraction the fact that is used
to borrow across several columns.

For example, consider the following:

19
Example 2.5.5 – Solution continued

In binary subtraction it may also be necessary to borrow


across more than one column. But when you borrow a
from what remains is

Thus the subtraction can


be performed as follows:

20
Two’s Complements and the
Computer Representation
of Signed Integers

21
Two’s Complements and the Computer Representation of Signed Integers

Typically a fixed number of bits is used to represent


integers on a computer. One way to do this is to select a
particular bit, normally the left-most, to indicate the sign of
the integer, and to use the remaining bits for its absolute
value in binary notation.
The problem with this approach is that the procedures for
adding the resulting numbers are somewhat complicated
and the representation of 0 is not unique. A more common
approach is to use “two’s complements,” which makes it
possible to add integers quite easily and results in a unique
representation for 0.

22
Two’s Complements and the Computer Representation of Signed Integers

Bit lengths of 64 and (sometimes) 32 are most often used


in practice, but, for simplicity and because the principles
are the same for all bit lengths, this discussion will focus on
a bit length of 8.

23
Two’s Complements and the Computer Representation of Signed Integers

Thus the 8-bit representation for a nonnegative integer is


the same as its 8-bit binary representation. As a concrete
example for the negative integer −46, observe that

and so the 8-bit two’s complement for −46 is 11010010.

24
Two’s Complements and the Computer Representation of Signed Integers

For negative integers, however, there is a more convenient


way to compute two’s complements, which involves less
arithmetic than applying the definition directly.

25
Example 2.5.6 – Finding a Two’s Complement

Use the method described above to find the 8-bit two’s


complement for −46.

26
Example 2.5.6 – Solution
Write the 8-bit binary representation for switch
all the 1’s to 0’s and all the 0’s to 1’s, and then add 1.

Note that this is the same result as was obtained directly


from the definition.

27
Two’s Complements and the Computer Representation of Signed Integers

The fact that the method for finding 8-bit two’s


complements works in general depends on the following
facts:

28
Two’s Complements and the Computer Representation of Signed Integers

Here is how the facts are used when a = −46:

29
Two’s Complements and the Computer Representation of Signed Integers

Because 127 is the largest integer represented in the 8-bit


two’s complement system and because
all the 8-bit two’s complements for nonnegative integers
have a leading bit of 0.

Moreover, because the bits are switched, the leading bit for
all the negative integers is 1.

30
Two’s Complements and the Computer Representation of Signed Integers

Table 2.5.2 illustrates the 8-bit two’s complement


representations for the integers from −128 through 127.

Table 2.5.2

31
Two’s Complements and the Computer Representation of Signed Integers

32
Example 2.5.7 – Finding a Number with a Given Two’s Complement

What is the decimal representation for the integer with


two’s complement 10101001?

33
Example 2.5.7 – Solution
Since the left-most digit is 1, the integer is negative.
Applying the two’s complement procedure gives the
following result:

So the answer is −87. You can check its correctness by


deriving the two’s complement of −87 directly from the
definition:

34
Addition and Subtraction with
Integers in Two’s Complement
Form

35
Addition and Subtraction with Integers in Two’s Complement Form

The main advantage of a two’s complement representation


for integers is that the same computer circuits used to add
nonnegative integers in binary notation can be used for
both additions and subtractions of integers in a two’s
complement system of numeration.
First note that because of the algebraic identity
a − b = a + (−b) for all real numbers,
any subtraction problem can be changed into an addition
one.

36
Addition and Subtraction with Integers in Two’s Complement Form

For example, suppose you want to compute 78 − 46. This


equals 78 + (−46), which should give an answer of 32.

To see what happens when you add the numbers in their


two’s complement forms, observe that the 8-bit two’s
complement for 78 is the same as the ordinary binary
representation for 78, which is 01001110 because 78 = 64
+ 8 + 4 + 2, and, as previously shown, the 8-bit two’s
complement for −46 is 11010010.

37
Addition and Subtraction with Integers in Two’s Complement Form

Adding the numbers using binary addition gives the


following:

The result has a carry bit of 1 in the ninth, or position,


but if you discard it, you obtain 00100000, which is the
correct answer in 8-bit two’s complement form because,
since

38
Addition and Subtraction with Integers in Two’s Complement Form

39
Hexadecimal Notation

40
Hexadecimal Notation
Hexadecimal notation is even more compact than decimal
notation, and it is much easier to convert back and forth
between hexadecimal and binary notation than it is
between binary and decimal notation.
The word hexadecimal comes from the Greek root hex-,
meaning “six,” and the Latin root deci-, meaning “ten.”
Hence hexadecimal refers to “sixteen,” and hexadecimal
notation is also called base 16 notation.

41
Hexadecimal Notation
Hexadecimal notation is based on the fact that any integer
can be uniquely expressed as a sum of numbers of the
form

where each n is a nonnegative integer and each d is one of


the integers from 0 to 15.

42
Hexadecimal Notation
The 16 hexadecimal digits are shown in Table 2.5.3,
together with their decimal equivalents and, for future
reference, their 4-bit binary equivalents.

Table 2.5.3
43
Example 2.5.8 – Converting from Hexadecimal to Decimal Notation

Convert to decimal notation.

44
Example 2.5.8 – Solution
A schema similar to the one introduced in Example 2.5.2
can be used here.

45
Hexadecimal Notation

46
Example 2.5.9 – Converting from Hexadecimal to Binary Notation

Convert to binary notation.

47
Example 2.5.9 – Solution

Consequently,

and the answer is

48
Hexadecimal Notation
To convert integers written in binary notation into
hexadecimal notation, reverse the steps of the previous
procedure. Note that the commonly used computer
representation for integers uses 32 bits. When these
numbers are written in hexadecimal notation only eight
characters are needed.

49
Example 2.5.10 – Converting from Binary to Hexadecimal Notation

Convert to hexadecimal notation.

50
Example 2.5.10 – Solution
First group the binary digits in sets of four, working from
right to left and adding leading 0’s if necessary.

0100 1101 1010 1001.

Convert each group of four binary digits into a hexadecimal


digit.

51
Example 2.5.10 – Solution continued

Then juxtapose the hexadecimal digits.

52
Example 2.5.11 – Reading a Memory Dump

The smallest addressable memory unit on most computers


is one byte, or eight bits. In some debugging operations a
dump is made of memory contents; that is, the contents of
each memory location are displayed or printed out in order.
To save space and make the output easier on the eye, the
hexadecimal versions of the memory contents are given,
rather than the binary versions. Suppose, for example, that
a segment of the memory dump looks like
A3 BB 59 2E.
What is the actual content of the four memory locations?

53
Example 2.5.11 – Solution

54

You might also like