You are on page 1of 32

Introduction to Computer Science (I1100)

Lecture 2: Computer Structure (Part 2)


Lebanese University
Faculty of Sciences 1 - Department of Computer Science
Dr. Abed EL Safadi
Numeral system and convert methods

Positional Numbering System


Conversions
Signed integer representation

2
Positional Numbering System
The general idea behind positional numbering system is that a numeric value
is represented through increasing powers of a base. This is often referred to
as a weighted numbering system because each position is weighted by a
power of the base.

Example : 24310 = 2x102 + 4x101 + 3x100

• Numbering systems and their properties:


 Any numeric value is represented through increasing powers of a base.
 The set of valid numerals (digits) is equal in size to the base of that system.
 The least numeral is 0 and the highest one is 1 smaller than the base.

3
Positional Numbering System
• The most important bases in computer science are:
– Decimal
• The base is 10
• The number of valid numerals is 10 (equal to the base)
• The set of valid numerals is: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
– Binary
• The base is 2
• The number of valid numerals is 2 (equal to the base)
• The set of valid numerals is: {0 , 1}

4
Positional Numbering System
• The most important bases in computer science are:
– Octal
• The Base is 8
• The number of valid numerals is 8 (equal to the base)
• The set of valid numerals is: {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7}
– Hexadecimal
• The base is 16
• The number of valid numerals is 16 (equal to the base)
• The set of valid numerals is: {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , A , B , C , D , E , F}

5
Positional Numbering System
• To distinguish among numbers in different bases, we use the base
as a subscript, such as 2310.
• Numbers written without a subscript should be assumed to be
decimal.
• Any decimal integer can be expressed exactly in any other integral
base system, and vice versa.
Integer part Fractional part
Example:
– 243.5110 = 2x102 + 4x101 + 3x100 + 5x10-1 + 1x10-2

– 10110.012 = 1x24 + 0x23 + 1x22 + 1x21 + 0x20 + 0x2-1 + 1x2-2= 22.2510

• Pay attention to the fact that the first position (right-hand side) of
the integer part, will be multiplied by the base raised to power
(0), and the first position (left-hand side) of the fractional part will
be multiplied by the base raised to power (-1).
– You can notice that in the above example, where 243.5110, 3 was
multiplied by 100, and 5 was multiplied by 10-1. 6
Outline

• Positional Numbering System


• Converting from Decimal to any Base
• Converting from any Base to Decimal
• Converting between Bases
• Converting Between Power-of-Two Bases
• Signed integer representation

7
Converting from Decimal to any Base
• When dealing with numbers, you might face ones that
contain fractions. A number, hence may include an
integer part and a fraction part separated by a base
point.

• Base points separate the integer part of a number from


its fractional part. The “Base point” is called a “decimal
point” in a decimal system, a “binary point” in a binary
system, and so on.

We will focus our discussion on converting the integer


part of a number.

8
Converting from Decimal to any Base
Converting the integer part
Given the integer part of a decimal number, you can find the
equivalent value in any base, n, as follows:
1. Divide the number by n.
2. You will get Quotient and Remainder. The remainder will
always be less than the base n. Keep this remainder aside.
3. Use the quotient to repeat the above steps, until you get a
quotient equal to 0.
4. Now, group the remainders in order, such that the first
remainder will be the least significant bit, and the last
one will be the most significant bit.

9
Converting from Decimal to any Base
Example: Convert 11310 to binary
Q R
2 |113 56 1 LSB
2 |56 28 0
2 |28 14 0
2 |14 7 0 11310 = 11100012
2 |7 3 1
2 |3 1 1
2 |1 0 1 MSB
0
Important note: A binary number with N bits can represent 2N unsigned integers from 0 to 2N-1. 10
Converting from Decimal to any Base
• Keep in mind the following tables or how to obtain them!

11
Outline

• Positional Numbering System


• Converting from Decimal to any Base
– Converting the integer part
– Converting the fraction part
• Converting from any Base to Decimal
• Converting between Bases
• Converting Between Power-of-Two Bases
• Signed integer representation

12
Converting from any Base to Decimal

Converting from any Base to Decimal


To convert from any base, say n, to decimal, all you need to do is
to multiply the digits, whether they were integers or fractions,
by the base n raised to the position of that digit.
Thus, given a number in base n, you can find the equivalent
value in decimal as follows:
1. Multiply each digit with the base n raised to the power of its
location.(Note that the LSB digit has a position of 0)
2. Add all the intermediate results to get the final equivalent
decimal value.
13
Converting from any Base to Decimal

