Number Representations and Computer Arithmetic. CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University.

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

ICS312 Set 2 Representation of Numbers and Characters.
Representing Numbers: Integers
Number Systems Discussion D4.1 Appendix C. Number Systems Counting in Binary Positional Notation Hexadecimal Numbers Negative Numbers.
King Fahd University of Petroleum and Minerals
CS 151 Digital Systems Design Lecture 3 More Number Systems.
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16)
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
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)
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
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.
Computer Systems 1 Fundamentals of Computing
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See: P&H Chapter 2.4, 3.2, B.2, B.5, B.6.
CS1800 Summer 2014 Binary Numbers. Decimal Integers  What does a decimal number like "87294" really mean?  More generally.
Codes and number systems Introduction to Computer Yung-Yu Chuang with slides by Nisan & Schocken ( ) and Harris & Harris (DDCA)
1 Lecture 2: Number Systems Binary numbers Base conversion Arithmetic Number systems  Sign and magnitude  Ones-complement  Twos-complement Binary-coded.
3. Representing Integer Data
Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :
Numbering systems.
Representing Integer Data Book : Chapter ( Subject has no point !! ) A99ACF.
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
ICS312 Set 1 Representation of Numbers and Characters.
CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION.
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.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
Number Systems Decimal (Base 10) –10 digits (0,1,2,3,4,5,6,7,8,9) Binary (Base 2) –2 digits (0,1) Digits are often called bits (binary digits) Hexadecimal.
Number Systems. Today Decimal Hexadecimal Binary –Unsigned Binary –1’s Complement Binary –2’s Complement Binary.
CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
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.
Number Representation
ECE2030 Introduction to Computer Engineering Lecture 2: Number System Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Tech.
Number Systems Decimal (Base 10) –10 digits (0,1,2,3,4,5,6,7,8,9) Binary (Base 2) –2 digits (0,1) Digits are often called bits (binary digits) Hexadecimal.
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.
Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)
Computer Math CPS120 Introduction to Computer Science Lecture 4.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.
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=
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
CPS3340 Computer Architecture Fall Semester, 2013
CEC 220 Digital Circuit Design Binary Arithmetic & Negative Numbers Monday, January 13 CEC 220 Digital Circuit Design Slide 1 of 14.
CEC 220 Digital Circuit Design Binary Arithmetic & Negative Numbers Fri, Aug 28 CEC 220 Digital Circuit Design Slide 1 of 14.
Representing Integer Data
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Arithmetic Circuits I. 2 Iterative Combinational Circuits Like a hierachy, except functional blocks per bit.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Design of Digital Circuits Reading: Binary Numbers
Lec 3: Data Representation
Data Representation – numbers Binary conversion Hexadecimal Negative numbers Binary addition Binary shifts.
Data Representation Binary Numbers Binary Addition
CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION
Computer Architecture & Operations I
University of Gujrat Department of Computer Science
CS1010 Programming Methodology
Decimal and binary representation systems
Number Systems Rayat Shikshan Sanstha’s
Number Systems Rayat Shikshan Sanstha’s
Two’s Complement & Binary Arithmetic
Presentation transcript:

Number Representations and Computer Arithmetic

CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University “Computing” with Computers Back in the “old days” (WW II time), the word “computer” meant this “Computers” were used to compute things such as trajectory tables, etc. –redundant “computers” for reliability and speed Quite effective! –new computers were tested against human computers

CS 21a 9/23/02 Odds and Ends Slide 3 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Of course, we don’t do things that way anymore! But HOW do (electronic) computers compute? The trick is to use BINARY numbers use 1’s and 0’s corresponds on current/voltage being ON or OFF easy to implement resilient against noise “Computing” with Computers

CS 21a 9/23/02 Odds and Ends Slide 4 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Java types: int and double int range: -2,147,483,648 to 2,147,483,647 double range: 4.94e-324 to 1.80e+308 These ranges depend on: Storage size Internal data representation

CS 21a 9/23/02 Odds and Ends Slide 5 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Review: Decimal Numbers Integer Representation number is sum of DIGIT * “place value” Adding two decimal numbers add by “place value”, one digit at a time d0d d1d d2d d3d d4d d5d d6d d7d = 3     10 0 = ??? Range 0 to 10 n “carry 1” because 9+3 =

CS 21a 9/23/02 Odds and Ends Slide 6 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Binary Numbers (Unsigned) Binary Integer Representation “base” of place values is 2, not b0b b1b1 1 2 b2b b3b b4b b5b b6b b7b = = = Range 0 to 2 n - 1 Humans can naturally count up to 10 values, But computers can count only up to 2 values (OFF and ON, or 0 and 1) aka “0b ”

