Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.

Slides:



Advertisements
Similar presentations
HEXADECIMAL NUMBERS Code
Advertisements

CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
COMP3221 lec05-numbers-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 5: Number Systems – I
Chapter 2 : Number System
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
Data Representation Computer Organization &
COMP3221 lec06-numbers-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 6: Number Systems - II
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Data Representation COE 205
Assembly Language and Computer Architecture Using C++ and Java
The slides are modified from Dan Garcia,
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
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.
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.
Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.
1 Lecture 2: Number Systems Binary numbers Base conversion Arithmetic Number systems  Sign and magnitude  Ones-complement  Twos-complement Binary-coded.
1.6 Signed Binary Numbers.
Lecture 2: Number Representation
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
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
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
CSCI 136 Lab 1 Outline Go through the CD attached to the textbook. Homework Hints 135 Review.
Lecture 5.
Computers Organization & Assembly Language
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
1 Modified from  Modified from 1998 Morgan Kaufmann Publishers Chapter Three: Arithmetic for Computers citation and following credit line is included:
IT253: Computer Organization
Computing Systems Basic arithmetic for computers.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Complement Numbers. Outline  Negative Numbers Representation  Sign-and-magnitude  1s Complement  2s Complement  Comparison of Sign-and-Magnitude.
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 2.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
Representation of Data Ma King Man. Reference Text Book: Volume 2 Notes: Chapter 19.
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
Number Systems and Codes. CS2100 Number Systems and Codes 2 NUMBER SYSTEMS & CODES Information Representations Number Systems Base Conversion Negative.
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.
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.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Computer Math CPS120 Introduction to Computer Science Lecture 4.
AEEE2031 Data Representation and Numbering Systems.
By Jariya Phongsai A two's-complement system is a system in which negative numbers are represented by the two's complement of the absolute value; this.
Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.
SAK Chapter 21 Chapter 2 : Number System 2.1 Decimal, Binary, Octal and Hexadecimal Numbers 2.2 Relation between binary number system with other.
Cs641 pointers/numbers.1 Overview °Stack and the Heap °malloc() and free() °Pointers °numbers in binary.
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.
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 23: Aritmatika 18 Desember 2002 Bobby Nazief Johny Moningka
Negative Integers Unsigned binary representation can not be used to represent negative numbers. With paper and pencil arithmetic, a number is made negative.
In decimal we are quite familiar with placing a “-” sign in front of a number to denote that it is negative The same is true for binary numbers a computer.
ECE 3110: Introduction to Digital Systems Number Systems: signed numbers.
EE204 L03-ALUHina Anwar Khan EE204 Computer Architecture Lecture 03- ALU.
Lecture 2: 8/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
CS2100 Computer Organisation
Addition and Subtraction
Lec 3: Data Representation
CS2100 Computer Organisation
Negative Integers Unsigned binary representation can not be used to represent negative numbers. With paper and pencil arithmetic, a number is made negative.
March 2006 Saeid Nooshabadi
CS1010 Programming Methodology
EEL 3705 / 3705L Digital Logic Design
Presentation transcript:

Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic

Number Representation (2) Fall 2005 Numbers: positional notation Number Base B  B symbols per digit: Base 10 (Decimal):0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Base 2 (Binary):0, 1 Number representation: d 31 d d 1 d 0 is a 32 digit number value = d 31  B 31 + d 30  B d 1  B 1 + d 0  B 0 Binary:0,1 (In binary digits called “bits”) 0b11010 = 1      2 0 = = 26 #s often written 0b…

Number Representation (3) Fall 2005 Hexadecimal Numbers: Base 16 Hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Normal digits + 6 more from the alphabet In C and SPIM, written as 0x… (e.g., 0xFAB5) Conversion: Binary  Hex 1 hex digit represents 16 decimal values 4 binary digits represent 16 decimal values  1 hex digit replaces 4 binary digits One hex digit is a “nibble”. Two is a “byte” Example: (binary) = 0x_____ ?

Number Representation (4) Fall 2005 Decimal vs. Hexadecimal vs. Binary Examples: (binary) = 0xAC (binary) = (binary) = 0x17 0x3F9 = (binary) How do we convert between hex and Decimal? A B C D E F1111 MEMORIZE! Examples: (binary) = 0xAC (binary) = (binary) = 0x17 0x3F9 = (binary) How do we convert between hex and Decimal?

