l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Similar documents
211: Computer Architecture Summer 2016

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

MACHINE LEVEL REPRESENTATION OF DATA

COMP2611: Computer Organization. Data Representation

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Inf2C - Computer Systems Lecture 2 Data Representation

COMP Overview of Tutorial #2

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Chapter 4: Data Representations

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

M1 Computers and Data

COMP2121: Microprocessors and Interfacing. Number Systems

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

CS 101: Computer Programming and Utilization

Chapter 3: Arithmetic for Computers

Introduction to Computers and Programming. Numeric Values

Number System. Introduction. Decimal Numbers

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Topic Notes: Bits and Bytes and Numbers

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

CHW 261: Logic Design

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

Topic Notes: Bits and Bytes and Numbers

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

SIGNED AND UNSIGNED SYSTEMS

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Floating Point Arithmetic

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Integer Representation

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Numeric Encodings Prof. James L. Frankel Harvard University

The type of all data used in a C++ program must be specified

Module 2: Computer Arithmetic

World Inside a Computer is Binary

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Final Labs and Tutors

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

The type of all data used in a C (or C++) program must be specified

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Chapter 2 Bits, Data Types, and Operations

UNIT 7A Data Representation: Numbers and Text. Digital Data

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

CO212 Lecture 10: Arithmetic & Logical Unit

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

Under the Hood: Data Representation. Computer Science 104 Lecture 2

BSM540 Basics of C Language

Number Systems. Both numbers are positive

EE 109 Unit 19. IEEE 754 Floating Point Representation Floating Point Arithmetic

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

More about Binary 9/6/2016

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

MC1601 Computer Organization

BINARY SYSTEM. Binary system is used in digital systems because it is:

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

ECE 372 Microcontroller Design Assembly Programming Arrays. ECE 372 Microcontroller Design Assembly Programming Arrays

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

EE292: Fundamentals of ECE

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Data Representations

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

Binary Representations and Arithmetic

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

Representing numbers on the computer. Computer memory/processors consist of items that exist in one of two possible states (binary states).

Divide: Paper & Pencil

CHAPTER 1 Numerical Representation

Numerical Representations On The Computer: Negative And Rational Numbers

INTEGER REPRESENTATIONS

Digital Fundamentals

D I G I T A L C I R C U I T S E E

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

Number Systems and Number Representation

Data Representations & Arithmetic Operations

CHAPTER 2 Data Representation in Computer Systems

Numerical Representations On The Computer: Negative And Rational Numbers

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Integer Representation Floating point Representation Other data types

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

Computer Systems Programming. Practice Midterm. Name:

ECE232: Hardware Organization and Design

Signed Binary Numbers

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Beginning C Programming for Engineers

CHAPTER 2 Data Representation in Computer Systems

Number Systems and Computer Arithmetic

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Midterm Exam Review Slides

Transcription:

198:211 Computer Architecture Topics: Lecture 8 (W5) Fall 2012 Data representation 2.1 and 2.2 of the book Floating point 2.4 of the book Computer Architecture What do computers do? Manipulate stored information Manipulation: operations more on this later Information is data: how is it represented? Basic information: numbers Human beings have represented numbers throughout history Roman numerals Decimal system Discoveregypt.com 1 2 Number System Comprises of Set of numbers or elements Operations on them (+ - / *) Rules that define properties of operations (identity, inverse ) Need to assign value to numbers Let us take decimal system 1 s place, 10 s place, 100 s place etc Base 10 Value= i 10 i Humans use decimal Binary numbers Base 2; each digit is 0 or 1 Each bit in place i has value 2 i Binary representation is used in computers Easy to represent by switches (on/off) Manipulation by Digital logic in hardware But hard for humans to read (13) 10 = (1101) 2 3 4 1

Hexadecimal representation Binary hard to read for humans Especially 16 bits, 32 bits, 64 bits 1010101001010101 Base 16 representation or hex representation Symbols ={0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} Value = i 16 i First 10 (0 through 9) symbols are same as decimal numbers A=10,B=11,C=12, D=13, E=14, F=15 Decimal to binary main(){ int N,i=0; int Maxbits=32; int bitarray[maxbits]; scanf ( %d, &N): for (i=0;i<maxbits;i++) bitarray[i]=0; /* initialize */ i=0; while (N > 0){ /* MSB is bit 0, LSB is bit 31 */ bitarray[maxbits-1-i]=n%2;i++; /* mod function */ N=N/2; } i=0; for (i=0; i<maxbits; i++) printf( %d,bitarray[i]); } 5 6 Same for Hex Example: Convert 10 in decimal to binary The code for binary works for any base with modifications For hex, replace by N%16 and N=N/16 replace reminders > 9 by A, B, C, D, E and F Example: Convert 240 to HEX Binary to decimal Convert 110110 to decimal Value = i*2 i Value = 2 5 2 4 2 2 2 1 = 32+ 16 + 4 + 2 = 54 7 8 2