CS 21a 9/23/02 Odds and Ends Slide 7 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Converting from Binary to Decimal 2 0 = = = = = = = = = = = 1,024 or “1K” VERY USEFUL trick for a CS/MIS person … Memorize powers of 2 from 2 0 up to 2 10 Lets you approximate any power of 2 “1 KB” is actually 1,024 bytes, not 1000 bytes “1 MB” is 1K*1KB = 2 20 bytes = 1,048,576 bytes = approximately 1 million bytes “1 GB” is 1K * 1MB = 2 30 bytes = (approx 1 billion) Example 1: what is 2 16 ? 2 6 * 2 10 = 64 * 1024 = 64 K = 65,536 Example 2: The Pentium processor does integer math with 32-bit numbers. What’s the highest unsigned number it can handle (approximately)? range = 2 n -1 = = 2 2 * 2 30 = 4 * 1 G = approximately 4 billion = actually 4*1024*1024*1024 = 4,294,967,296 (minus 1)

CS 21a 9/23/02 Odds and Ends Slide 8 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 2 0 = = = = = = = = = = = 1,024 or “1K” More practice 0b b b b Converting from Binary to Decimal

CS 21a 9/23/02 Odds and Ends Slide 9 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University From Decimal to Binary General rule: Divide and write remainder from right to left Why this works remainder gives bit 0 odd numbers have a 1 in bit 0 Note that this rule works in converting decimal to any base e.g., HEX numbers (more later) 3792 / 2 = 1896 rem / 2 = 948 rem / 2 = 474 rem / 2 = 237 rem / 2 = 118 rem / 2 = 59 rem 0 59 / 2 = 29 rem 1 29 / 2 = 14 rem 1 14 / 2 = 7 rem 0 7 / 2 = 3 rem 1 3 / 2 = 1 rem 1 1 / 2 = 0 rem 1 0b = = 3792

CS 21a 9/23/02 Odds and Ends Slide 10 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Binary Arithmetic Adding two binary numbers same, but “1 + 1 = 10” Adding two decimal numbers ??? 1 “carry 1” because 9+3 = “carry 1” because 1+1 =

CS 21a 9/23/02 Odds and Ends Slide 11 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Binary Arithmetic A bits B bits In general: Add up to 3 bits at a time per place value A and B “carry in” Output 2 bits at a time sum bit for that place value “carry out” bit (becomes carry-in of next bit) carry-in bits sum bits carry-out bits

CS 21a 9/23/02 Odds and Ends Slide 12 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University So what about negative values? In math, it is easy to represent negative values Just put a negative sign (-) prefix In computers, we extend the binary notation in order to support signed values We can use the following 3 methods: Sign and magnitude (using the Most Significant Bit or MSB) 1’s complement 2’s complement

CS 21a 9/23/02 Odds and Ends Slide 13 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Signed Integers Sign-and-Magnitude MSB represents sign bit (0 for positive, 1 for negative) has 2 “zeroes” 1’s complement flip bits easy to do subtraction STILL has 2 “zeros” 2’s complement flip bits, then add 1 easy subtraction (just negate, then add) has only 1 zero Another interpretation: add place values as before, except that MSB is negative (i.e., MSB is place value is 2 n-1 )

CS 21a 9/23/02 Odds and Ends Slide 14 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Binary Subtraction 1’s complement subtraction: flip the negative = flip -3 in 1’s complement form 1 Add the carry overflow = in 1’s complement form flip

CS 21a 9/23/02 Odds and Ends Slide 15 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Binary Subtraction 2’s complement subtraction: flip then add = flip in 2’s complement form 2 ignore overflow = in 2’s complement form flip flip +1 (flip+1 also gives positive of negative number) 2

CS 21a 9/23/02 Odds and Ends Slide 16 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Range of binary numbers Java (and most computer platforms today) use 2’s comp. Hence the range for the different int types byte (8 bits): ? short (16 bits): ? int (32 bits): -2,147,483,648 to 2,147,483,647 long (64 bits): ?

CS 21a 9/23/02 Odds and Ends Slide 17 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Hexadecimal (Hex) Numbers Base 16 Each hex digit goes from 0-9, then A-F Hex is a convenient shortform for binary 4 bits = 1 hex digit (aka nibble) 1 byte = 8 bits = 2 hex digits Another useful trick: memorize binary of 0 to F Addition and conversion to/from decimal is similar except use base 16 instead of b0b b1b1 1 2 b2b b3b b4b b5b b6b b7b = 6* = aka “0x64”

CS 21a 9/23/02 Odds and Ends Slide 18 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Some Exercises What’s the range of an n-bit sign-mag integer? How about 1’s comp? 2’s comp? Convert the ff signed 8-bit values to decimal 0b , 0b , 0x14, 0x41 Convert the ff to 16-bit binary and hex numbers: 413, 39, 1045, -3, -124, -134 Add the following pairs 0b b , 0x3F + 0x2F If 0x7F and 0x32 are signed 8-bit integers, what’s wrong with adding them and storing the result in a byte? Puzzle: How can I use my 10 fingers to count up to 1000?