Number Representation (5) Fall 2005 BIG IDEA: Bits can represent anything!! Characters? 26 letters  5 bits (2 5 = 32) upper/lower case + punctuation  7 bits (in 8) (“ASCII”) standard code to cover all the world’s languages  8,16,32 bits (“Unicode”) Logical values? 0  False, 1  True Colors ? Ex: Locations / addresses? MIPS instructions? MEMORIZE: N bits  at most 2 N things Red (00)Green (01)Blue (11)

Number Representation (6) Fall 2005 Signed numbers representation Till now, we have only considered how unsigned numbers can be represented. There are three common ways of representing signed numbers: Sign-And-Magnitude 1s complement 2s complement

Number Representation (7) Fall 2005 Negative Numbers: Sign-and-Magnitude (I) Negative numbers are usually written by pre-pending a minus sign in front.  Example: - (12) 10 = - (1100) 2 In computer memory of fixed width, this sign is usually represented by a bit: 0 for + 1 for -

Number Representation (8) Fall 2005 Negative Numbers: Sign-and-Magnitude (II) Example: an 8-bit number can have 1-bit sign and 7-bits magnitude. sign magnitude

Number Representation (9) Fall 2005 Negative Numbers: Sign-and-Magnitude (III) Largest Positive Number: (127) 10 Largest Negative Number: (127) 10 Zeroes: (0) (0) 10 Range: - (127) 10 to +(127) 10

Number Representation (10) Fall 2005 Negative Numbers: Sign-and-Magnitude (IV) To negate a number, just invert the sign bit. Examples: - ( ) sm = ( ) sm - ( ) sm = ( ) sm

Number Representation (11) Fall 2005 Shortcomings of sign and magnitude? Arithmetic circuit complicated Special steps depending whether signs are the same or not Also, two zeros 0x = +0 ten 0x = -0 ten What would two 0s mean for programming? Therefore sign and magnitude abandoned

Number Representation (12) Fall s and 2s complement notations In these notations, a positive number is represented as it is (like an unsigned positive number) A negative number, however, is represented by taking the complement of unsigned number

Number Representation (13) Fall s Complement (I) 1s complement of an unsigned number is obtained by inverting all the bits of the number Examples: 1s complement of ( ) 2 is ( ) 1s 1s complement of ( ) 2 = ( ) 1s 1s complement representation of the number in 8-bits Example: 1.(+14) 10 = ( ) 2 = ( ) 1s 2.(-14) 10 = -( ) 2 = ( ) 1s 3.(-80) 10 = (?) 2 = (?) 1s

Number Representation (14) Fall s Complement (II) For 8-bits number system: Largest Positive Number: (127) 10 Largest Negative Number: (127) 10 Zeroes: Range: -(127) 10 to +(127) 10 The most significant bit still represents the sign: 0 = +,1 = -

Number Representation (15) Fall s Complement (III) Given a number x which can be expressed as an n-bit binary number, its negative value can be obtained in 1s-complement representation using: - x = 2 n - x - 1 Example: With an 8-bit number , its negative value, expressed in 1s complement, is obtained as follows: -( ) 2 = - (12) 10 = ( ) 10 = (243) 10 = ( ) 1s

Number Representation (16) Fall 2005 Shortcomings of One’s complement Arithmetic still a somewhat complicated. Still two zeros 0x = +0 ten 0xFFFFFFFF = -0 ten Although used for a while on some computer products, one’s complement was eventually abandoned because another solution was better.

Number Representation (17) Fall s Complement (I) 2s complement of an unsigned number is obtained by inverting all the bits and adding 1. Examples: 1. 2s complement of ( ) 2 = ( ) 1s (invert) = ( ) 2s (add 1) 2. 2s complement of ( ) 2 = ( ) 1s (invert) = ( ) 2s (add 1)

Number Representation (18) Fall s Complement (II) 2s complement representation for 8 bit numbers: Example: 1.(+14) 10 = ( ) 2 = ( ) 2s 2.(-14) 10 = -( ) 2 = ( ) 2s 3.(-80) 10 = (?) 2 = (?) 2s

