Data Representation. CMPE12cGabriel Hugh Elkaim 2 Data Representation Goal: Store numbers, characters, sets, database records in the computer. What we.

Slides:



Advertisements
Similar presentations
Floating Point Numbers. CMPE12cGabriel Hugh Elkaim 2 Floating Point Numbers Registers for real numbers usually contain 32 or 64 bits, allowing 2 32 or.
Advertisements

1 Data Representation Patt and Patel Ch. 2 & 9. 2 Data Representation Goal: Store numbers, characters, sets, database records in the computer.Goal: Store.
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Standard positional representation of numbers:

Introduction to Programming with Java, for Beginners
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Assembly Language and Computer Architecture Using C++ and Java
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Arithmetic and Logical Operations
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
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.
Number Systems Lecture 02.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Binary Representation and Computer Arithmetic
Chapter 5 Data representation.
CENG 311 Machine Representation/Numbers
Simple Data Type Representation and conversion of numbers
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Computer Science 111 Fundamentals of Programming I Number Systems.
IT253: Computer Organization
Computing Systems Basic arithmetic for computers.
Data Representation - Part I. Representing Numbers Choosing an appropriate representation is a critical decision a computer designer has to make The chosen.
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Number Systems Spring Semester 2013Programming and Data Structure1.
Digital Logic Design Lecture 3 Complements, Number Codes and Registers.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Data Representation in Computer Systems
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Data Representation Dr. Ahmed El-Bialy Dr. Sahar Fawzy.
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.
CSC 221 Computer Organization and Assembly Language
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
AEEE2031 Data Representation and Numbering Systems.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
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=
Arithmetic Operations
Data Representation. Goal: Store numbers, characters, sets, database records in the computer. What we got: Circuit that stores 2 voltages, one for logic.
CCE Department – Faculty of engineering - Islamic University of Lebanon Chapter 6 Binary Arithmetic.
Numbers in Computers.
Nguyen Le CS147.  2.4 Signed Integer Representation  – Signed Magnitude  – Complement Systems  – Unsigned Versus Signed Numbers.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Programming and Data Structure
Data Representation Binary Numbers Binary Addition
Chapter 3 Data Storage.
William Stallings Computer Organization and Architecture 7th Edition
Data Representation Data Types Complements Fixed Point Representation
ECEG-3202 Computer Architecture and Organization
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Presentation transcript:

Data Representation

CMPE12cGabriel Hugh Elkaim 2 Data Representation Goal: Store numbers, characters, sets, database records in the computer. What we got: Circuit that stores 2 voltages, one for logic 0 (0 volts) and one for logic 1 (ex: 3.3 volts). –DRAM – uses a single capacitor to store and a transistor to select. –SRAM – typically uses 6 transistors.

CMPE12cGabriel Hugh Elkaim 3 Definition: A unit of information. It is the amount of information needed to specify one of two equally likely choices. –Example: Flipping a coin has 2 possible outcomes, heads or tails. The amount of info needed to specify the outcome is 1 bit. Bit

CMPE12cGabriel Hugh Elkaim 4 Value Representation HTHT 0101 False True 0101 Value Representation 1e Use more bits for more items Three bits can represent 8 things: 000,001,…,111 N bits can represent 2 N things N bitsCan representWhich is approximately ,53665 thousand (64K where K=1024) 324,294,967,2964 billion …x billion billion Storing Information

CMPE12cGabriel Hugh Elkaim 5 Most computers today use: Type# of bitsName for storage unit Character8-16byte (ASCII) – 16b Unicode (Java) Integers32-64word (sometimes 8 or 16 bits) Reals32-64word or double word Storing Information

