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

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

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

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Data Representation and Binary Arithmetic. Lecture 2

Fundamentals of Programming (C)

Chapter 2 Bits, Data Types, and Operations

Number Representations

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Number System

Chapter 2 Bits, Data Types, and Operations

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

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

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

Fundamentals of Programming

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

Number Systems Base r

Chapter 3. Information Representation

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

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.

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

Positional Number System

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

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

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

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

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

The Binary Number System

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

3.1. Unit 3. Binary Representation

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

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

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

Oberon Data Types. Matteo Corti. December 5, 2001

Simple Data Types in C. Alan L. Cox

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

Fundamental Data Types

UNIT 2 NUMBER SYSTEM AND PROGRAMMING LANGUAGES

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

ASSIGNMENT 5 TIPS AND TRICKS

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

Chapter 8. Characters and Strings

COMP2611: Computer Organization. Data Representation

EE 109 Unit 2. Binary Representation Systems

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

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

CSE 30 Fall 2013 Final Exam

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

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

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Source coding and compression

Do not start the test until instructed to do so!

CSE 30 Spring 2007 Final Exam

Lecture (09) x86 programming 8

Chapter 7. Binary, octal and hexadecimal numbers

CSE 30 Fall 2012 Final Exam

CSE 30 Fall 2007 Final Exam

Introduction to Computers and Programming. Numeric Values

Variables and data types

Imperial College London Department of Computing

CHW 261: Logic Design

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

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

Do not start the test until instructed to do so!

Number System. Introduction. Decimal Numbers

CSE 30 Winter 2014 Final Exam

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

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

MACHINE LEVEL REPRESENTATION OF DATA

CSE 30 Spring 2006 Final Exam

CSE 30 Winter 2009 Final Exam

Inf2C - Computer Systems Lecture 2 Data Representation

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

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

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

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 )

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

Do not start the test until instructed to do so!

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

Number Systems. Both numbers are positive

Electronic Data and Instructions

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Digital Fundamentals

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

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

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

M1 Computers and Data

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

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

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

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

CSE 30 Fall 2006 Final Exam

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

T02 Tutorial Slides for Week 2

Numeric Encodings Prof. James L. Frankel Harvard University

Chapter 3: Arithmetic for Computers

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

Transcription:

CPS 104 Computer Organization and Programming Lecture-2 : Data representations, Sep. 1, 1999 Dietolf Ramm http://www.cs.duke.edu/~dr/cps104.html CPS104 Lec2.1 GK&DR Fall 1999

Data Representation Computers store variables (data) Typically Numbers and Characters or composition of these We reason about numbers many different ways The key is to use a representation that is efficient CPS104 Lec2.2 GK&DR Fall 1999

Number Systems A number is a mathematical concept 10 Many ways to represent a number 10, ten, 2x5, X, 100/10, Symbols are used to create a representation Which representation is best for addition and subtraction? Which representation is best for multiplication and division? CPS104 Lec2.3 GK&DR Fall 1999

More Number Systems Humans use decimal (base 10) digits 0-9 are composed to make larger numbers 11 = 1*10 1 + 1*10 0 weighted positional notation Addition and Subtraction are straightforward carry and borrow (today called regrouping) Multiplication and Division less so can use logarithms and then do adds and subtracts CPS104 Lec2.4 GK&DR Fall 1999

Changing Base (Radix) Given 4 positions, what is largest number you can represent? CPS104 Lec2.5 GK&DR Fall 1999

Number Systems for Computers Today s computers are built from transistors Transistor is either off or on Need to represent numbers using only off and on two symbols off and on can represent the digits 0 and 1 A bit can have a value of 0 or 1 Binary representation weighted positional notation using base 2 11 10 = 1*2 3 + 1*2 1 + 1*2 0 = 1011 2 11 10 = 8 + 2 + 1 What is largest number, given 4 bits? CPS104 Lec2.6 GK&DR Fall 1999

Conversion from Decimal to Binary N is a positive Integer (in decimal representation) b i i=0,...,k are the bits (binary digits) for the binary representation of N N = b k *2 k + +b 2 *2 2 + b 1 *2 + b 0 binary representation: b k b 3 b 2 b 1 b 0 How do I compute b 0? Compute binary representation of 11? CPS104 Lec2.7 GK&DR Fall 1999

