Professional Documents
Culture Documents
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.
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
• 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
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.
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
12
Converting from any Base to Decimal
14
Outline
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.
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
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).
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:
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)
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
22
Converting Between Power-of-Two Bases
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)
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
0010 1100 1001 1101 (Reminder : 1101 is 1* 23 + 1*22 + 0*21 + 1*20= 13=D)
2 C 9 D
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.
Binary values are derived from Hex by representing each Hex digit by a group of four binary bits.
Octal values are derived from binary by using groups of three bits (8 = 23) 25
Outline
26
Signed integer representation
The conversions we have so far presented have involved only unsigned numbers.
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).
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.
Example: Express 2310, -2310, and -910 in 8-bit binary two’s complement form.
32