600 likes | 682 Views
CS111 – Fundamentals of CS Lecture 3 – 9 Oct. 2016 Binary System II. Mohammad El-Ramly, PhD 2016 Acadox.com/class/37658 HAFADV. دعاء. اللهم انفعنا بما علمتنا، وعلمنا ما ينفعنا، وزدني علما. حديث شريف رواه الترمذي والحاكم وصححه الألباني. حديث شريف.
E N D
CS111 – Fundamentals of CSLecture 3 – 9 Oct. 2016Binary System II Mohammad El-Ramly, PhD 2016 Acadox.com/class/37658 HAFADV
دعاء اللهم انفعنا بما علمتنا، وعلمنا ما ينفعنا، وزدني علما. حديث شريف رواه الترمذي والحاكم وصححه الألباني.
حديث شريف ”صيام يوم عاشوراء ، إني احتسب على الله أن يكفر السنة التي قبله“رواه مسلم
Lectures 2 & 3 & 4 Outline • Bits and bytes • Binary numbers • Binary operations • Binary fractions • Hexadecimal / Octal • Binary system for integers • 2’s compliment and excess notations • Binary system for fractions • Floating point representation
Reading for this lecture • Computer Science: An Overview Section 1.6 and 1.7 2. External links and sources to help you understand the materials.
What Will We Study? 2 2 H E 3 G 5 4 أ ب ت L 1 Programs Algorithms 5 4
Number Conversion Decimal Octal Hexadecimal Binary
VI. Storing Integers in ComputersNow, what is the problem? • We cannot represent –ve sign. • What is a good representation for negative numbers? • How can we simplify binary arithmetic to simplify circuit design?
One possible solution … Signed Magnitude • Use signed magnitude. + It is easy for people to understand. - It requires complicated hardware. - It allows two different representations for zero: positive zero and negative zero.
VI. Storing Integers inside a Computer • Binary system is compatible with the design of digital circuitry. • We study three binary notations: • One’s complement notation • Two’s complement notation and • Excess notation • They represent integer values inside computers & have additional properties that make them compatible with H/W design.
VI. Storing Integers inside a Computer • Memory and CPU registers are accessed in words. • A word is usually 4 bytes or 32 bits or it can be 64 bits. • An integer number is usually stored in one full word.
Signed vs. unsigned? • When to use each? • Both are useful. • Age ? Year ? Speed ? X-value? • Memory addresses are always unsigned. • Using the same number of bits, unsigned integers can express twice as many values as signed numbers.
One’s ComplimentAnother possible solution … • It is based on the Diminished Radix Complement representation. • To get the negative numbers, subtract each digit from the (base-1). • In the binary system, this gives us one’s complement. • It almost like inverting the bits.
One’s ComplimentAnother possible solution … • For example, in 8-bit one’s complement, • 3 is: 00000011 • -3 is: 11111100 • Negative values are indicated by a 1 in the highorderbit (MSB). • Advantages? • Disadvantages? (1) & (2)
One’s ComplimentAnother possible solution … 2.4 Signed Integer Representation • With one’s complement addition, the carry bit is “carried around” and added to the sum. • Example: Using one’s complement binary arithmetic, find the sum of 48 and - 19 • We note that 19 in one’s complement is00010011, so -19 in one’s complement is: 11101100.
Two’s Compliment • To express a value in two’s complement: • Decide the word / pattern length. • If the number is +ve, convert it to binary • If the number is -ve, find the one’scomplement and then add 1. • Example: • In 8-bit one’s complement, 3 is: 00000011 • -3 in one’s complement is: 11111100 • -3 in two’s complement form: 11111101
Two’s complement (3 & 4 bits) Sign bit / Most significant bit
Two’s Compliment • With two’s complement arithmetic, all we do is add our two binary numbers. Just discard any carries emitting from the high order bit. • Example: Using two’s complement binary arithmetic, find the sum of 48 and - 19.
What is the largest integer we can store? • Word size + signed or unsigned • 4-bits word unsigned: • 0000, ….., 1111 = 16 values • Max is number 15 = 24 - 1 • 4-bits word signed magnitude: • 1111, …, 0000, ., 0111 => 16 (15) • Max/min is number 7 = 24-1 - 1
What is the largest integer we can store? • Word size + signed or unsigned • n-bits word unsigned: • Max number = 2n - 1 • n-bits word signed magnitude: • Max/min number (2n-1-1) / 2n-1 • 32-bits word unsigned / signed: 4294967295, 2147483647/8
Problems when Representing Numeric Values • Binary notation: Uses bits to represent a number in base two • But it has limitations / problems. • Limitations of computer representations of numeric values • Overflow: occurs when a value is too big to be represented • Truncation: occurs when a value cannot be represented accurately
Overflow • Using a finite number of bits to represent a number, may cause risk of the result of our calculations becoming too large to be stored in the computer. • We can’t always prevent overflow, we can always detect overflow. • In complement arithmetic, an overflow condition is easy to detect. How?
Overflow 2.4 Signed Integer Representation • Example: • Using two’s complement binary arithmetic, find the sum of 107 and 46. • We see that the nonzero carry from the seventh bit overflowsinto the sign bit, giving us the erroneous result: 107 + 46 = -103.
Overflow 2.4 Signed Integer Representation • How to detect overflow in addition? • How to deal with it? • Is it different from signed and unsigned?
Excess Notation • For four bits • 0000 is the least number • 1111 is the biggest number • 1000 is the zero • Mainly used in floating-point representation of fractions.
Excess Notation • Each value is represented by a bit pattern of the same length. • First select the pattern length • Write down all the differentbit patterns of that length • Next, we observe that the first pattern with a 1 as its MSB most significant bit appears approximately halfway through the list. • We pick this pattern to represent zero
Excess NotationTable (excess 8) Negative numbers have 0 sign bit
An excess notation system using bit patterns of length three(Excess 4)
VII. Storing Fractions • How do we store fractions? • How do we store the decimal point? • What actually do we need to store?
VII. Storing Fractions • Computers use a form of scientific notation for floating-point representation • Floating-point Notation has a sign bit, a mantissa (significand) field, and an exponent field.
Why floating point? • To satisfy them all, a number format has to provide accuracy for numbers at very different magnitudes. • To satisfy the physicist, it must be possible to do calculations that involve numbers with different magnitudes. • Basically, having a fixed number of integer and fractional digits is not useful - and the solution is a format with a floating point.
Why floating point? • For an engineer building a highway, it does not matter whether it’s 10 meters or 10.0001 meters wide. • For a microchip designer, 0.0001 meters (1/10 of a millimeter) is a huge difference. • A physicist needs to use the speed of light (~ 300000000) and Newton’s gravitational constant (~0.0000000000667) together in the same calculation.
Decoding the FB value 01101011 01101011 0 1 1 0 1 0 1 1 + 2(excess notation) .1011 10.11 2.75
Truncation Error 01101010 0 1 1 0 1 0 1 0 + 2(excess notation) .1010 10.10 2.50
IEEE 754 32-bit Single Precision S E M 1 8 23 -1S × 1.M × 2E - 127 • E is an unsigned twos-complement integer. A bias of 127 is used, so that the actual exponent is E – 127.
IEEE 754 32-bit Single Precision • Exponents 00000000 and 11111111 are reserved for special purposes • The mantissa is therefore an unsignedfixed point fraction with an implicit 1 to the left of the binary point. • All zero bits (S, E, and M) means zero (0). In this case there is no leading 1 mantissa bit implied.