Converting Hex to binary Each digit in Hex can be represented by 4 bit binary (base 16) or nibble Convert 2A8C to binary 0010 1010 0100 1100 Convert Binary to hex Group binary bits into groups of four Replace each nibble by a hex digit Example 1011011110011100 1011011110011100 B79C or 0x B79C In C, numeric constants starting with 0x are interpreted as being in hexadecimal 9 10 Examples 0x8F7A93 to binary 1011011110011100 to hex 0xC4E5 to decimal Decimal and binary fractions In decimal, digits to the right of radix point have value 1/10 i for each digit in the i th place 0.25 is 2/10 + 5/100 Similarly, in binary, digits to the right of radix point have value 1/2 i for each i th place after the decimal point. Binary Fractions are the same except the base is different 8.625 is 1000.101 11 12 3

Decimal to binary example Decimal to binary fractions 0.625 to binary ANS: 0.101 0.625*2 = 1.25 output 1 0.25*2 =0.5 output 0 0.5*2 = 1 output 1 Exit Algorithm /* precondition: 0< number <1) number=decimalfraction while (number >0) { number=number*2 /*shift left */ if (number >=1) {Output result=1; number=number-1} else Output result=0 } Decimal Binary 0.5 0.1 0.25 0.01 0.625 0.101 0.75 0.11 13 14 Data sizes Big endian vs little endian All Information is represented in binary form but require different sizes Characters in 1 byte, integers 2 to 4 bytes, real numbers 4 to 8 bytes C declaration 32-bit machine 64-bit machine char 1 1 Short int 2 2 int 4 4 pointer 4 8 float 4 4 double 8 8 15 A binary representation of a number in memory may require multiple bytes How to determine value of a sequence of bytes Most Significant byte first big endian Lease significant byte first little endian Depends on the type of machine Why we need to know: Look at machine code If we are to interpret bytes and determine value One computer (big endian) sending data to another computer (small endian) Need to convert into standard form before transmitting 16 4

Representing integers Signed integers Unsigned or natural numbers Directly represent as binary What about negative numbers? Signed Magnitude MSB is sign bit S Magnitude 4 is 0100-4 1100 3 is 0011-3 1011 2 is 0010-2 1010 1 is 0001-1 1001 0 is 0000..what is 1000? Problems: two zeros +ve 0 and ve 0 normal bit-wise addition does not work -1 + 4 Representing Integers Divide the binary space into two halves One with leading 0s are +ve One with leading 1s are ve This is called two s complement representation Two bit binary numbers 00 01 10 11 0 1-2 -1 Three bit 000 001 010 011 100 101 110 111 0 1 2 3-4 -3-2 -1 17 18 2s complement advantages Adding two integers works as normal arithmetic Normal arithmetic works for 2 s complement (ignore carry) Only 1 zero X+ (-X) = 0 Used in almost all computers Negate a 2s complement invert the bits and add 1 X = 001 X 110 à 111 All 1s is -1, so add 1 to make it zero So, to get X, invert all bits and add 1 The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again. 19 20 5

Finding 2s complement Take the binary representation and invert all bits Step1: 0 to 1 and 1 to 0 Step 2:Then add 1 Example: 00101 (5) (-5) is Find 2s complement of 9 Finding 2s complement Copy all bits from right to left until first 1 Atlantic ocean rule!! At Then flip all bits 0100 1 1011 1 Example find 2s complement of 0100, 0110 0100 0110 21 22 Decimal value of 2s complement Most significant bit has ve value X n-1, X n-2,.. X 1, X 0 Value of MSB is -2 (n-1) Rest is same as +ve binary number 110011 what is the decimal value? Decimal value of 2 s complement If leading bit or MSB is 1, take 2 s complement to get +ve number Determine decimal value of number And add ve sign to it If leading bit or MSB is 0, compute as normal Example 1100110 TWO 23 24 6

