Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

Similar documents
A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

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,...

COMP2611: Computer Organization. Data Representation

Number System. Introduction. Decimal Numbers

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

Inf2C - Computer Systems Lecture 2 Data Representation

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

MACHINE LEVEL REPRESENTATION OF DATA

Signed umbers. Sign/Magnitude otation

Numeric Encodings Prof. James L. Frankel Harvard University

Module 2: Computer Arithmetic

CO212 Lecture 10: Arithmetic & Logical Unit

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Signed Binary Numbers

Chapter 1 Review of Number Systems

Representation of Non Negative Integers

Chapter 4: Data Representations

COMP Overview of Tutorial #2

Floating Point Arithmetic

Data Representation 1

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

CHAPTER 5: Representing Numerical Data

Representation of Numbers

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

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

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

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

CS & IT Conversions. Magnitude 10,000 1,

Bits, Words, and Integers

Introduction to Computers and Programming. Numeric Values

Chapter 3: Arithmetic for Computers

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

CS 101: Computer Programming and Utilization

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

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

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Chapter 2 Bits, Data Types, and Operations

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

Chapter 2. Data Representation in Computer Systems

CS 261 Fall Mike Lam, Professor Integer Encodings

Internal Data Representation

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Chapter Three. Arithmetic

Computer Organization

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

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

Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997

Floating Point Numbers. Lecture 9 CAP

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Integers and Floating Point

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

Chapter 2 Bits, Data Types, and Operations

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Chapter 2: Number Systems

Number Systems. Both numbers are positive

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

Chapter 2 Data Representations

Chapter 4. Operations on Data

CHW 261: Logic Design

Logic, Words, and Integers

World Inside a Computer is Binary

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

More about Binary 9/6/2016

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Advanced Computer Architecture-CS501

Arithmetic Processing

Organisasi Sistem Komputer

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

Data Representations & Arithmetic Operations

Number representations

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

Chapter 2 Bits, Data Types, and Operations

unused unused unused unused unused unused

CHAPTER 2 Data Representation in Computer Systems

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

CHAPTER 2 Data Representation in Computer Systems

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

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

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

IEEE Standard for Floating-Point Arithmetic: 754

Chapter 2 Bits, Data Types, and Operations

Chapter 1. Digital Systems and Binary Numbers

INF2270 Spring Philipp Häfliger. Lecture 4: Signed Binaries and Arithmetic

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.

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

Chapter 10 - Computer Arithmetic

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

Floating Point January 24, 2008

Number Systems and Computer Arithmetic

Lecture 2: Number Representa2on

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

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

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

Lesson #1. Computer Systems and Program Development. 1. Computer Systems and Program Development - Copyright Denis Hamelin - Ryerson University

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

Transcription:

Integers So far, we've seen how to convert numbers between bases. How do we represent particular kinds of data in a certain (32-bit) architecture? We will consider integers floating point characters What do we know about integers? They can be represented in binary using the digits (bits) 0 or 1 The value N can be represented using K bits. K = ceil (lg N) Suppose we have 32 bits. Then our numbers will be of the form i = 31 N = sum (b i * 2 i ) where b i = 0 or 1 i = 0 This is called unsigned binary representation.

Integers: unsigned binary How many possible values are there for 32-bit unsigned binary representation? There are 2 32 different representations, so there are about 4 billion possible values Why 4 billion? Why do we say possible values? Minimum value: 0 Maximum value: 2 32-1 (approximately 4 billion) In general, for N bits, the max value is 2 N - 1 Why -1, since there are 2 N representations? Think of array of size 2 N. What is the largest index? What is 2 N? 1 followed by N 0's (N+1 bits). (2 2 = 100, 2 3 = 1000, 2 4 = 10000, etc.) Why do we use unsigned values? Anything which is only positive or zero. Memory address Array index ASCII character What if we need to have negative values? Need different representation.