CMPE12cGabriel Hugh Elkaim 6 Memory location for a character usually contains 8 bits: to (binary) 0x00 to 0xff (hexadecimal) Which characters? A, B, C, …, Z, a, b, c, …, z, 0, 1, 2, …,9 Punctuation (,:{…) Special ( \ n \ O …) Which bit patterns for which characters? Want a standard!!! Want a standard to help sort strings of characters. Character Representation

CMPE12cGabriel Hugh Elkaim 7 ASCII (American Standard Code for Information Interchange) Defines what character is represented by each sequence of bits. Examples: is 0x41 (hex) or 65 (decimal). It represents “A” is 0x42 (hex) or 66 (decimal). It represents “B”. Different bit patterns are used for each different character that needs to be represented. Character Representation

CMPE12cGabriel Hugh Elkaim 8 ASCII has some nice properties. If the bit patterns are compared, (pretending they represent integers), then “A” < “B” 65 < 66 This is good because it helps with sorting things into alphabetical order. But…: ‘a’ (61 hex) is different than ‘A’ (41 hex) ‘8’ (38 hex) is different than the integer 8 ‘0’ is 30 (hex) or 48 (decimal) ‘9’ is 39 (hex) or 57 (decimal) ASCII Properties

CMPE12cGabriel Hugh Elkaim 9 Consider this program, what does it do if I enter a character from 0 to 9? getc $t1 # get a character add $a0, $t1, $t1# add char to itself li$v0, 11# putc code syscall# print character ASCII and Integer I/O

CMPE12cGabriel Hugh Elkaim 10 How to convert digits Need to take the “bias” out. # do a syscall to get a char, move to $t1 sub $t2, $t1, 48 # convert char to number add $t3, $t2, $t2 add $t3, $t3, 48 # convert back to character putc $t3 The subtract takes the “bias” out of the char representation. The add puts the “bias” back in. This will only work right if the result is a single digit. Needed is an algorithm for translating character strings to and from integer representation

CMPE12cGabriel Hugh Elkaim 11 Example: For ‘3’ ‘5’ ‘4’ Read ‘3’ translate ‘3’ to 3 Read ‘5’ translate ‘5’ to 5 integer = 3 x = 35 Read ‘4’ translate ‘4’ to 4 integer = 35 x = 354 Character string - > Integer

CMPE12cGabriel Hugh Elkaim 12 asciibias = 48 integer = 0 while there are more characters get character digit  character – asciibias integer  integer x 10 + digit Pseudo code for string to integer algorithm

CMPE12cGabriel Hugh Elkaim 13 Back to the 354 example: For 354, figure out how many characters there are For 354 div 100 gives 3 translate 3 to ‘3’ and print it out 354 mod 100 gives div 10 gives 5, translate 5 to ‘5’ and print it out, 54 mod 10 gives 4 4 div 1 gives 4 translate 4 to ‘4’ and print it out 4 mod 1 gives 0, so your done Integer - > Character string

CMPE12cGabriel Hugh Elkaim 14 Compare these two data declarations mystring:.asciiz “123” mynumber:.word 123 Character String / Integer Representation

CMPE12cGabriel Hugh Elkaim 15 The string “123” is: ‘1’=0x31= ‘2’=0x32= ‘3’=0x33= ‘ \ 0’=0x00= Which looks like this in memory: Basically a series of four ASCII characters Character String / Integer Representation

CMPE12cGabriel Hugh Elkaim 16 The integer 123 is a number 123 = 0x7b = 0x b = b Which looks like this in memory: This is a 32-bit, 2’s complement representation Character String / Integer Representation

CMPE12cGabriel Hugh Elkaim 17 Integer Representation Assume our representation has a fixed number of bits (n = 32) Which 4 billion integers do we want? –Infinite number of integers greater than 0 –Infinite number of integers less than 0 What bit patterns should be choose to represent each integer AND where the representation –Does not affect the result of calculation –Does dramatically affect the ease of calculation Convert to/from representation to human-readable form as needed.

CMPE12cGabriel Hugh Elkaim 18 Usual answers: 1.Represent 0 and consecutive positive integers Unsigned integers 2.Represent positive and negative integers Signed magnitude One’s complement Two’s complement Biased Unsigned and two’s complement the most common Integer Representation

CMPE12cGabriel Hugh Elkaim 19 Integer represented is binary value of bits: > 0, > 1, > 2, … Encodes only positive values and zero Range: 0 to 2 n –1, for n bits Unsigned Integers

CMPE12cGabriel Hugh Elkaim 20 Unsigned Integers If we have 4 bit numbers: To find range make n = 4. Thus 2 4 –1 is 15 Thus the values possible are 0 to 15 [0:15] = 16 different numbers 7 would be not represent able -3 not represent able For 32 bits: Range is 0 to = [0: 4,294,967,295] Which is 4,294,967,296 different numbers

CMPE12cGabriel Hugh Elkaim 21 Signed Magnitude Integers A human readable way of getting both positive and negative integers –i.e. 5 and –5 Not well suited for hardware implementation Is used for IEEE Floating Point representation

CMPE12cGabriel Hugh Elkaim 22 Representation: Use 1 bit of integer to represent the sign of the integer –Sign bit is msb: 0 is “+”, 1 is ”–” Rest of the integer is a magnitude, with same encoding as unsigned integers. To get the additive inverse of a number, just flip (invert, complement) the sign bit. Range: -(2 n-1 – 1) to 2 n-1 -1 Signed Magnitude Integers

CMPE12cGabriel Hugh Elkaim 23 Signed Magnitude - Example If 4 bits then range is: to 2 3 – 1 which is -7 to +7 Questions: 0101 is ? -3 is ? +12 is ? [-7,…, -1, 0, +1,…,+7] = = 15 < 16 = 2 4 Why? What problems does this cause?

CMPE12cGabriel Hugh Elkaim 24 One’s Complement Historically important (i.e.: not used today) Early computers built by Seymore Cray (while at CDC) were based on 1’s complement integers Positive integers are same as unsigned –0000=0 –0111=7 Negation is done by taking the bitwise complement of positive integer –each bit is flipped, 0  1,1  0 Sign bit is first bit (1 MSB is negative)

CMPE12cGabriel Hugh Elkaim 25 To get one’s complement of –1 –take +1:0001 –invert bits:1110 –don’t add or take away any bits Another example: 1100 –must be negative, invert bits to find out –0011 is +3 –So, 1100 in 1SC is? Properties of one’s complement –negative numbers have 1 in MSB –Two representation for zero (1111,0000) One’s Complement Representation

CMPE12cGabriel Hugh Elkaim 26 One’s Complement Examples

CMPE12cGabriel Hugh Elkaim 27 More 1SC Examples

CMPE12cGabriel Hugh Elkaim 28 Two’s Complement Variation of one’s complement that does not have two representations of zero This makes the hardware ALU much faster than other representations Negative values are “slipped” up by one, eliminating –0. How to get two’s complement integer –Positive are same as unsigned binary –Negative numbers: take positive number compute the one’s complement add 1

CMPE12cGabriel Hugh Elkaim 29 Two’s Complement Example, what is -5 in 2SC? 1. What is 5? Invert all the bits: 1010 (basically find the 1SC) 3. Add one: = 1011 which is -5 in 2SC To get the additive inverse of a 2’s complement integer 1. Take the 1’s complement 2. Add 1

CMPE12cGabriel Hugh Elkaim 30 Two’s Complement Example

CMPE12cGabriel Hugh Elkaim 31 More 2SC Examples

CMPE12cGabriel Hugh Elkaim 32 Number of integers represent able is -2 n-1 to 2 n-1 -1 So if 4 bits: [-8,…,-1,0,+1,…,+7] = = 16 = 2 4 numbers With 32 bits: [-2 31,…,-1,0,+1,…,( )] = ( ) = 2 32 numbers [ ,…,-1,0,+1,…, ] ~ 2 Billion Two’s Complement

CMPE12cGabriel Hugh Elkaim 33 A Little Bit on Adding Simple way of adding 1 Start at LSB working from left to right –while the bit is 1, change it to a 0 –when you get to a zero, change it to a 1 –stop Can be combined with bit inversion to make 2’s complement.

CMPE12cGabriel Hugh Elkaim 34 x0011 +y sum0100 More generally, it’s just like decimal!! = = = 2, which is 10 in binary, sum is 0, carry is = 3, sum is 1, carry is 1. A Little Bit on Adding

CMPE12cGabriel Hugh Elkaim 35 A Little Bit on Adding Carry inABSumCarry out

CMPE12cGabriel Hugh Elkaim 36 Biased An integer representation that skews the bit patterns so as to look just like unsigned but actually represent negative numbers. Example: 4-bit, with BIAS of 2 3 (or called Excess 8) True value to be represented 3 Add in the bias+8 Unsigned value 11 The bit pattern of 3 in biased-8 representation will be 1011

CMPE12cGabriel Hugh Elkaim 37 Suppose we were given a biased-8 representation, 0110, to find what the number represented was: Unsigned 0110 represents 6 Subtract out the bias-8 True value represented-2 Operations on the biased numbers can be unsigned arithmetic but represent both positive and negative values How do you add two biased numbers? Subtract? Biased Representation

CMPE12cGabriel Hugh Elkaim 38 Biased Representation in excess 100 is: 52 10,excess127 is: ,excess31 is: ,excess31 is: Exercises

CMPE12cGabriel Hugh Elkaim 39 Where is the sign “bit” in excess notation? Bias notation used in floating-point exponents. Choosing a bias: To get an ~ equal distribution of values above and below 0, the bias is usually 2 n-1 or 2 n-1 – 1. Range of bias numbers? Depends on bias, but contains 2 n different numbers. Biased Representation

CMPE12cGabriel Hugh Elkaim 40 Sign Extention How to change a number with a smaller number of bits into the same number (same representation) with larger number of bits This is done frequently by arithmetic units (ALU)

CMPE12cGabriel Hugh Elkaim 41 Unsigned representation: Copy the original integer into the LSBs, and put 0’s elsewhere. Thus for 5 bits to 8 bits: xxxxx  000xxxxx Sign Extension - unsigned

CMPE12cGabriel Hugh Elkaim 42 Signed magnitude: Copy the original integer’s magnitude into the LSBs & put the original sign into the MSB, put 0’s elsewhere. Thus for 6 bits to 8 bits sxxxxx  s00xxxxx Sign Extension – signed magnitude

CMPE12cGabriel Hugh Elkaim 43 Sign Extension – 1SC and 2SC 1’s and 2’s complement: 1.Copy the original n-1 bits into the LSBs 2.Take the MSB of the original and copy it elsewhere Thus for 6 bits to 8 bits: sxxxxx  sssxxxxx

CMPE12cGabriel Hugh Elkaim 44 In 2’s complement, the MSB (sign bit) is the –2 n-1 place. It says “subtract 2 n-1 from b n-2 …b 0 ”. Sign extending one bit Adds a –2 n place Changes the old sign bit to a +2 n-1 place -2 n + 2 n-1 = -2 n-1, so the number stays the same To make this less clear… Sign Extension – 2SC

CMPE12cGabriel Hugh Elkaim 45 Sign Extension What is -12 in 8-bit 2’s complement form?

CMPE12cGabriel Hugh Elkaim 46 Questions?