COMP Overview of Tutorial #2

Similar documents
CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

MACHINE LEVEL REPRESENTATION OF DATA

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

Number representations

FLOATING POINT NUMBERS

Numerical Representations On The Computer: Negative And Rational Numbers

CS & IT Conversions. Magnitude 10,000 1,

Number System. Introduction. Decimal Numbers

COMP2611: Computer Organization. Data Representation

CHW 261: Logic Design

Numerical Representations On The Computer: Negative And Rational Numbers

Module 2: Computer Arithmetic

COE 202- Digital Logic. Number Systems II. Dr. Abdulaziz Y. Barnawi COE Department KFUPM. January 23, Abdulaziz Barnawi. COE 202 Logic Design

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Introduction to Computers and Programming. Numeric Values

Introduction to Computer Science-103. Midterm

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

Inf2C - Computer Systems Lecture 2 Data Representation

BINARY SYSTEM. Binary system is used in digital systems because it is:

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

Number Systems CHAPTER Positional Number Systems

Signed umbers. Sign/Magnitude otation

CS 101: Computer Programming and Utilization

World Inside a Computer is Binary

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

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

A Level Computing. Contents. For the Exam:

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

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

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

Chapter 1 Review of Number Systems

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

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

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

Digital Fundamentals

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Chapter 3: Arithmetic for Computers

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.

Chapter 4. Operations on Data

Octal & Hexadecimal Number Systems. Digital Electronics

Chapter 2: Number Systems

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


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

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

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Number Systems. Both numbers are positive

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Organisasi Sistem Komputer

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

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

COE 202: Digital Logic Design Number Systems Part 2. Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:

Final Labs and Tutors

Level ISA3: Information Representation

Divide: Paper & Pencil

Lecture 2: Number Systems

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

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

Number Systems. TA: Mamun. References: Lecture notes of Introduction to Information Technologies (ITEC 1011) by Dr Scott MacKenzie

Internal Data Representation

Chapter 2 Data Representations

D I G I T A L C I R C U I T S E E

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Signed Binary Numbers

Integers and Floating Point

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

Moodle WILLINGDON COLLEGE SANGLI. ELECTRONICS (B. Sc.-I) Introduction to Number System

Data Representations & Arithmetic Operations

MC1601 Computer Organization

Chapter 2. Data Representation in Computer Systems

SIGNED AND UNSIGNED SYSTEMS

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

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

UNIT-III COMPUTER ARTHIMETIC

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

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

ITEC 1011 Introduction to Information Technologies

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

Number Systems (2.1.1)

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

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

CO212 Lecture 10: Arithmetic & Logical Unit

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

Octal and Hexadecimal Integers

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

Chapter 1. Digital Systems and Binary Numbers

Numbering systems. Dr Abu Arqoub

Number Systems and Number Representation

unused unused unused unused unused unused

COMP2121: Microprocessors and Interfacing. Number Systems

Representation of Non Negative Integers

Arithmetic and Bitwise Operations on Binary Data

Korea University of Technology and Education

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

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Number Systems and Computer Arithmetic

Transcription:

COMP 1402 Winter 2008 Tutorial #2 Overview of Tutorial #2 Number representation basics Binary conversions Octal conversions Hexadecimal conversions Signed numbers (signed magnitude, one s and two s complement, Excess-M) Float conversions 1

Number representation basics In binary each digit (or bit) has 2 possible values: 0 or 1. Ex: (10010101) 2 In octal each digit has 8 possible values: 0, 1, 2 7. Ex: (4127) 8 In hexadecimal (or hex for short) each digit has 16 possible values: 0, 1, 2 9, then A, B, C, D, E and F (representing 10, 11, 12, 13,14 and 15 in decimal respectively). Ex: (1AF6) 16 or 1AF6h Arbitrary base b: Bases d n d n-1 d n-2... d 2 d 1 d 0 To calculate the value: d n *b n + d n-1 *b n-1 +...+ d 1 *b 1 + d 0 *b 0 Decimals (base 10): How do we interpret the number 2510? = 2*10 3 + 5*10 2 + 1*10 1 + 0*10 0 2

Bases (2) Given a number d n d n-1 d n-2... d 2 d 1 d 0 In binary (base 2) value = d n *2 n + d n-1 *2 n-1 +...+ d 1 *2 1 + d 0 *2 0 In octal (base 8) value = d n *8 n + d n-1 *8 n-1 +...+ d 1 *8 1 + d 0 *8 0 In hexadecimal (base 16) value = d n *16 n + d n-1 *16 n-1 +...+ d 1 *16 1 + d 0 *16 0 Fractions in bases Arbitrary base fractions: 0. d -1 d -2 d -3 value = d -1 *b -1 + d -2 *b -2 + d -3 *b -3 In decimal (base 10): 0.512 is equal to 5*10-1 + 1*10-2 + 2*10-3 In binary (base 2): 0.1011 is equal to 1*2-1 + 0*2-2 + 1*2-3 + 1*2-4 = 1*0.5 + 0*0.25 + 1*0.125 + 1*0.0625 = 0.6875 3