Number Representation (19) Fall s Complement (III) Given a number x which can be expressed as an n-bit binary number, its negative number can be obtained in 2s-complement representation using: - x = 2 n - x Example: With an 8-bit number , its negative value in 2s complement is thus: -( ) 2 = - (12) 10 = ( ) 10 = (244) 10 = ( ) 2s

Number Representation (20) Fall s Complement (IV) Largest Positive Number: (127) 10 Largest Negative Number: (128) 10 Zero: Range: -(128) 10 to +(127) 10 The most significant bit still represents the sign: 0 = +, 1 = -

Number Representation (21) Fall ’s Complement Number “line”: N = 5 2 N-1 non- negatives 2 N-1 negatives one zero how many positives?

Number Representation (22) Fall 2005 Two’s Complement for N= two = 0 ten two = 1 ten two = 2 ten two = 2,147,483,645 ten two = 2,147,483,646 ten two = 2,147,483,647 ten two = –2,147,483,648 ten two = –2,147,483,647 ten two = –2,147,483,646 ten two =–3 ten two =–2 ten two =–1 ten One zero; 1st bit called sign bit 1 “extra” negative:no positive 2,147,483,648 ten

Number Representation (23) Fall 2005 Two’s Complement Formula Can represent positive and negative numbers in terms of the bit value times a power of 2: d 31 x -(2 31 ) + d 30 x d 2 x d 1 x d 0 x 2 0 Example: 1101 two = 1x-(2 3 ) + 1x x x2 0 = = = = -3 ten

Number Representation (24) Fall 2005 Arithmetic Complement numbers can help perform subtraction. With complements, subtraction can be performed by addition.

Number Representation (25) Fall 2005 Use of complements Complement number system is used to minimize the amount of circuitry needed to perform integer arithmetic. For example, A-B can be performed by computing A + (- B), where (-B) is represented in 2s complement of B. Hence, the computer needs only binary adder and complementing circuit to handle both addition and subtraction

Number Representation (26) Fall 2005 Overflow Signed binary numbers are of a fixed range. If the result of addition/subtraction goes beyond this range, overflow occurs. Two conditions under which overflow can occur are: (i) positive add positive gives negative (ii) negative add negative gives positive

Number Representation (27) Fall s complement addition Algorithm: 1.Perform binary addition on the two numbers. 2.Ignore the carry out of the MSB. 3.Check for overflow: Overflow occurs if the carrier into and out of the MSB are different.

Number Representation (28) Fall s complement subtraction Algorithm for performing A - B: A-B = A + (-B) 1.Take 2s complement of B by inverting all the bits and adding 1 2.Add the 2s complement of B to A

Number Representation (29) Fall 2005 Examples: 2s addition/Subtraction 4-bits system

Number Representation (30) Fall 2005 Examples: Overflow in 2s addition/Subtraction 4-bits system

Number Representation (31) Fall s complement Addition/Subtraction rules Algorithm C=A+B: 1.Perform binary addition on the two numbers 2.If there is a carry out of the MSB, add 1 to the result (to get C) 3.Check for overflow: if carried into and out of of MSB are different and C is opposite sign of A and B Algorithm A-B 1.Complement all bits of B 2.Proceed as addition

Number Representation (32) Fall 2005 Examples: 1s addition/subtraction

Number Representation (33) Fall 2005 Quickie Quiz 1. In a 6-bit 2’s complement binary number system, what is the decimal value represented by (100100) 2s ? a. -4 b.36 c.-36 d.-27 +e In a 6-bit 1’s complement binary number system, what is the decimal value represented by (010100) 1s ? a.-11 b.43.c-43 +d.20 e For 2’s complement binary numbers, the range of values for 5-bit numbers is a..0 to 31 b.-8 to +7 c.-8 to +8 d.-15 to e. -16 to +15

Number Representation (34) Fall 2005 Quickie Quiz 4. In a 4-bit twos-complement scheme, what is the result of this operation: (1011)_2s + (1001)_2s? a b c d e. overflow Q5. Perform subtraction with the following unsigned binary numbers by taking first the 1's complement, and then, the 2's complement, of the subtrahend: (this is a 6 bit system) (a) (26-16) (b) (26-13) (c) (4-48) (d) (84-84)