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

Similar documents
Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Data Representation and Binary Arithmetic. Lecture 2

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

Chapter 2 Bits, Data Types, and Operations

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Number System (Different Ways To Say How Many) Fall 2016

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Fundamentals of Programming (C)

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Number Systems Base r

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

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Chapter 3. Information Representation

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

3.1. Unit 3. Binary Representation

The Binary Number System

Fundamentals of Programming

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

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Number Representations

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

COMP Overview of Tutorial #2

EE 109 Unit 2. Binary Representation Systems

Chapter 2 Number System

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Oberon Data Types. Matteo Corti. December 5, 2001

Simple Data Types in C. Alan L. Cox

ASSIGNMENT 5 TIPS AND TRICKS

Positional Number System

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

Fundamental Data Types

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

Number Systems CHAPTER Positional Number Systems

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Source coding and compression

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

Variables and data types

COMP2611: Computer Organization. Data Representation

UNIT 2 NUMBER SYSTEM AND PROGRAMMING LANGUAGES

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Do not start the test until instructed to do so!

Signed umbers. Sign/Magnitude otation

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Do not start the test until instructed to do so!

World Inside a Computer is Binary

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

MACHINE LEVEL REPRESENTATION OF DATA

Do not start the test until instructed to do so!

Data Representation 1

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

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Hardware. ( Not so hard really )

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

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

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

Chapter 8. Characters and Strings

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

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

= Chapter 1. The Binary Number System. 1.1 Why Binary?

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

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

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

Chapter 3: Arithmetic for Computers

Chapter 1. Hardware. Introduction to Computers and Programming. Chapter 1.2

Chapter 7. Binary, octal and hexadecimal numbers

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Objectives. Connecting with Computer Science 2

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

FA269 - DIGITAL MEDIA AND CULTURE

Lecture (09) x86 programming 8

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

Bits, Words, and Integers

Final Labs and Tutors

COMP2121: Microprocessors and Interfacing. Number Systems

unused unused unused unused unused unused

Coding Theory. Networks and Embedded Software. Digital Circuits. by Wolfgang Neff

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Module 2: Computer Arithmetic

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Topic Notes: Bits and Bytes and Numbers

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

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

IBM 370 Basic Data Types

CHW 261: Logic Design

Inf2C - Computer Systems Lecture 2 Data Representation

Transcription:

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

Think of a number between 1 and 15 8 9 10 11 12 13 14 15 4 5 6 7 12 13 14 15 2 3 6 7 10 11 14 15 1 3 5 7 9 11 13 15 2

Binary Numbers Yes = 1 No = 0 Number 7 appears on the four cards in the pattern No, Yes, Yes, Yes The number 7 in binary code is 0111 This is the Computers Language! 3

Why binary? Information is stored in computer via voltage levels. Using decimal would require 10 distinct and reliable levels for each digit. This is not feasible with reasonable reliability and financial constraints. Everything in computer is stored using binary: numbers, text, programs, pictures, sounds, videos,... 4

Bit, Byte, and Word 0 A bit is a size that can store 1 digit of a binary number, 0 or 1. 0 1 1 0 1 1 0 0 A byte is 8 bits, which can store eight 0 s or 1 s. cont d 0 1 1 1 0 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 A word is either 32 or 64 bits, depending on computers. Regular PC s are 32-bit word in size, higher-end workstations are 64-bit. Word size is the size of the registers. What do these bits mean is a matter of interpretation! All information in a computer are represented in a uniform format of bit patterns. 5

Binary Nonnegative Integers Given a 32-bit pattern, e.g., 0000 0000 0000 1101 1100, a a 0 lowest bit 31 highest bit it can represent the integer (if you interpret it that way) k n= a + a 2+ a 2 + a 2 + a 2 +, a = 0,1 2 3 0 1 2 3 Note that if we use 32-bit word, the smallest number is 0, and the largest number is 11111111 11111111 11111111 11111111, which is 2 32-1=4294967295. Numbers bigger than this cannot be represented. If such things happen in a calculation, we say it overflowed. Such interpretation is called unsigned int in the programming language C. k k 6

In General (binary) No. of bits Min Binary Max n 0 2 n -1 7

