CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.

Slides:



Advertisements
Similar presentations
Floating Point Numbers
Advertisements

COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Assembly Language and Computer Architecture Using C++ and Java
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
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
2-1 Computer Organization Part Fixed Point Numbers Using only two digits of precision for signed base 10 numbers, the range (interval between lowest.
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
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.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
Number Systems Lecture 02.
Binary Number Systems.
School of Computer Science G51CSA 1 Computer Arithmetic.
Binary Representation and Computer Arithmetic
The Binary Number System
Data Representation Number Systems.
CENG 311 Machine Representation/Numbers
Simple Data Type Representation and conversion of numbers
Numbers and number systems
Data Representation – Binary Numbers
Computer Organization and Architecture Computer Arithmetic Chapter 9.
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.
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
IT253: Computer Organization
Data Representation.
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.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
46 Number Systems Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs Display: Seven segment displays Inputs: Switches.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
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
Fractions in Binary.
69 Decimal (Base 10) Numbers n Positional system - each digit position has a value n 2534 = 2*1, * *10 + 4*1 n Alternate view: Digit position.
Binary Arithmetic.
Dr Mohamed Menacer College of Computer Science and Engineering Taibah University CE-321: Computer.
Data Representation: Floating Point for Real Numbers Computer Organization and Assembly Language: Module 11.
IT1004: Data Representation and Organization Negative number representation.
Numbers in Computers.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Lecture No. 4 Computer Logic Design. Negative Number Representation 3 Options –Sign-magnitude –One’s Complement –Two’s Complement  used in computers.
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.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Computer Representation of Information
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
William Stallings Computer Organization and Architecture 7th Edition
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSC 370 (Blum)
ECEG-3202 Computer Architecture and Organization
Presentation transcript:

CS1Q Computer Systems Lecture 2 Simon Gay

Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation of numbers in binary. unsigned integers (i.e. positive integers; this is probably revision) signed integers (i.e. positive and negative integers) fractions floating point numbers It’s important to understand the binary representation of unsigned and signed integers. We won’t be doing any work with floating point numbers, but it’s interesting to see some of the complexities.

Lecture 2CS1Q Computer Systems - Simon Gay3 Converting Binary to Decimal Converting binary numbers to decimal is easy: just add up the values of the columns which contain = = = 255

Lecture 2CS1Q Computer Systems - Simon Gay4 Converting Decimal to Binary Method 1 First work out how many bits are needed. If the number is at least but less than then bits are needed. Examples: 103 is at least 64 but less than 128; it needs 7 bits 32 is at least 32 but less than 64; it needs 6 bits 257 is at least 256 but less than 512; it needs 9 bits 1000 is at least 512 but less than 1024; it needs 10 bits

Lecture 2CS1Q Computer Systems - Simon Gay5 Converting Decimal to Binary Method 1 Work out the column values for the number of bits needed. Example: 103, using 7 bits Starting from the left, enter a 1 if the number is at least as big as the column value; otherwise 0. If 1 is entered, subtract the column value. Repeat for each column. More bits can be used: just put zeros in front

Lecture 2CS1Q Computer Systems - Simon Gay6 Converting Decimal to Binary Method 2 This method produces the binary digits from right to left. If the number is odd, enter 1 and subtract 1; if the number is even, enter 0. Divide the number by 2 and repeat. Example: Check: = 237.

Lecture 2CS1Q Computer Systems - Simon Gay7 Hexadecimal Hexadecimal, also known as hex, is base 16. Each digit represents a number from 0 to 15. The letters A to F (or a to f) are used for digits 10 to 15. 3C Each hex digit corresponds to 4 bits: FEDCBA

Lecture 2CS1Q Computer Systems - Simon Gay8 Hexadecimal A hex number is best thought of as an abbreviation for a binary number. The number of bits can easily be seen (4 times the number of hex digits) but the number itself is shorter. A35E

Lecture 2CS1Q Computer Systems - Simon Gay9 Addition in Binary Just like in decimal: work from right to left, carrying to the next column if necessary carry

Lecture 2CS1Q Computer Systems - Simon Gay10 Working Within a Word Size instead of 265 Usually a computer does addition on words of a fixed size. A carry beyond the leftmost column is an overflow, which might be detectable, and the result is calculated modulo 256 (with 8 bits) overflow 265 divided by 256 = 1 remainder 9 With 16 bit words: addition modulo 65536, etc.

Lecture 2CS1Q Computer Systems - Simon Gay11 Unsigned or Signed? Everything we have said so far applied to unsigned numbers: we are simply working with positive integers. If we want to work with both positive and negative integers then we need to be able to distinguish between them: we need signed numbers. We will now look at the representation of negative numbers in binary.

Lecture 2CS1Q Computer Systems - Simon Gay12 Negative Numbers in Binary We need a representation of negative numbers in binary. On paper we might write for but how do we represent the minus sign within a byte or word? The obvious idea is the sign-magnitude representation: means negative, 0 means positive (the sign) the normal representation of 22 (the magnitude)

Lecture 2CS1Q Computer Systems - Simon Gay13 Sign-Magnitude Doesn’t Work Unfortunately the sign-magnitude representation makes arithmetic difficult. Try in a 4 bit sign-magnitude representation: WRONG! Straightforward addition of signed numbers gives incorrect results. Another problem is that there are two representations of zero:

Lecture 2CS1Q Computer Systems - Simon Gay14 2s Complement Representation Positive numbers have the normal binary representation. To work out the representation of a negative number: invert each bit (exchange 0 and 1) add 1 to the result, ignoring overflow Example: 8 bit 2s complement representation of invert add 1

Lecture 2CS1Q Computer Systems - Simon Gay15 Facts about 2s Complement Normal addition works for both positive and negative numbers CORRECT! 1100 invert add invert add 1

Lecture 2CS1Q Computer Systems - Simon Gay16 Facts about 2s Complement There is no difference between positive zero and negative zero invert add 1, ignoring overflow The leftmost bit is still a sign bit: 0 for positive, 1 for negative. Whatever the word size, -1 is represented by a word full of 1s.

Lecture 2CS1Q Computer Systems - Simon Gay17 Facts about 2s Complement Half the bit patterns represent positive numbers, half negative. With 8 bits we get numbers from -128 to Here’s how it works with 3 bits: bit pattern meaning as unsigned number meaning as 2s complement number

Lecture 2CS1Q Computer Systems - Simon Gay18 Converting to and from binary When converting from decimal to binary it is important to know whether we are producing a signed or unsigned representation. This is usually obvious: if we are given a negative decimal number then we must use the signed (two’s complement) representation. When converting from binary to decimal it is important to know whether the given number is signed or unsigned as an unsigned binary number means 170 in decimal as a signed binary number means -86 in decimal. Some programming languages provide both signed and unsigned integer types, and confusion can result. (Example: C)

Lecture 2CS1Q Computer Systems - Simon Gay19 Real Numbers in Binary Real numbers (i.e. non-integers) can be represented in binary in the same way as in decimal: the column values keep halving as we move to the right …… 1/21/41/81/16 Example: = The familiar issues of decimal expansions also arise in binary: different numbers have expansions of different lengths, some have recurring or non-recurring infinite expansions, and so on. 210

Lecture 2CS1Q Computer Systems - Simon Gay20 Floating Point Numbers For computational purposes we need a fixed-size representation of real numbers. Fixing the number of digits before and after the binary point would be too inflexible, so we use floating point numbers. The basic idea is the same as standard scientific notation in decimal: 2.5  10 =  10 = but we use powers of 2 instead of powers of 10, and express everything in binary: 1.01  2 (binary) = 1.25  16 (decimal) = 20. mantissa or fractionexponent always

Lecture 2CS1Q Computer Systems - Simon Gay21 Floating Point Numbers A particular floating point format will use a fixed number of bits for the mantissa, a fixed number of bits for the exponent, and one extra bit to represent the sign (0 for positive, 1 for negative) of the overall number. Example: let’s use a 2 bit mantissa and a 3 bit exponent The 2 bit mantissa gives 4 possibilities: 00, 01, 10, 11 and we will interpret these as 0.00, 0.01, 0.10 and 0.11 (in binary), i.e. 0, 0.25, 0.5 and 0.75 (in decimal). The 3 bit exponent gives 8 possibilities and we will interpret these as -4 … +3.

Lecture 2CS1Q Computer Systems - Simon Gay22 Example Floating Point Format exponent /21/41/81/16 mantissa Points to note: the 32 combinations only give 18 different values the values are not evenly distributed

Lecture 2CS1Q Computer Systems - Simon Gay23 IEE Floating Point Format NaN  NaN NaN exponent mantissa The IEE floating point format avoids multiple representations, and represents some special values (NaN,  ) to help with error detection. The exponent is interpreted differently, and the interpretation of the mantissa depends on the value of the exponent. Here’s how it would look with a 2 bit mantissa and 3 bit exponent.

Lecture 2CS1Q Computer Systems - Simon Gay24 Floating Point Numbers However many bits we use for the mantissa and exponent (IEE single precision: 23 and 8; IEE double precision: 52 and 11) the following points are always true: Only a finite set of numbers is available, whereas in mathematical reality any range contains an infinite set of real numbers. A real number is represented by the nearest floating point number; usually this is only an approximation. Floating point arithmetic does not correspond exactly to mathematical reality: numbers of different sizes do not mix well. E.g. in the IEE example, = 12. Usually it is possible to be accurate enough for a given purpose, but:

Lecture 2CS1Q Computer Systems - Simon Gay25 Floating Point Mathematics Define the sequence by In any floating point format, no matter how many bits are used, the sequence converges to 100. In reality it converges to 6.