Lecture 2
Lecture 2
(WBCS010-05)
Lecture 2: Number Systems
Reza Hassanpour
[email protected]
Introduction
› Computers work with digital signals, which
are discrete and have only two possible states:
0 or 1.
› Binary numbers are the most natural way to
represent these two states.
› The binary system simplifies the
representation of digital data by aligning with
the on/off states of electronic switches.
› Question:
• How can we represent decimal numbers in
binary?
Positional and Non-Positional Number
Systems
› Positional number system is the type of number
system in which the value of a digit depends upon its
position in the number.
• Ex. Decimal Numbers
10
Sign-Magnitude
› Uses one bit to represent the sign
0 = positive, 1 = negative
› Remaining bits are used to represent the magnitude
› Range - (2n-1 – 1) to 2n-1 - 1
where n=number of digits
› Example: Let n=4:
› Range is –7 to 7 or
1111 to 0111
1's complement
12
Signed-Magnitude and 1's Complement Disadvantages
13
2's Complement
14
Unsigned
1110
1111
15
0000
0
0 0001
1
0010
14 -1 +1
2
-2 +2
4-bit 2's value 4-bit 2's value 1101
13 0011
-3 +3 3
complement complement Signed Values
(2’s Complement) +4 4 0100
1100 12 -4
0000 0
1011
11
-5
-6
-+ +6
+5
5
0101
0001 1 1111 −1 10
-7
-8
+7 6
0110
1010 7
9
0010 2 1110 −2 1001
8
1000
0111
0011 3 1101 −3
0100 4 1100 −4
0101 5 1011 −5
0110 6 1010 −6
0111 7 1001 −7
1000 −8
› A shortcut method:
› Copy bits from right to left up to (and including) the first '1'.
Then flip remaining bits to the left
17
Converting Binary (2's C) to Decimal
X = 01101000 two
2 4
3 8
4 16
= 2 + 2 + 2 = 64 + 32 + 8
6 5 3
5 32
=104 ten 6
7
64
128
› In the
8
following 256
table, read
‘2n’ as 2 to
9 512
› Examples use 8-bit 2’s complement numbers the power
n.
10 1,024
18
More Examples
X = 00100111two
n 2n
= 2 + 2 + 2 + 2 = 32 + 4 + 2 + 1
5 2 1 0 0 1
1 2
= 39ten 2 4
3 8
X = 11100110 two 4 16
5 32
− X = 00011010 6 64
= 24 + 23 + 21 = 16 + 8 + 2 7
8
128
256
= 26ten 9 512
10 1,024
X = −26 ten
› Examples use 8-bit 2’s complement numbers
19
Converting Decimal to Binary (2's C)
› First Method: Division
1. Find magnitude of decimal number (conversion always positive)
2. Divide by two – remainder is least significant bit
3. Keep dividing by two until answer is zero, writing remainders
from right to left
4. Append a zero as the MS bit; if original number was negative, flip
bits and add +1
X = 104 ten 104 / 2 = 52 r 0 bit 0
52 / 2 = 26 r 0 bit 1
26 / 2 = 13 r 0 bit 2
13 / 2 = 6 r1 bit 3
6 / 2 = 3 r0 bit 4
3 / 2 = 1 r1 bit 5
X = 01101000 two 1/ 2 = 0 r1 bit 6
20
Converting Decimal to Binary (2's C)
n 2n
› Second Method: Subtract Powers of Two 0 1
X = 104 ten
10 1,024
104 − 64 = 40 bit 6
40 − 32 = 8 bit 5
8−8=0 bit 3
X = 01101000 two
21
Fractions: Fixed-Point Binary
25
Operation: Subtraction
26
Operation: Sign Extension
27
Overflow
› If the numbers are two big, then we cannot represent the sum using
the same number of bits
› For 2's complement, this can only happen if both numbers are positive
or both numbers are negative
28
Hexadecimal Notation: Binary Shorthand
0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 A 10
0011 3 3 1011 B 11
0100 4 4 1100 C 12
0101 5 5 1101 D 13
0110 6 6 1110 E 14
0111 7 7 1111 F 15
30
Very Large or Very Small Numbers: Floating-Point
32
Floating Point Representation
› The binary number is written in scientific format as
F × 2E
› F is normalized to be between 1 and 2
1≤F<2
› Because F always has the format 1.xxxx, we may
avoid storing 1, and assume its existence.
› 127 is added to E before storing it. Therefore,
negative exponents will be between 1 and 126.
Floating Point Representation
› The sign of the number is given by the left-most bit.
› The smallest valid value of E is 1.
› If E is zero, the existence of 1 before floating point is
not assumed
35
Converting from Decimal to Floating-Point Binary
› How do we represent −6 5 8 as a floating-point binary number?
36
Special Values
Infinity
• If exponent field is 11111111, the number represents infinity
• Can be positive or negative (per sign bit)
Subnormal
• If exponent field is 00000000, the number is not normalized. This
means that the implicit 1 is not present before the binary point.
The exponent is −126
−126
• N = (−1) 0. fraction 2 .
S
Example:
00000000000000000000000000000000
Binary Coded Decimal Representation
› Digital computers employ the binary representations
for integers and floating point numbers.
› However, not every decimal fraction can be
represented using binary floating point numbers.
› Example:
• Convert 0.1 (10) to binary => 0.0001100110011….
• 0.1 x 2 = 0.2
• 0.2 x 2 = 0.4
• 0.4 x 2 = 0.8
• 0.8 x 2 = 1.6
• 0.6 x 2 = 1.2
• 0.2 x 2 =0.4
Binary Coded Decimal Representation
› The use of binary fractions has some unintended
consequences, and their use does not suffice for all
computations.
› For example, consider a bank account that stores
Euros and cents.
› Cents are usually represented as hundredths of
Euros.
› 10.83 denotes 10 Euros and 83 cents.
› If binary floating point arithmetic is used for bank
accounts, individual cents are rounded, making the
totals inaccurate.
BCD Codes
› BCD systems represent digits in binary where four
bits are used to represent each digit.
Character Sets
› Bits have no intrinsic meaning; the hardware or
software must determine what each bit represents.
› More than one interpretation can be used; a group of
bits can be created with one interpretation and later
used with another.
› Example:
• character data has both a numeric and symbolic
interpretation.
› Each computer system defines a character set to be a
set of symbols that the computer and I/O devices
agree to use.
Character Sets
› A typical character set contains uppercase and
lowercase letters, digits, and punctuation marks.
› In the 1960s, IBM Corporation chose the Extended
Binary Coded Decimal Interchange Code (EBCDIC)
representation as the character set used on IBM
computers.
› The American National Standards Institute (ANSI)
defined a character representation known as the
American Standard Code for Information
Interchange (ASCII).
› The goal was to make peripheral devices compatible.
ASCII Codes
Summary
› Digital computers use binary number to represent
data.
› Arithmetic operations on binary data can be
performed in sign-magnitude, 1’s complement, or 2’s
complement forms.
› Floating point representation of binary numbers use
sign-exponent-fraction format.
› Floating point representation includes a discrete
subset of real numbers.
› Some decimal numbers cannot be represented using
a limited number of binary digits.
Questions?