1 CSE1301 Computer Programming Lecture 29: Number Representation (Part 1)

Slides:



Advertisements
Similar presentations
HEXADECIMAL NUMBERS Code
Advertisements

The Binary Numbering Systems
EMB1006 The Binary System There is no 2 Jonathan-Lee Jones.
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
Chapter 2 : Number System
ECE 331 – Digital System Design
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Assembly Language and Computer Architecture Using C++ and Java
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
1 CSE1301 Computer Programming Lecture 28 Number Representation (Integer)
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
Signed Numbers CS208. Signed Numbers Until now we've been concentrating on unsigned numbers. In real life we also need to be able represent signed numbers.
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
3. Representing Integer Data
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CENG 311 Machine Representation/Numbers
Simple Data Type Representation and conversion of numbers
Lecture 5.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Number Systems Part 2 Numerical Overflow Right and Left Shifts Storage Methods Subtraction Ranges.
Representing Integer Data Book : Chapter ( Subject has no point !! ) A99ACF.
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
IT253: Computer Organization
Binary Arithmetic & Data representation
1 Digital Systems and Binary Numbers EE 208 – Logic Design Chapter 1 Sohaib Majzoub.
Data Representation.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Number Systems Spring Semester 2013Programming and Data Structure1.
Digital Logic Design Lecture 3 Complements, Number Codes and Registers.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
Bits, Data Types, and Operations Slides based on set prepared by Gregory T. Byrd, North Carolina State University.
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Positional Number Systems
Lecture 2 Binary Values and Number Systems. The number 943 is an example of a number written in positional notation. The relative positions of the digits.
THE BINARY SYSTEM.
ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were.
Computer Organization and Design Information Encoding II Montek Singh Wed, Aug 29, 2012 Lecture 3.
ECE 301 – Digital Electronics Representation of Negative Numbers, Binary Arithmetic of Negative Numbers, and Binary Codes (Lecture #11) The slides included.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
CEC 220 Digital Circuit Design Binary Codes
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
CEC 220 Digital Circuit Design Binary Arithmetic & Negative Numbers Monday, January 13 CEC 220 Digital Circuit Design Slide 1 of 14.
CSE1301 Computer Programming Lecture 32: Number Representation
EEE342 Digital Electronics Ian McCrumRoom 5B18, Lecture 2: Codes & Arithmetic.
CEC 220 Digital Circuit Design Binary Codes Mon, Aug 31 CEC 220 Digital Circuit Design Slide 1 of 14.
Two’s and one’s complement arithmetic CLOCK ARITHMETIC.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Nguyen Le CS147.  2.4 Signed Integer Representation  – Signed Magnitude  – Complement Systems  – Unsigned Versus Signed Numbers.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Advanced Binary b. describe and use two’s complement and sign and magnitude to represent negative integers; c. perform integer binary arithmetic, that.
Representing Positive and Negative Numbers
Computer Representation of Information
Number Systems.
Data Structures Mohammed Thajeel To the second year students
ECE 331 – Digital System Design
Presentation transcript:

1 CSE1301 Computer Programming Lecture 29: Number Representation (Part 1)

2 Topics Binary numbers Signed Magnitude Two’s Complement Excess-k

3 Representing Characters ASCII encoding (American Standard Code for Information Interchange) Each character represented by a one-byte (8-bit) number Other representations possible too: –EBCDIC (used by older IBM machines) –Unicode (16-bit character codes, provides enough codes for characters from all modern languages)

4 Representing Integers We represent integers using a base-10 positional notation. So the number means: 9      10 0

5 Representing Integers Computers represent integers using a base-2 positional notation. So the number means: 1             1 43

6 Representing Integers Curiously, ye Olde Englishe pub-keepers used the same system: So the number means: 1  gallon + 0  pottle + 1  quart + 0  pint + 1  chopin + 1  gill 1  4.6l + 0  2.3l + 1  1.1l + 0  0.6l + 1  0.3l + 1  0.1l 6.1 litres

7 Representing Integers The first few binary numbers are:

8 Converting decimal to binary We've seen that you convert binary to decimal by multiplying and adding. So it's no surprise that you convert decimal to binary by dividing and subtracting (well, remaindering, actually).

9 Converting decimal to binary 123 ÷2 61  remainder 1 ÷2 30  remainder 1 ÷2 15  remainder 0 ÷2 7  remainder 1 ÷2 3  remainder 1 ÷2 1  remainder 1 ÷2 0  remainder 1

10 Converting decimal to binary 123 ÷2 61  remainder 1 ÷2 30  remainder 1 ÷2 15  remainder 0 ÷2 7  remainder 1 ÷2 3  remainder 1 ÷2 1  remainder 1 ÷2 0  remainder 1

11 Converting decimal to binary ÷2 61  remainder 1 ÷2 30  remainder 1 ÷2 15  remainder 0 ÷2 7  remainder 1 ÷2 3  remainder 1 ÷2 1  remainder 1 ÷2 0  remainder 1

12 Converting decimal to binary 102 ÷2 51  remainder 0 ÷2 25  remainder 1 ÷2 12  remainder 1 ÷2 6  remainder 0 ÷2 3  remainder 0 ÷2 1  remainder 1 ÷2 0  remainder 1

13 Converting decimal to binary 102 ÷2 51  remainder 0 ÷2 25  remainder 1 ÷2 12  remainder 1 ÷2 6  remainder 0 ÷2 3  remainder 0 ÷2 1  remainder 1 ÷2 0  remainder 1

÷2 51  remainder 0 ÷2 25  remainder 1 ÷2 12  remainder 1 ÷2 6  remainder 0 ÷2 3  remainder 0 ÷2 1  remainder 1 ÷2 0  remainder 1 Converting decimal to binary

÷2 51  remainder 0 ÷2 25  remainder 1 ÷2 12  remainder 1 ÷2 6  remainder 0 ÷2 3  remainder 0 ÷2 1  remainder 1 ÷2 0  remainder 1 Converting decimal to binary Most significant bit

÷2 51  remainder 0 ÷2 25  remainder 1 ÷2 12  remainder 1 ÷2 6  remainder 0 ÷2 3  remainder 0 ÷2 1  remainder 1 ÷2 0  remainder 1 Converting decimal to binary Least significant bit

17 Representing Signed Integers That only takes care of the positive integers. To handle negative integers, we need to use one of the bits to store the sign. The rest of the bits store the absolute value of the number. Hence this is known as "signed magnitude" representation.

18 Representing Signed Integers If we use the first ("top") bit for the sign: – – – – – – – –7

19 Representing Signed Integers If we use the first ("top") bit for the sign: – – – – – – – –7

20 Adding binary numbers For individual bits there are only four possibilities:

21 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

22 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

23 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

24 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

25 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

26 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

27 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

28 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

29 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

30 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:    322

31 Adding binary numbers But that only works for unsigned numbers. For signed numbers (in signed magnitude format) it's much more complicated. Try and work out a scheme yourself (hint: you need to consider four cases).

32 Two's complement representation Another representation scheme makes addition and subtraction easy, regardless of the signs of the operands. Rather than using the first bit as a sign bit, we give it a negative weight. In other words, if it’s the eighth bit, its positional value is -2 7, rather than +2 7

33 Two's complement representation So now the number means: 1             1 -21

34 Two's complement representation Now the first few numbers are: – – – – – – – –1

35 Two's complement representation Now the first few numbers are: – – – – – – – –1

36 Negation with two's complement Even though the top bit indicates the sign, we can't just flip the bit to negate a number. To negate a two's complement number, we have to flip all its bits and then add 1: ( ) ( ) ( )-42

37 Even though the top bit indicates the sign, we can't just flip the bit to negate a number. To negate a two's complement number, we have to flip all its bits and then add 1: Negation with two's complement ( ) ( ) ( )+42

38 Addition with two's complement The top bit acts just like all the other bits: it tells us whether to include the multiplier for the particular position). The fact that the multiplier is negative is irrelevant. So we can just add 2 two's complement numbers as if they were unsigned.

39 Addition with two's complement The mechanics are exactly the same as in the earlier example:

40 Addition with two's complement The mechanics are exactly the same as in the earlier example:

41 Addition with two's complement The mechanics are exactly the same as in the earlier example:

42 Addition with two's complement The mechanics are exactly the same as in the earlier example:

43 Addition with two's complement The mechanics are exactly the same as in the earlier example:

44 Addition with two's complement The mechanics are exactly the same as in the earlier example:

45 Addition with two's complement The mechanics are exactly the same as in the earlier example:

46 Addition with two's complement The mechanics are exactly the same as in the earlier example:

47 Addition with two's complement The mechanics are exactly the same as in the earlier example:

48 Addition with two's complement The mechanics are exactly the same as in the earlier example (except we only have 8 bits so we throw away the extra one):

49 Addition with two's complement The mechanics are exactly the same as in the earlier example (except we only have 8 bits so we throw away the extra one):    66

50 Addition with two's complement But things don't always work out so neatly. Because we have only a limited number of bits, some sums are too big to be represented correctly.

51 Addition with two's complement Consider the addition of two large positive numbers:

52 Addition with two's complement Consider the addition of two large positive numbers:

53 Addition with two's complement Consider the addition of two large positive numbers:

54 Addition with two's complement Consider the addition of two large positive numbers:

55 Addition with two's complement Consider the addition of two large positive numbers:

56 Addition with two's complement Consider the addition of two large positive numbers:

57 Addition with two's complement Consider the addition of two large positive numbers:

58 Addition with two's complement Consider the addition of two large positive numbers:

59 Addition with two's complement Consider the addition of two large positive numbers:

60 Addition with two's complement Consider the addition of two large positive numbers:    -62 Oops! This is known as overflow.

61 Subtraction with two's complement X – Y = X + (-Y) Therefore to subtract two's complement numbers, negate the second operand and add.

62 Excess- k representation Yet another way to represent numbers in binary. For N bit numbers, k is 2 N-1 -1 So for 4-bit integers, k is 7 The value of each bit string is its unsigned value minus k.

63 Excess- k representation "Sliding ruler" k = 7 UnsignedExcess-k

64 Excess- k representation Now the first few numbers are: –7(i.e. 0–7) –6(i.e. 1–7) –5(i.e. 2–7) –4(i.e. 3–7) –3(i.e. 4–7) –2(i.e. 5–7) –1(i.e. 6–7) (i.e. 7–7) (i.e. 8–7) (i.e. 9–7) (i.e. 10–7) (i.e. 11–7) (i.e. 12–7) (i.e. 13–7) (i.e. 14–7) (i.e. 15–7)

65 Excess- k representation Now the first few numbers are: – – – – – – – –

66 Excess- k representation Top bit is still the sign bit (but now 1 is +'ve, 0 is –'ve). Numerical order is the same as lexical order. Disadvantage in arithmetic (e.g. have to subtract a k after adding two excess-k numbers)

67 Excess- k representation: Example Convert -14 (decimal) to 8-bit excess-k What is k ? k = 2 N-1 -1 = = 127 Find the number u such that u - k = -14. u = -14, implies u = 113 Convert u to unsigned binary:

68 Limitations of representations None of these number representations acts exactly like the integers. Infinitely many integers, but only 2 N binary numbers for a specific N-bit representation. That means some operations will overflow. If the program doesn't crash when that happens, it will simply produce wrong answers (which is worse!)

69 Limitations of representations Computer arithmetic is only an approximation of integer arithmetic. Many of the arithmetic laws you're used to don't always hold.

70 Reading Brookshear: Sections 1.4 to 1.7 D&D: Appendix E Donald E. Knuth, The Art of Computer Programming, Vol. 2: Semnumerical Algorithms, 2 nd edition, Addison-Wesley, 1981