Binary: Most/Least Significant bits Most significant bit (MSB) is the leftmost bit: Ex: 10010100 It is the bit of highest value (128 in the example above) but can also be used as the sign of the number (as we ll see later). Least significant bit (LSB) is the rightmost bit: Ex: 10010100 Has the least value of all the bits (0 or 1). Binary to decimal To convert binary to decimal we must add the digits weighed by exponents of 2 used in the binary number as seen previously. value = d n *2 n + d n-1 *2 n-1 +...+ d 1 *2 1 + d 0 *2 0 Ex: convert (11010) 2 to decimal. This is equal to 1*2 4 +1*2 3 + 0*2 2 + 1*2 1 +0*2 0 16 + 8 + 0 + 2 + 0 = 26 (11010) 2 = (26) 10 4

Decimal to binary Converting a number from base 10 to base 2. Let s look again at the value of a binary number: d n *2 n + d n-1 *2 n-1 +...+ d 1 *2 1 + d 0 *2 0 We need to fill in the d 0 to d n to build the binary number as d n d n-1 d n-2... d 2 d 1 d 0 Different ways to solve this Start with the largest power of 2 in the decimal number, then move down (slow) Algorithm using the mod approach (easier) Decimal to binary (2) Decimal to binary algorithm: Q = decimal number While Q is not equal to 0 do the following: Binary digit = Q mod 2 Q = Q / 2 (quotient) End While Let s try an example 5

Decimal to binary (3) Example: convert (134) 10 to binary. 134 mod 2 = 0 67 mod 2 = 1 33 mod 2 = 1 16 mod 2 = 0 8 mod 2 = 0 4 mod 2 = 0 2 mod 2 = 0 1 mod 2 = 1 We then read the numbers from bottom up: 10000110 So (134) 10 = (10000110) 2 Decimal fraction to binary Convert both sides of the period separately. We ve seen how to do the left side, now the fraction side. Instead of mod, we multiply by two (*2). When we go over 1.0, subtract 1 for next round. Ex: Convert (0.21875) 10 to binary. 0.21875 * 2 = 0.4375 0.4375 * 2 = 0.875 0.875 * 2 = 1.75 0.75 * 2 = 1.5 0.5 * 2 = 1.0 (stop at 1.0) Then we read the numbers top to bottom: 00111 Therefore (0.21875) 10 = 0.00111 6

Octal to binary Since every octal digit can take 8 values we can convert each digit to binary using 3 bits. Ex: convert (7213) 8 to binary. We ll convert each digit separately: Octal: 7 2 1 3 Binary: 111 010 001 011 Therefore: (7213) 8 = (111010001011) 2 Binary to octal Group the binary number into groups of 3 bits, starting from the right, then convert each group into their octal value. Ex: convert (11010110011101) 2 to octal. 11 010 110 011 101 3 2 6 3 5 Therefore: (11010110011101) 2 = (32635) 8 7

Hexadecimal to binary Since every digit of hex has 16 possible values, we represent each digit using 4 binary bits. Ex: convert (5DE9) 16 to binary. convert each digit into four binary bits: 5 D E 9 0101 1101 1110 1001 Therefore (5DE9) 16 = (0101110111101001) 2 Binary to hexadecimal Group the binary number into groups of 4 bits, starting from the right, then convert each group into their hex value. Ex: (10010010111110111) 2 to hex. 1 0010 0101 1111 0111 1 2 5 F 7 Therefore (10010010111110111) 2 = (125F7) 16 8

Octal to decimal Count the digits weighed by exponents of 8, as seen previously: value = d n *8 n + d n-1 *8 n-1 +...+ d 1 *8 1 + d 0 *8 0 Ex: convert (314) 8 to decimal. = 3*8 2 + 1*8 1 + 4*8 0 = 3*64 + 1*8 + 4*1 = 192 + 8 + 4 (314) 8 = (204) 10 Hexadecimal to decimal Count the digits weighed by exponents of 16, as seen previously: value = d n *16 n + d n-1 *16 n-1 +...+ d 1 *16 1 + d 0 *16 0 Ex: convert (4A1C) 16 to decimal. = 4*16 3 + 10*16 2 + 1*16 1 + 12*16 0 = 4*4096 + 10*256 + 1*16 + 12*1 = 16384 + 2560 + 16 + 12 (4A1C) 16 = 18972 9

Part I of the exercises then correction Signed Magnitude One s Complement Two s Complement Excess-M (Bias) Signed Numbers 10