Example 1: Suppose that we want to convert the binary number 1011


to its decimal equivalent:
(1 * 23) + (0*22) + (1*21) + (1*20) = 8+0+2+1 = 1110

Note: In the hexadecimal system, you might encounter a letter. In this


case, you should multiply the respective equivalent value in decimal
with n raised to the position.

Example 2: Suppose that we want to convert the Hexadecimal number 1A


to its decimal equivalent:

(1 * 161) + (A * 160) + = (1 * 161) + (10 * 160) = 16 +10=2610

14
Outline

• Positional Numbering System


• Converting from Decimal to any Base
– Converting the integer part
– Converting the fraction part
• Converting from any Base to Decimal
• Converting between Bases
• Converting Between Power-of-Two Bases
• Signed integer representation

15
Converting Between Bases
Converting Between Bases
Although there are direct ways to convert any number in any
base to any other base, it is faster and more accurate to convert
to base 10 and then to the desired base. One exception to this
rule exists when you are working between bases that are
powers of two, as you will see later in this section.

In general, when you want to convert from Base x To Base y,


you can use Base 10 as an intermediate step of conversion.

Base x Decimal Base y


16
Converting Between Bases
Example: Convert 31214 to base 3.

First, convert to decimal: Second, convert to base 3:


Q R
31214= 3* 43 + 1*42 + 2*41 + 1*40
217 / 3 = 72 1
= 3*64 + 1*16 + 2*4 + 1 72 / 3 = 24 0
24 / 3 = 8 0
= 21710 8/3 = 2 2
2/3 = 0 2

Alors, 31214= 220013


17
Converting Between Bases
The binary numbering system is the most important base system for digital
computers.

However, it is difficult to read long strings of binary numbers—and even a


modestly-sized decimal number becomes a very long binary number.

For example: 110101000110112 = 1359510

For ease of reading, binary values are usually expressed using the hexadecimal or
octal numbering system.

Working between bases that are powers of two, is much easier than other bases.

In the next section, you will learn in details how to convert among these bases.

18
Outline

• Positional Numbering System


• Converting from Decimal to any Base
– Converting the integer part
– Converting the fraction part
• Converting from any Base to Decimal
• Converting Between Power-of-Two Bases
• Signed integer representation

19
Converting Between Power-of-Two Bases

The most famous power-of-two bases are: binary (base 2), octal (base 23 - base 8)
and hexadecimal (base 24 - base 16).

Each octal digit is equivalent to a group of 3 binary digits.

Each hexadecimal digit is equivalent to a group of 4 binary digits.

We convert from binary to octal and from binary to hexadecimal by simply grouping
bits

20
Converting Between Power-of-Two Bases
Binary to Octal Conversion:

To convert from binary to octal, we do the following:

1. Make Groups of 3 bits (from right to left for the integer part, from left to right for
the fraction part)

2. Add zero(s) (on the left for the integer part, on the right for the fraction part) to
complete the last octet (if not complete)

3. Convert each octet to its corresponding octal digit

4. Concatenate ( join together) the digits. (Do not add them!)

21
Converting Between Power-of-Two Bases
Example: Suppose that we want to convert the Binary
number 101100100111012 to octal:
1. Make Groups of 3 bits (from right to left): 10 110 010 011 101

2. Add zero(s) on the left to complete the last octet: 010 110 010 011 101

3. Convert each octet to its corresponding octal digit

010 110 010 011 101


(1*22) + (0*21) + (1*20)=5
2 6 2 3 5

4. Concatenate: 101100100111012 = 262358

22
Converting Between Power-of-Two Bases

Binary to Hexadecimal Conversion:


To convert from binary to hexadecimal, we do the following:

1. Make Groups of 4 bits (from right to left for the integer part, from left to
right for the fraction part)

2. Add zero(s) (on the left for the integer part, on the right for the fraction
part) to complete the last hextet (if not complete)

3. Convert each hextet to its corresponding Hexadecimal digit.

4. Concatenate (Do not add!) the digits.

23
Converting Between Power-of-Two Bases
Example: Convert 101100100111012 to hexadecimal.
1. Make Groups of 4 bits (from right to left): 10 1100 1001 1101