Conversion from Decimal i=0; while ( N > 0 ) { b i = N % 2; // b i = remainder; N mod 2 N = N / 2; // N becomes quotient of division i++; } Replace 2 by A and you have an algorithm that computes the base A representation for N CPS104 Lec2.8 GK&DR Fall 1999

Powers of 2 N 2 n Binary 0 1 00000000001 1 2 00000000010 2 4 00000000100 3 8 00000001000 4 16 00000010000 5 32 00000100000 6 64 00001000000 7 128 00010000000 8 256 00100000000 9 512 01000000000 10 1024 (1K) 10000000000 CPS104 Lec2.9 GK&DR Fall 1999

Binary, Octal and Hexidecimal numbers Computers can input and output decimal numbers but they convert them to internal binary representation. Binary is good for computers, hard for us to read Use numbers easily computed from binary Binary numbers use only two different digits: {0,1} Example: 1200 10 = 0000010010110000 2 Octal numbers use 8 digits: {0-7} Example: 1200 10 = 04260 8 Hexidecimal numbers use 16 digits: {0-9, A-F} Example: 1200 10 = 04B0 16 = 0x04B0 does not distinguish between upper and lower case CPS104 Lec2.10 GK&DR Fall 1999

Binary and Octal Easy to convert Binary numbers To/From Octal. Group the binary digits in groups of three bits and convert each group to an Octal digit. 2 3 = 8 Bin. Oct. 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 Example: 11 000 010 011 001 110 100 111 101 010 101 2 3 0 2 3 1 6 4 7 5 2 5 8 CPS104 Lec2.11 GK&DR Fall 1999

Binary and Hex To convert to and from hex: group binary digits in groups of four and convert according to table 2 4 = 16 Hex Bin Hex Bin 0 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111 Example: 1100 0010 0110 0111 0100 1111 1101 0101 2 C 2 6 7 4 F D 5 16 CPS104 Lec2.12 GK&DR Fall 1999

Issues for Binary Representation Complexity of arithmetic operations Negative numbers Maximum representable number CPS104 Lec2.13 GK&DR Fall 1999

Binary Integers Unsigned Integers: i = 100101 2 ; i = 1*2 5 + 0*2 4 + 0*2 3 + 1*2 2 + 0*2 1 + 1*2 0 4 bits => max number is 15 What about representing negative numbers? CPS104 Lec2.14 GK&DR Fall 1999

Sign-Magnitude Representation for Integers Add a sign bit Example: 010110 2 = 22 10 ; 110110 2 = -22 10 Advantages: Simple extension of unsigned numbers. Same number of positive and negative numbers. Disadvantages: Two representations for 0: 0=000000; -0=100000. Algorithm (circuit) for addition depends on the arguments signs. CPS104 Lec2.15 GK&DR Fall 1999

2 s Complement Representation for Integers Key idea is to use largest positive binary numbers to represent negative numbers Obtain negative number by subtracting large constant i = -a n-1 *2 n-1 + a n-2 *2 n-2 +... a 0 *2 0 6-bit examples: 010110 2 = 22 10 ; 101010 2 = -22 10 0 10 = 000000 2 ; 1 10 = 000001 2 ; -1 10 = 111111 2 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000-8 1001-7 1010-6 1011-5 1100-4 1101-3 1110-2 1111-1 CPS104 Lec2.16 GK&DR Fall 1999

2 s Complement Advantages: Only one representation for 0: 0 = 000000 Addition algorithm independent of sign bits. Disadvantage: One more negative number than positive : Example: 6-bit 2 s complement number. 100000 2 = -32 10 ; but 32 10 could not be represented CPS104 Lec2.17 GK&DR Fall 1999

2 s Complement Negation and Addition To negate a number do: Step 1. complement the digits Step 2. add 1 Examples 14 10 = 001110 2 ~ 110001 2 + 1-14 10 = 110010 2 To add signed numbers use regular addition but disregard carry out Example 18 10-14 10 = 18 10 + (-14 10 ) = 4 10 010010 2 +110010 2 000100 2 CPS104 Lec2.18 GK&DR Fall 1999

2 s Complement (cont.) Example: A = 0x0ABC; B = 0x0FEB. Compute: A + B and A - B in 16-bit 2 s complement arithmetic. CPS104 Lec2.19 GK&DR Fall 1999

2 s Complement Precision Extension Most computers today support 32-bit (int) or 64-bit integers 64-bit using gcc is long long 64-bit using Digital/Compaq compiler is long To extend precision do sign bit extension precision is number of bits used to represent a number Example 14 10 = 001110 2 in 6-bit representation. 14 10 = 000000001110 2 in 12-bit representation -14 10 = 110010 2 in 6-bit representation -14 10 = 111111110010 2 in 12-bit representation. CPS104 Lec2.20 GK&DR Fall 1999

What About Non-integer Numbers? There are infinitely many real numbers between two integers Many important numbers are real speed of light ~= 3x10 8 π = 3.1415 Fixed number of bits limits range of integers Can t represent some important numbers Humans use Scientific Notation 1.3x10 4 CPS104 Lec2.21 GK&DR Fall 1999

Floating Point Representation Numbers are represented by: X = (-1) s 2 E-127 1. M S := 1-bit field ; Sign bit E := 8-bit field; Exponent: Biased integer, 0 E 255. M:= 23-bit field; Mantissa: Normalized fraction with hidden 1 (don t actually store it) Single precision floating point number uses 32-bits for representation 31 30 8-bit s exp 22 23-bit Mantissa 0 CPS104 Lec2.22 GK&DR Fall 1999

Floating Point Representation The mantissa represents a fraction using binary notation: M =. s 1, s 2, s 3... = 1.0 + s 1 *2-1 + s 2 *2-2 + s 3 *2-3 +... Example: X = -0.75 10 in single precision (-(1/2 + 1/4)) -0.75 10 = -0.11 2 = (-1) x 1.1 2 x 2-1 = (-1) x 1.1 2 x 2 126-127 S = 1 ; Exp = 126 10 = 0111 1110 2 ; M = 100 0000 0000 0000 0000 0000 2 X = 31 30 23 22 1 0111 1110 100 0000 0000 0000 0000 0000 s E M 0 CPS104 Lec2.23 GK&DR Fall 1999

Floating Point Representation Example: What floating-point number is: 0xC1580000? CPS104 Lec2.24 GK&DR Fall 1999

Floating Point Representation Double Precision Floating point: 64-bit representation: 1-bit sign, 11-bit (biased) exponent; 52-bit mantissa (with hidden 1). X = (-1) s 2 E-1023 1. M Double precision floating point number S Exp Mantissa 1 11-bit 20 - bit 32 - bit CPS104 Lec2.25 GK&DR Fall 1999

ASCII Character Representation Oct. Chr. 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us 040 sp 041! 042 " 043 # 044 $ 045 % 046 & 047 ' 050 ( 051 ) 052 * 053 + 054, 055-056. 057 / 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077? 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w 170 x 171 y 172 z 173 { 174 175 } 176 ~ 177 del Each character is represented by a 7-bit ASCII code. It is packed into 8-bits CPS104 Lec2.26 GK&DR Fall 1999

Basic Data Types Bit: 0, 1 Bit String: sequence of bits of a particular length 4 bits is a nibble 8 bits is a byte 16 bits is a half-word 32 bits is a word 64 bits is a double-word Character: ASCII 7 bit code Decimal: (BCD code) digits 0-9 encoded as 0000 thru 1001 two decimal digits packed per 8 bit byte Integers: 2's Complement (32-bit representation). Floating Point: Single Precision (32-bit representation). Double Precision (64-bit representation). Extended Precision(128 -bit representation). M x R E mantissa exponent base How many +/- #'s? Where is decimal pt? How are +/- exponents represented? CPS104 Lec2.27 GK&DR Fall 1999

Summary Computers operate on binary numbers (0s and 1s) Conversion to/from binary, oct, hex Signed binary numbers 2 s complement arithmetic, negation Floating point representation hidden 1 biased exponent single precision, double precision sign magnitude CPS104 Lec2.28 GK&DR Fall 1999

Next Time Memory Pointers Arrays Strings Bitwise operations Reading Start Chapter 3 CPS104 Lec2.29 GK&DR Fall 1999