Department of Computer Science Georgia State University Data Manipulation Department of Computer Science Georgia State University
What will be covered? A review on binary notation Fractions in binary Binary addition Storing integers Storing fractions
A review on binary notation How to convert a binary into a decimal (base 10)? ( 1 0 1 1 1 )2= ( ? )10
( 1 0 1 1 1 )2= ( ? )10 23 1 Position 4 3 2 1 Position’s quantity 24 23 22 21 20 16 8 4 2 1 1 0 1 1 1 ⨯16 ⨯ 8 ⨯ 4 ⨯ 2 ⨯ 1 + + + + = 16 + 0 + 4 + 2 + 1 = 23
Fractions in Binary 1 0 1 . 0 1 1
Fractions in Binary Decimal Point 1 0 1 . 0 1 1
1 0 1 . 0 1 1 Fractions in Binary Radix Point Integer Part 1 0 1 . 0 1 1 Integer Part Fractional Part
( 1 0 1 .0 1 1 )2= ( ? )10 1 . Position 2 1 -1 -2 -3 Position’s quantity 22 21 20 2-1 2-2 2-3 4 2 1 ½ ¼ ⅛ 1 0 1 0 1 1 ⨯4 ⨯ 2 ⨯ 1 ⨯ ½ ⨯ ¼ ⨯ ⅛ + + + + + = 4 + 0 + 1 + 0 + ¼ + ⅛ = 5 ⅜
Practice 1 ( 1 0 . 0 1 1 )2= ( ? )10 ½ ¼ ⅛ Position 7 6 5 4 3 2 1 . -1 ( 1 0 . 0 1 1 )2= ( ? )10 Position 7 6 5 4 3 2 1 . -1 -2 -3 Position's quantity 27 26 25 24 23 22 21 20 2-1 2-2 2-3 128 64 32 16 8 ½ ¼ ⅛
Practice 1 ( 1 0 . 1 0 1 )2= ( ? )10 1 0 . 1 0 1 Position's quantity 128 64 32 16 8 4 2 1 . ½ ¼ ⅛ + ½ + ⅛ 2 = 2⅝ ( 1 0 . 0 1 1 )2= ( 2 + ½ + ⅛ )10=( 2⅝ ) 10
Binary Addition Base 2 Base 10 1 1 1 0 1 0 + 1 1 0 1 1 ? 5 8 + 2 7 ?
Binary Addition Base 10 1 5 8 + 2 7 5 8 2 7 8 5
Binary Addition Base 2 1 1 1 0 1 0 + 1 1 0 1 1
Binary Addition + 0 + 1 1 + 0 1 + 1 ⨯ 1 1 1 2
1 1 1 0 1 0 + 1 1 0 1 1 1 1 1 1 Binary Addition Integers Base 2 1 1 1 + 1 1 0 1 1 Integers 1 1 1 1
Binary Addition Base 2 1 0 . 0 1 1 + 1 0 0 . 1 1 0 Fractions 1 1 1 . 1
Storing Integers In the “real world” of mathematics, computers must represent both positive and negative binary numbers for positive and negative integers. For example, even when dealing with positive arguments, mathematical operations may produce a negative result. Example : 125 - 130 = -5 Thus needs to be a consistent method of representing negative numbers in computers.
Storing Integers Sign-Magnitude The simplest conceptual format Two’s complement The most popular system for representing integers within today’s computers. Excess notation Representing a fixed length binary number in computer circuits.
Sign-Magnitude Sign bit : leftmost bit 0 represents positive, 1 represents negative Magnitude : the remaining bits The same as binary notation of absolute value E.g. 4-bit sign-magnitude format But….... Sign Magnitude 0000 1000 +0 -0 E.g. Using 4-bit sign-magnitude format Calculate 110+(-1)10 +1 is stored as 0001 -1 is stored as 1001 0011 represents 3 1011 represents -3 0001 + 1001 1010 = -210
Two’s complement notation Definition: “The two’s complement of a binary integer is the 1’s complement of the number plus 1.” For each bit, change 0 to 1, and 1 to 0
Two’s complement notation How to encode an integer in two’s complement notation? For positive integers, keep the binary representation For negative integers, Start with N-bit representation of their absolute values (positive integer) Complement each bit (change 0 to 1, and 1 to 0) Add one (Binary addition algorithm) “Complement and Add One”
Two’s complement notation How to encode an integer in two’s complement notation? For positive integers, keep the binary representation. For negative integers, Start with N-bit representation of their absolute values (positive integer) Complement each bit (change 0 to 1, and 1 to 0) Add one (Binary addition algorithm) E.g. Represent -610 in Two’s complement The positive integer: 0 1 1 0 610 Complement each bit: 1 0 0 1 Add one: + 0 0 0 1 1 0 1 0 -610
Two’s complement notation Another simple way to get the two’s complement notation for a negative integer “Copy until one and Complement” 0 1 1 0 6 Copy the bits from right to left until a 1 has been copied Complement the remaining bits 0 1 1 0 1 0 1 0 - 6
Two’s complement notation Another simple way to get the two’s complement notation for a negative integer “Copy until one and Complement” The positive integer: 0 1 1 0 Copy until 1 1 0 Complement remaining: 1 0 1 0 -610 610 610 -610 The positive integer: 0 1 1 0 Complement each bit: 1 0 0 1 Add one: + 0 0 0 1 1 0 1 0 “Complement and Add one”
Two’s complement notation E.g. Represent -410 in Two’s complement using 4-bit pattern The positive integer: 0 1 0 0 410 Copy until 1: 1 0 0 Complement remaining: 1 1 0 0 -410 E.g. Represent -810 in Two’s complement using 4-bit pattern The positive integer: 1 0 0 0 810 Copy until 1: 1 0 0 0 Complement remaining: 1 0 0 0 -810
Figure 1.19 Two’s complement notation systems -4 ~ +3 -8 ~ +7
Two’s complement notation Range of integers with Two’s complement 3-bit : -4 ~ +3 4-bit : -8 ~ +7 5-bit : -16 ~ +15 8-bit : -128 ~ +127 N-bit : -23-1 ~ +23-1-1 -24-1 ~ +24-1-1 -25-1 ~ +25-1-1 -28-1 ~ +28-1-1 -2N-1 ~ +2N-1-1 So In order to store value 8 in two’ complement notation, how many bits should be used at least?
Two’s complement notation How to decode a Two’s complement notation? Check “Sign bit” Sign bit = 0 ( positive number) Convert the binary number to a decimal directly Sign bit = 1 ( negative number) Do the “ Copy until 1 and Complement ” process Convert binary into decimal E.g. Decode Two’s complement notation 1010 Two’s complement: 1 0 1 0 Negative Copy until one: 1 0 Complement remaining: 0 1 1 0 The positive integer: 0 1 1 0 610 Result: -610
Practice 2 Decode the following two’s complement (8-bit) 1 1 1 0 1 1 1 0
Practice 2 Decode the following two’s complement (8-bit) 1 1 1 0 1 1 1 0 Complement Copy 0 0 0 1 0 0 1 0 = 1 *16 + 1*2 = 18 Final answer: - 18
Addition in Two’s Complement
Overflow problem 9 is not in range -8 ~ 7, thus error happened. Problem in base ten Problem in 4-bit Two’s complement 5 0 1 0 1 + 4 + 0 1 0 0 1 0 0 1 Answer in base ten 9 -7 9 is not in range -8 ~ 7, thus error happened. We call this phenomenon as overflow.
Excess Notation Excess four conversion table 7 7 6 5 4 3 2 1
Storing Fractions Besides storing the binary representation, we need to store the position of radix. Radix Point 1 0 1 . 0 1 1 One popular way to store fractions in computer is called floating-point notation.
Floating-point Notation Scientific notation ±M x BE (M < 1) M is the mantissa, B is the exponent base (usually 2), and E is the exponent Normalized form E.g. - 0.25 * 102 , 0.11 * 22 , - 0.1* 2-3 8 bits Sign bit Exponent ( 3 bits) Mantissa ( 4 bits)
Error can be reduced by using a longer mantissa field Truncation errors Encoding the value 2 ⅝ 2½ 0 1 1 0 Error can be reduced by using a longer mantissa field
What you have learned ? Binary System Binary notation Fractions in binary Binary addition How to store integers and fractions in computer? Two’s complement notation Overflow problem Floating point notation (scientific notation) Truncation error
Thank you !
Floating-point Notation Decode a floating-point notation 1 8 bits Sign bit Exponent Mantissa Scientific notation : ± . M x B E - 4 + . 1011 x 2 110- 4 + . 1011 x 2 6- 4 + . 1011 x 2 2 2¾ 10.11
Practice 3 ( 5⅝)10= ( ? )2 0 1 0 1 . 1 0 1 ½ ¼ ⅛ 5 = 4+1 ⅝ = ½ + ⅛ ( 5⅝)10= ( ? )2 0 1 0 1 . 1 0 1 Position's quantity 128 64 32 16 8 4 2 1 . ½ ¼ ⅛ 5 = 4+1 ⅝ = ½ + ⅛
Two’s complement notation How to encode an integer in two’s complement notation? For positive integers, keep the binary representation. For negative integers, Start with N-bit representation of absolute value (positive integer) Complement each bit (change 0 to 1, and 1 to 0) Add one (Binary addition algorithm) E.g. Represent -410 in Two’s complement The positive integer: 0 1 0 0 410 Complement each bit: 1 0 1 1 Add one: + 0 0 0 1 1 1 0 0 -410
Answer ( 1 1 . 0 1 )2= ( 1*2 + 1*1 + 0*0.5+1*0.25 )10 ( 0 . 1 0 1 )2= ( 0*1 + 1*0.5 + 0*0.25 + 1*0.125 )10 ( 4.5 )10= ( 1 0 0 . 1 )2 ( 5.625)10= ( 1 0 1 . 1 0 1 )2 Position 7 6 5 4 3 2 1 . -1 -2 -3 Position's quantity 27 26 25 24 23 22 21 20 2-1 2-2 2-3 128 64 32 16 8 1/2 1/4 1/8
Patterns that Add Up to Zero How to represent -1 ? Find a particular bit pattern results that represents zero when added to one. 0001 + 1111 0000 1 -1 1 How to represent -3 ? Find a particular bit pattern results that represents zero when added to three. 0011 + 1101 0000 3 -3 1
Floating-point Notation Decode a floating-point notation 1 8 bits Sign bit Exponent Mantissa Scientific notation : ± 0 . M x B E - 4 . 0 1 0 1 0 1 1 0 . 1 0 1 1 1 . 0 1 0 1 1 0.101011 * 2-1 0.101011 * 21 0.101011 * 22 + 0 . 1011 x 2 110- 4 + 0 . 1011 x 2 6- 4 + 0 . 1011 x 2 2 10.11 2¾
Exercises 1 ( 5⅝)10= ( ? )2 5 = 4 +1 ⅝ = ½ + ⅛ 1 0 1 . 1 0 1 Position ( 5⅝)10= ( ? )2 5 = 4 +1 ⅝ = ½ + ⅛ 1 0 1 . 1 0 1 Position 7 6 5 4 3 2 1 . -1 -2 -3 Position's quantity 27 26 25 24 23 22 21 20 2-1 2-2 2-3 128 64 32 16 8 1/2 1/4 1/8
( 1 0 1 1 1 )2= ( ? )10 23 1 16 8 4 2 1 16 + + + 4 2 1 = 23