2. Add zero(s) on the left to complete the last hextet: 0010 1100 1001 1101

3. Convert each hextet to its corresponding hexadecimal digit

0010 1100 1001 1101 (Reminder : 1101 is 1* 23 + 1*22 + 0*21 + 1*20= 13=D)

2 C 9 D

4. Concatenate: 101100100111012 = 2C9D16

24
Converting Between Power-of-Two Bases
Note: To convert from Hex to Octal, or from Octal to Hex, you can simply use
the Binary as an intermediate step of conversion.

Octal Binary Hex

Hex Binary Octal

Example: Convert the Hex number 351B to Octal.

First, you need to convert from Hex to Binary.

Binary values are derived from Hex by representing each Hex digit by a group of four binary bits.

Next, you have to convert from binary to Octal:

Octal values are derived from binary by using groups of three bits (8 = 23) 25
Outline

• Positional Numbering System


• Converting from Decimal to any Base
– Converting the integer part
– Converting the fraction part
• Converting from any Base to Decimal
• Converting between Bases
• Converting Between Power-of-Two Bases
• Signed integer representation

26
Signed integer representation
The conversions we have so far presented have involved only unsigned numbers.

To represent signed integers, computer systems allocate the high-order bit to


indicate the sign of a number.

The high-order bit is the leftmost bit. It is also called the most significant bit.
0 is used to indicate a positive number; 1 indicates a negative number.
The remaining bits contain the value of the number (but this can be interpreted
different ways).

Signed binary integers may be expressed in two ways:


1. One’s complement
2. Two’s complement

Reminder: Binary addition is as easy as it gets. You need to know only four rules:
0+0= 0 0+1= 1
1 + 0 = 1 1 + 1 = 10 27
Signed integer representation
One’s complement (C1)
A positive number in one’s complement (C1), is directly converted to its binary
representation.

To convert a negative number to its one’s complement (C1), this number is subtracted
from all ones, or you can simply complement each digit, such that the 1s become
0s and vice versa.

Example 1:
The one’s complement of 0101 is 1111 - 0101 = 1010C1
Or, you can simply complement it:
01012 positive number
1010 negative number, in C1
Example 2: Express 2310 and -910 in 8-bit binary one’s complement form.
Answer:
2310 = + (000101112) = 00010111C1
-910 = - (000010012) = 11110110C1 28
Signed integer representation
Reading the value of a C1 number:
In order to find an equivalent decimal value of a C1 number, you have to:
Check the MSB:
1. If it is 0, then you can directly convert the binary to decimal.
2. If it is 1, then :
a. Complement the digits. MSB
b. Convert the binary number to decimal.
c. Include the negative sign.
Example: What is the decimal equivalent of the C1 number 11110110 ?
Answer: Here, you can notice that the MSB is 1. Then, you have to complement all
the bits of the C1 number first, and then find the equivalent value in decimal.
Finally, remember to add the negative sign.
11110110C1
Complement: 000010012
Convert :9
Include (-) : -9 29
Signed integer representation
Two’s Complement Representation
In One’s complement, we still have two representations for zero: 00000000
and 11111111. Computer engineers long ago stopped using one’s
complement.

A more efficient representation for binary numbers is: two’s complement.

C2 is nothing more than C1 incremented by 1!

Example: Express 2310, -2310, and -910 in 8-bit binary two’s complement form.

2310 = + (000101112) = 00010111c2 (no need to complement, as it is a positive number)


-2310 = -(000101112) = 11101000c1 + 1 = 11101001c2
-910 = -(000010012) = 11110110c1 + 1 = 11110111c2 30
Signed integer representation
Reading the value of a C2 number: In order to find the equivalent decimal of a C2, you have to:
Check the MSB:

a. If it is 0, then you can directly convert the binary to decimal.


b. If it is 1, then :
1. Complement the digits.
2. Add 1 .
3. Convert the binary number to decimal.
4. Include the negative sign.
Example: What is the decimal equivalent of the C2 number 11110110?
Here, you can notice that the MSB is 1. Then, you have to complement all the bits of the C2 number first,
add one, and then find the equivalent value in decimal. Finally, remember to add the negative sign.
11110110C2
Complement: 00001001
Add 1 : 00001010
Convert : 10
Include (-) : -10 31
End of Chapter 2

Try to solve all exercises related to chapter 2

32

You might also like