1s complement Alternative representation -ve numbers represented by the complement Max numbers that can be represented is halved When adding ve numbers carry need to be added to the result -3 + (-2) 100 101 1 001 à add carry 1 010 à 2 but correct answer is (-)5 Overflow 000 001 010 011 100 101 110 111 0 1 2 3-3 -2-1 -0 101 (-2) 110 (-1) ----- 1011 -------- 1 --------- 100 (-3) ASCII Characters are also stored as bits 1 byte is 8 bits but MSB is used for error detection ASCII represents typical keys on a keyboard 7 bits is 128 different possibilities 7 bit ASCII included printable and nonprintable characters Non-printable for controlling printer such as LF or linefeed 25 26 ASCII table unicode Extended to 16 bit characters Represent other characters (Japanese) Java supports this format In Unicode, the first 128 characters are ASCII 27 28 7

Floating point With integers range is limited short int 2 bytes 2 16 unsigned short int Unsigned: Range is natural number 0 to 65536 Signed: 2s complement is -32768 to 32767 for int (4 bytes) -2147483648 to 2147483647 Can also be represented as magnitude and exponent 2.147483647x10 9 Scientific notation 2.147483647x10 9 Mantissa exponent Number of digits in Mantissa à precision Exponent gives range A binary number can also be expressed in this form 10111011 à 1.0111011x2 7 29 30 Same number with varying exponent 10101 à 21.. In decimal 1010.1 x 2 à 10.5 x 2 101.01 x 2 2 à 5.25 x 4 10.101 x 2 3 à 2.625 x 8 Too many ways to represent the same number Need a standard representation IEEE floating point standard Most computers follow IEEE 754 standard Single precision (32 bits) Double precision (64 bits) Extended precision (80 bits) S Fraction Exponent 31 32 8

Floating point in C 32 bits single precision or float 1 sign bit, 23 bits for mantissa, 8 bits for exponent Exponent is power of 2 Signed magnitude Sign bit is 1 for ve numbers, sign bit is 0 for +ve numbers Exponent has 8 bits 0 to 256 or -128 to + 127 2 128 is approx 10 38 Range is -3.4x 10 38 to +3.4x 10 38 For double precision 1 sign bit, 11 bits for exponent, 52 bits for mantissa Range is -1.798x 10 308 to +1.798x 10 308 Exponent bias The binary exponent is represented by adding a bias of 127 Saves an extra bit for sign bit Note: it is not 2s complement 2 3 is 2 130 which is 2 10000010 2 0 is 2 127 which is 2 01111111 2-3 is 2 124 which is 2 01111100 33 34 Normalization The exponent value is adjusted so that the mantissa has a 1 before the radix point 0.25 is 0.01x 2 0 adjusted to 1x 2-2 4.625 is 100.101 x 2 0 adjusted to 1.00101 x2 2 64 is 1000000 x 2 0 adjusted 1x 2 6 In this way, the mantissa always has 1 digit with value 1 This can be omitted to save a bit!!! Decimal to floating point 5.625 In binary 101.101 à 1.01101 x 2 2 Exponent field has value 2 add 127 to get 129 Exponent is 10000001 Mantissa is 01101 Sign bit is 0 0 0110100000000000000000 10000001 35 36 9

One more example Convert 12.375 to floating point representation Binary is 1100.011 Floating point to decimal 1 10000010 0100100000000000000000 What is the decimal value of the above? Exponent is (10000010) - 127 Mantissa is 1.01001 Sign bit is -1 s f e (1 2s) (1 + f ) 2 e bias 37 38 Special cases 0 00000000 00000000000000000000000 All zeros 0 (+ 0) 1 00000000 00000000000000000000000 All zeros with sign bit is -0 (-0) 0 11111111 00000000000000000000000 Note: the minimum value in the exponent after adding the bias should be 1. Hence, the minimum value for the exponent in single precision is = -126 (=1-127 or x1-x7f) + Infinity 1 11111111 00000000000000000000000 -Infinity Note: the maximum value in the exponent after adding the bias 254. Hence, the maximum value for the exponent in single precision is = +127 (=254-127 or xfe-x7f) Extended precision 80 bits used to represent a real number 1 sign bit, 15 bit exponent, 64 bit mantissa 20 decimal digits of accuracy 10-4932 to 10 4932 Not supported in C 39 40 10