Negative Numbers Popular schemes: Signed Magnitude One s Complement Two s Complement 8

Sign-Magnitude Extra bit on left to represent sign 0 = positive value 1 = negative value E.g., 6-bit sign-magnitude representation of +5 and 5: +5: 0 0 0 1 0 1-5: 1 0 0 1 0 1 +ve 5 -ve 5 9

Ranges (revisited) No. of bits 1 2 3 4 5 6 Etc. Unsigned Min 0 0 0 0 0 0 Max 1 3 7 15 31 63 Binary Signmagnitude Min -1-3 -7-15 -31 Max 1 3 7 15 31 10

In General (revisited) No. of bits Unsigned Min Max Binary Sign-magnitude Min Max n 0 2 n -1 -(2 n-1-1) 2 n-1-1 11

Difficulties with Sign-Magnitude Two representations of zero Using 6-bit sign-magnitude 0: 000000 0: 100000 Arithmetic is awkward! pp. 95-96 12

Complementary Representations 9 s complement 10 s complement 1 s complement 2 s complement 13

Exercises Complementary Notations What is the 3-digit 10 s complement of 247? Answer: What is the 3-digit 10 s complement of 17? Answer: 777 is a 10 s complement representation of what decimal value? Answer: Skip answer Answer 14

Exercises Complementary Notations Answer What is the 3-digit 10 s complement of 247? Answer: 753 What is the 3-digit 10 s complement of 17? Answer: 983 777 is a 10 s complement representation of what decimal value? Answer: 223 15

Ones Complement Bitwise Not (simple) Used in UNIVAC Two representation for 0 16

Ones Complement binary decimal 11111110-1 + 00000010 +2...... 1 00000000 0 <-- not the correct answer 1 +1 <-- add carry...... 00000001 1 <-- correct answer 17

Two s Complement Most common scheme of representing negative numbers in computers Affords natural arithmetic (no special rules!) To represent a negative number in 2 s complement notation 1. Decide upon the number of bits (n) 2. Find the binary representation of the +ve value in n-bits 3. Flip all the bits (change 1 s to 0 s and vice versa) 4. Add 1 18

Two s Complement Example Represent -5 in binary using 2 s complement notation 1. Decide on the number of bits 6 (for example) 2. Find the binary representation of the +ve value in 6 bits 000101 +5 3. Flip all the bits 111010 4. Add 1 111010 + 1 111011-5 19

Sign Bit In 2 s complement notation, the MSB is the sign bit (as with signmagnitude notation) 0 = positive value 1 = negative value +5: 0 0 0 1 0 1-5: 1 1 1 0 1 1 +ve 5 -ve? (previous slide) 20

Complementary Notation Conversions between positive and negative numbers are easy For binary (base 2) 2 s C +ve -ve 2 s C 21

Example +5 2 s C -5 2 s C 0 0 0 1 0 1 1 1 1 0 1 0 + 1 1 1 1 0 1 1 0 0 0 1 0 0 + 1 +5 0 0 0 1 0 1 22

In General (revisited) No. of bits Unsigned Binary Sign-magnitude 2 s complement Min Max Min Max Min Max n 0 2 n -1 -(2 n-1-1) 2 n-1-1 -2 n-1 2 n-1-1 23

Negative Integers To represent negative numbers, we ll agree that the highest bit a 31 representing sign, rather than magnitude, 0 for positive, 1 for negative numbers. More precisely, all modern computers use 2 s complement representations. The rule to get a negative number representation is: first write out the bit pattern of the corresponding positive number, complement all bits, then add 1. 24

Property of 2 s Complement Numbers Two s complement m of a number n is such that adding it together you get zero (m + n = 0, modulo word size) Thus m is interpreted as negative of n. The key point is that computer has a finite word length, the last carry is thrown away. 25

2 s Complement, example 1 in bits is 00000000 00000000 00000000 00000001 complementing all bits, we get 11111111 11111111 11111111 11111110 Add 1, we get representation for 1, as 11111111 11111111 11111111 11111111 Decimal 00000000 00000000 00000000 00000001 + 11111111 11111111 11111111 11111111 1 00000000 00000000 00000000 00000000 1 1 0 Overflow bit not kept by computer 26