Signed Magnitude MSB is 1 if the number is negative (-), 0 if the number is positive (+). The rest of the number is converted to binary like we ve seen before. What happens with numbers smaller than -127? overflow Example: (-14) 10 : 14 in binary is: 00001110-14 in signed magnitude is: 10001110 One s complement MSB is the sign bit 1 is negative 0 is positive Rules to change sign: Flip all the bits (change 0 s to 1 s and 1 s to 0 s) Two zeroes: 00000000 and 11111111 Harder to know the value of a negative number (have to be complemented first) 11

One s Complement (decimal to binary) First, convert the decimal to binary If the number is negative, flip every bit (0 become 1, 1 becomes 0). Positive numbers are converted to binary without change. Example: (-44) 10 in one s complement 44 in binary is: 00101100-44 in one s complement is: 11010011 Example: (38) 10 in one s complement 38 in binary is: 00100110 38 in one s complement is: 00100110 (same) One s Complement (binary to decimal) If the MSB is 1, we must flip every bit before converting to decimal. Ex: convert (10011100) 2 from one s complement to decimal. Flip the bits since M.S.B. is a 1 : 01100011 Then find the decimal value: 64+32+2+1 = 99 and we add the sign= (-99) 10 If the number is positive (MSB is 0 ) we convert to decimal without any other changes. 12

Two s complement MSB is the sign bit 1 is negative 0 is positive Rules to change sign: Take one s complement, then add 1. Only one zero: 00000000 Have to take the two s complement of negatives in order to know the value. Two s Complement (decimal to binary) Convert the decimal to binary, then to one s complement, then add 1. Ex: convert (-57) 10 to two s complement. 57 in binary is: 00111001 in one s complement: 11000110 add 1: + 1 in two s complement: 11000111 13

Two s Complement (binary to decimal) If the MSB is 1, take one s complement (flip every bit) then add 1. Ex: convert (11001100) 2 from two s complement to decimal. Take one s complement : 00110011 add 1: + 1 00110100 and convert to decimal = 32+16+4 = 52 so the answer is (-52) 10 Excess-M (Bias) 8-bit version has 256 values (-127 to +128). Used to store the exponent in the float rep. For floating point, we ll use Excess-127. Ex: Convert 32 to Excess-127 = 32 + 127 = 159 or 10011111 To convert Excess-127 back to decimal: convert to decimal then subtract 127. Ex: Convert 11001010 in Excess-127 to decimal 11001110 = 206 206 127 = (79) 10 14

Addition in One s complement First convert both decimals in one s complement then add. If there is a carry, add it to the right. Ex: add (-15) 10 and (+69) 10 in one s complement. -15 in one s complement: 11110000 +69 in one s complement: 01000101 add: 100110101 add carry to right: 1 answer: 00110110 or +54 Addition in Two s complement First convert both decimals in two s complement, then add. If there is a carry, discard it. Ex: add (-22) 10 and (19) 10 in two s complement -22 in two s complement: 11101010 19 in two s complement: 00010011 add: 11111101 Since MSB is 1, take two s complement to get the answer: 00000011 or -3 15

Float representation Used to represent fractional numbers Overall sign Mantissa (fraction part) Exponent Base In C: 32-bit float (4 bytes) Sign Exponent Mantissa 1 bit 8 bits 23 bits Float to decimal Convert 1 00000011 11000000000000000000000 from float (1-bit sign, 8-bit exponent and 23-bit mantissa) to decimal. Sign: - since the bit sign is 1 Exponent: 00000011 to decimal = 3, then subtract 127 to convert from Excess-127 to true value: 3-127 = -124. Mantissa: 11 followed by zeroes. We supply the leading 1 (always). So the number is -(1.11) 2 * 2-124 = -1.75 * 2-124 16

Decimal to float Convert (3.6875) 10 to float (1-bit sign, 8-bit exponent and 23-bit mantissa). Sign: 0 (since the number is positive) To figure out the mantissa and the exponent, we must first convert the decimal to binary. 3 to binary = 11.6875 to binary: 0.6875 * 2 = 1.375 0.375 * 2 = 0.75 0.75 * 2 = 1.5 0.5 * 2 = 1.0 So 3.6875 in binary is 11.1011 (continued next slide) Decimal to float (2) Floats must always be in the format 1.x so we must move the period to have 1.x in our binary number: 11.1011 (we have to shift the period one place to the left). So 11.1011 = 1.11011 * 2 1 We now have the mantissa: 11011(followed by zeroes) And the exponent is 1, converted to Excess-127 = 128. In binary 128 = 10000000. Answer: 0 10000000 11011000000000000000000 Convert back to decimal to verify the answer. 17

Part II of the exercises then correction 18