Integers: signed magnitude Signed magnitude Reserve one particular bit for the sign. Sign bit: If msb is 0, then positive value, if msb is 1, then negative value. Converting base 10 to N-bit signed magnitude (SM): 1. Ignoring sign, convert base 10 value to binary. 2. If less than (N-1) bits, pad rest of bits to (N-1) with 0. 3. If negative, set msb to 1. Converting N-bit SM to base 10: 1. Convert lower (N-1) bits to base 10. 2. If msb is 1, put minus sign in front of number. Example: Convert 3 in base 10 to 4-bit SM: binary value: 11 using 3 bits: 011 with sign bit: 0011 Convert -3 in base 10 to 4-bit SM: binary value: 11 using 3 bits: 011 with sign bit: 1011 What about 15? binary value: 1111 However, this is already 4 bits, and the sign bit needs to be 0 for positive value. Range of values for SM How many possible values (i.e., representations)? 2 N for N bits Maximum value 2 N-1-1 for (N-1) bits Minimum value -(2 N-1-1) for (N-1) bits

Total values, including 0: 2 *(2 N-1-1) + 1 = 2 N - 1 What happened to the extra value? There are actually 2 zeroes! positive zero: N 0's negative zero: 1 followed by (N-1) 0's So, total number of representations is 2 N, but total number of values is 1 less. Disadvantage of SM: 2 zeroes adds complexity to hardware. Addition It would be nice if we could use the same hardware for signed or unsigned addition. However, that is not the case for signed magnitude. Consider adding -1 and -1 in 4-bit SM 1001 + 1001 = 0010 in 4 bits, since the carry from the msb is lost. This gives a result of +2, which is incorrect. One way around this is to ignore the sign bit in doing the addition, then use the sign bit for the result. However, that doesn't work if the sign bits are different. The sign bit of the result will depend on the relative magnitudes. The magnitude of the result will be the difference in the magnitudes. For example, (-5) + 3 is -2, but 5 + (-3) is +2. Negation Negating a value means giving it the opposite sign. This is easy with SM, since we simply flip the sign bit. negative value: sign bit 1 --> 0 positive value: sign bit 0 --> 1

Integers: 1's complement Another way to represent negative values: Negate (flip) each bit. Use the operator ~ to represent flipping each bit. ~B is B with all of its bits flipped. This is called 1's complement (1C). Example: 11 ten is 01011 in 5-bit unsigned binary. Call this number B. B = 01011 -B = 10100 Converting from base 10 to N-bit 1C 1. Ignoring sign, convert value to unsigned N-bit value. 2. If sign is negative, negate (flip) all bits. 1C to base 10 1. If sign bit (msb) is 1, flip all bits. 2. Convert N-bit unsigned value to base 10, using negative value if sign bit was 1. Range of values Number of positive values (including +0): 2 N-1 Number of negative values (including -0): 2 N-1 Total number of different values: 2 N - 1 Negation To negate a number, flip all bits. Problems 2 zeroes, as in signed magnitude addition is complicated, but easier than in signed magnitude Since it has the same problems as SM, why look at 1C? Because it leads to a better representation.

Integers: 2's complement 2's complement (2C) may not be intuitively obvious, but it has nice properties Negation 1. flip all bits 2. add 1 (ignoring any carry out of the msb) Example: 11 ten is 01011 in 5-bit unsigned binary. Call this number B. 1. flip bits: 10100 ~B 2. add 1: + 1 10101 -B What do we get if we add this to the original number? 01011 B + 10101 + -B 00000 0 If we negate B (-B) using 2C and negate again (--B) we get the original value B back. 1. flip bits: 01010 ~(-B) 2. add 1: + 1 01011 B Other ways to do N-bit 2C negation: 1. Find the rightmost 1 bit, then flip all the higher bits (bits to the left). Example: 01011 Rightmost 1 bit is b 0 flip bits to the left: 10101 Why does this work? Consider N-bit value value: B = b N-1 b N-2... 1 0... 0 where b k is 1 and all b i are 0 for i < k If we create the negation -B by flipping the bits to the left of b k, then -B = ~b N-1 ~b N-2... 1 0... 0 What happens when we add these 2 numbers? For each b i, i < k, the sum of the bits is 0 + 0 = 0