2 s Complement, another e.g. 125 in bits is 00000000 00000000 00000000 01111101 complement all bits, we get 11111111 11111111 11111111 10000010 Add 1, we get representation for 125, as 11111111 11111111 11111111 10000011 Decimal Overflow bit not kept by computer 00000000 00000000 00000000 01111111 + 11111111 11111111 11111111 10000011 1 00000000 00000000 00000000 00000010 127 125 +2 27

What is -5 plus +5? Zero, of course, but let s see Sign-magnitude -5: 10000101 +5: +00000101 10001010 Twos-complement 1 1-5: 11111011 +5: +00000101 00000000 1 1 1 1 1 1 28

Signed and Unsigned Int If we interpret the number as unsigned, an unsigned integer takes the range 0 to 2 32-1 (that is 000 000 to 1111.1111) If we interpret the number as signed value, the range is 2 31 to 2 31-1 (that is 1000.000 to 1111 111, to 0, to 0111.1111). Who decide what interpretation to take? What if we need numbers bigger than 2 32? 29

Addition, Multiplication, and Division in Binary + 0 0 0 1 0 0 1 1 19+37=56 0 0 1 0 0 1 0 1 0 0 1 1 1 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 0 1 1 1 7 5=35 0 1 0 1 0 1 1 1 + 0 1 1 1 1 0 0 0 1 1 74 8 = 9 remainder 2 30

Overflow - Explanation 2147483645 + 2147483645 = -6 Why? 2 31-1 = 2147483647 and has 32 bit binary representation 0111 111. This is largest 2 s complement 32 bit number. 2147483645 would have representation 011111 101. When we add this to itself, we get X = 1111 1010 (overflow) So, -X would be 000 0101 + 1 = 00 0110 = 6 So, X must be -6. 31

Floating Point Numbers (reals) To represent numbers like 0.5, 3.1415926, etc, we need to do something else. First, we need to represent them in binary, as m 2 1 2 3 k n= + am 2 + a22 + a12+ a0 + a 1 + a 2 2 + a 3 2 + a k 2 + 2 E.g. 11.00110 for 2+1+1/8+1/16=3.1875 Next, we need to rewrite in scientific notation, as 1.100110 2 1. That is, the number will be written in the form: 1.xxxxxx 2 e x = 0 or 1 32

Figure 3-7 Changing fractions to binary Multiply the fraction by 2, 33

Example 17 Transform the fraction 0.875 to binary Solution Write the fraction at the left corner. Multiply the number continuously by 2 and extract the integer part as the binary digit. Stop when the number is 0.0. 0.875 1.750 1.5 1.0 0.0 0. 1 1 1 34

Example 18 Transform the fraction 0.4 to a binary of 6 bits. Solution Write the fraction at the left cornet. Multiply the number continuously by 2 and extract the integer part as the binary digit. You can never get the exact binary representation. Stop when you have 6 bits. 0.4 0.8 1.6 1.2 0.4 0.8 1.6 0. 0 1 1 0 0 1 35

Normalization Example of normalization Original Number ------------ +1010001.1101 111.000011 +0.00000111001 0.001110011 Move ------------ 6 2 6 3 Normalized ------------ +2 6 x 1.01000111001 2 2 x 1.11000011 +2 6 x 1.11001 2 3 x 1.110011 Sign, exponent, and mantissa 36

Figure 3-8 IEEE standards for floating-point representation 37

Example 19 Show the representation of the normalized number + 2 6 x 1.01000111001 Solution The sign is positive. The Excess_127 representation of the exponent is 133. You add extra 0s on the right to make it 23 bits. The number in memory is stored as: 0 10000101 01000111001000000000000 000000000000 38

Number ------------ -2 2 x 1.11000011 +2-6 x 1.11001-2 -3 x 1.110011 Sign ---- 1 0 1 Exponent ----------- 10000001 01111001 01111100 Mantissa ------------------------------- 11000011000000000000000 11001000000000000000000 11001100000000000000000 Example of floating-point representation 39

Example 20 Interpret the following 32-bit floating-point number 1 01111100 11001100000000000000000 Solution The sign is negative. The exponent is 33 (124 127). The number after normalization is -2-3 x 1.110011 40

Limitations in 32-bit Integer and Floating Point Numbers Limited range of values (e.g. integers only from 2 31 to 2 31 1) Limited resolution for real numbers. E.g., if x is a machine representable value, the next value is x + ε (for some small ε). There is no value in between. This causes floating point errors in calculation. The accuracy of a single precision floating point number is about 6 decimal places. 41

Limitations of Single Precision Numbers Given the representation of the single precision floating point number format, what is the largest magnitude possible? What is the smallest number possible? With floating point number, it can happen that 1 + ε = 1. What is that largest ε? 42

Floating Point Rounding Error Consider 4-bit mantissa floating point addition: 1.010 2 2 + 1.101 2-1 1.010000 2 2 + 0.001101 2 2 1.011101 2 2 1.100 2 2 Shift exponent to that of the large number Round to 4 bits In decimal, it means 5.0 + 0.8125 6.0 43

Representation of Characters, the ASCII code 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI 1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 2 SP! " # $ % & ' ( ) * +, -. / 3 0 1 2 3 4 5 6 7 8 9 : ; < = >? 4 @ A B C D E F G H I J K L M N O 5 P Q R S T U V W X Y Z [ \ ] ^ _ 6 ` a b c d e f g h I j k l m n o 7 p q r s t u v w x y z { } ~ DEL How to read the table: the top line specifies the last digit in hexadecimal, the leftmost column specifies the higher value digit. E.g., at location 41 16 (=0100 0001 2 =65 10 ) is the letter A. 44

Base-16 Number, or Hexadecimal binary hexadecimal decimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 binary hexadecimal decimal 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 Instead of writing out strings of 0 s and 1 s, it is easier to read if we group them in group of 4 bits. A four bit numbers can vary from 0 to 15, we denote them by 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. 45

Program as Numbers High level programming language C = A + B; Assembly language add $5, $10, $3 Machine code (MIPS computer) bit 31 $10 $3 $5 add 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 bit 0 Not used source 1 source 2 destination not used operation 46

Graphics as Numbers A picture like this is also represented on computer by bits. If you magnify the picture greatly, you ll see square pixels which is either black or white. These can be represented as binary 1 s and 0 s. Color can also be presented with numbers, if we allow more bits per pixel. 47

Music as Numbers MP3 format CD music is sampled 44,100 times per second (44.1 khertz), each sample is 2 bytes long The digital music signals are compressed, at a rate of 10 to 1 or more, into MP3 format The compact MP3 file can be played back on computer or MP3 players 48

Some other points Computer Science starts counting from 0 (why?) We have to perform operations in a finite space, unlike what we have done when we counted with real numbers, which were infinite imagine a world in which we are born, grow older by one year, become 1, 2, 3,,62, 63 then again 0. Say, we decide we will not grow older beyond 64 strange but computer does similar things! A computer counting our age will count like 0, 1, 2, 3,, 62, 63, 0,! This is called modular arithmetic and gives lot of interesting results. Can you tell me from the above count values, some information of our computer? 49

Modulo Arithmetic Consider the set of number {0,,7} 0 Suppose all arithmetic operations were finished by taking the result modulus 8 3 + 6 = 9, 9 mod 8 = 1 6 7 1 2 3 + 6 = 1 3*5 = 15, 15 mod 8 = 7 5 3 3*5 = 7 4 50

Modulo Arithmetic: Computing in a finite world What is the additive inverse of 7 in modulo arithmetics? 7 + x = 0 7 + 1 = 0 7 0 1 0 and 4 are their own additive inverses Does each number also have a multiplicative inverse? 6 2 7 x 7 = 1 Does each number has a multiplicative inverse? 5 3 What if m=11? Now does each number have a multiplicative inverse? 4 51

Summary All information in computer are represented by bits. These bits encode information. It s meaning has to be interpreted in a specific way. We ve learnt how to represent unsigned integer, negative integer, floating pointer number, as well as ASCII characters. Computers have to compute in a finite world. 52