For bit k, the sum of the bits is 1 + 1 = 10, so the result is 0 with carry 1 Now, for i > k, each bit is either 0 or 1 in B and the opposite in -B So, the result is 1 + 0 + carry bit 1 for a result of 0 and a carry 1 For the leftmost bit position N-1, the result is 0, and the final carry is ignored. Therefore, the result consists of all 0's, which must be true for B + (-B) 2. Subtract B from value: 1 followed by N 0's. Example: 01011 subtract from 100000: 100000-01011 10101 Odometer (Ferris Bueller) view: Ferris Bueller's Day Off Consider a 4-digit decimal odometer. What is the largest milage it can show? 9999 What happens if we go 1 more mile? 0000 Now, think of starting at 0000 and going backward, like Ferris: Go back 1 mile, and the odometer reads 9999, 2 miles 9998, and so forth. N-bit 2's complement is really like a binary odometer: Go forward from 00... 0 for positive numbers. Go backward for negative numbers. What's really going on here mathematically? What is the value of 1 followed by N 0's? 2 N So, N-bit 2C is really based on arithmetic modulo 2 N This is why 2C has nice properties for doing arithmetic. Advantages of 2C representation: Only 1 zero Add using same hardware as unsigned binary Subtract by taking complement and adding. Why is there only 1 zero? Consider the 5-bit positive zero 00000 1. flip bits: 11111

2. add 1: + 1 00000 This is negative 0, but it has the same representation :) Range of values: Maximum positive number: 2 N-1-1 Minimum negative number: - (2 N-1 ) Total number of values (including 0): 2 N Converting from base 10 to N-bit 2C 1. Convert to N bits UB 2. If number has minus sign, negate: flip bits add 1 Converting from N-bit 2C to base 10 1. If msb is 1 (negative number), negate 2. Convert result (or original value) as UB to base 10 3. If number was negative, insert minus sign Conclusion: 2C rules! Most computer representations of signed integers use 2C.

Integers: excess/bias One disadvantage of 2C: Can't sort values just using the bit representation. Would look like negative numbers were greater than positive numbers. Another idea: Consider the unsigned values for a 3-bit representation representation value excess-4 000 0-4 001 1-3 010 2-2 011 3-1 100 4 0 101 5 1 110 6 2 111 7 3 Represent negative values, but keep the values in representation order First half of the representations for negative, second half for positive This is called excess, or biased, representation. Each value is shifted by a constant amount; in this case the bias is 4. Since there are 3 bits, the bias value is 2 (3-1) = 4 In general, for an N-bit representation, we will have half of the values negative and half of the values non-negative if the bias is 2 N-1. However, the bias could be any number < 2 N - 2 if the range of values includes both positive and negative numbers. Range of values: N-bit excess K Number of values: 2 N Minimum negative value: -K Maximum positive value: 2 N -1 - K Converting base 10 to excess K Add K to the number

Convert to unsigned binary Converting excess K to base 10 Convert unsigned binary value to base 10 Subtract K Disadvantage: Can't use unsigned binary hardware to do addition. Excess notation is used as part of floating point representation.

Integers: BCD Since people think in decimal, it may be convenient to use a decimal-like representation. Binary coded decimal (BCD) uses unsigned binary to represent each decimal digit. How many bits are needed? 4 bits per decimal digit: ceil (lg 10) = 4 Since 4 bits can represent 16 values, 6 values are unused for every digit. Advantages Decimal input/output doesn't require complicated conversion. Fewer problems with repeating fractions. Example: Represent 1739 ten in BCD: decimal 1 7 3 9 BCD 0001 0111 0011 1001 Some early computers used BCD representation, and many calculators still use BCD. Since calculators can be much slower than computers, they can use hardware which operates on 4 bits at a time. Negative values are represented using 10's complement (similar to 2C).

Integers: Sign extension We may need to change the number of bits used to represent a number. For example, C has int, short int, and long int to represent integers. The language doesn't specify the exact size of each form, but sizeof (short int) <= sizeof (int) <= sizeof (long int) How do we cast from one kind of int to a larger number of bits? ints are represented in N-bit 2C To convert a number from N bits to N + K bits in 2C notation, copy (extend) the sign bit from bit N-1 to bit N + K -1 If the value is positive, all the higher-order bits will become 0; if the value is negative, all the higher-order bits will become 1. All of the other bits (location N-2,..., 0) remain unchanged. Example: 3 ten is 0011 in 4 bits In 8 bits, it becomes 00000011 (the zero sign bit is extended to the left). -3 ten is 1101 in 4 bits In 8 bits, it becomes 11111101 (the one sign bit is extended to the left). Casting to a shorter representation May lose value 32-bit to 16-bit: upper 16 bits are chopped off Unsigned binary No sign bit, so extend with 0's

This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.