Number Representations

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

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

Chapter 2 Number System

Data Representation and Binary Arithmetic. Lecture 2

Chapter 2 Bits, Data Types, and Operations

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

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

Chapter 2 Bits, Data Types, and Operations

Fundamentals of Programming (C)

Chapter 2 Bits, Data Types, and Operations

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

Chapter 2 Bits, Data Types, and Operations

Chapter 3. Information Representation

Chapter 2 Bits, Data Types, and Operations

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

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

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

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.

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

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

Fundamentals of Programming

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

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

EE 109 Unit 2. Binary Representation Systems

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

Oberon Data Types. Matteo Corti. December 5, 2001

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

Number Systems Base r

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

Positional Number System

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

3.1. Unit 3. Binary Representation

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

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

ASSIGNMENT 5 TIPS AND TRICKS

RS-232 Control of the Advantage EQ281/8, EQ282/8 and Advantage SMS200

The Binary Number System

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

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

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

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

Do not start the test until instructed to do so!

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

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

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

Do not start the test until instructed to do so!

Do not start the test until instructed to do so!

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

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

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

CSE 30 Spring 2007 Final Exam

Variables and data types

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

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

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

Chapter 8. Characters and Strings

Simple Data Types in C. Alan L. Cox

Number Systems and Their Representations

CSE 30 Fall 2007 Final Exam

CSE 30 Winter 2014 Final Exam

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

Introduction to Computer Engineering. CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison

Source coding and compression

Chapter Three. Arithmetic

Table of Contents Sleep Settings How to Configure the Scanner. 7 Chapter 2 System Setup

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

CSE 30 Fall 2013 Final Exam

Lecture (09) x86 programming 8

Arithmetic for Computers

Fundamental Data Types

RS-232 Control of the Advantage DRI

CSE 30 Spring 2006 Final Exam

FD-011WU. 2D Barcode Reader User Guide V1.6CC

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

Chapter Two MIPS Arithmetic

CSE 30 Winter 2009 Final Exam

Chemistry Hour Exam 2

SPM723 Programmable Stereo Preamp/Mixer

Chapter 3 Arithmetic for Computers

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

J2 LCM Customer Display. Manual

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

4/14/2015. Architecture of the World Wide Web. During this session we will discuss: Structure of the World Wide Web

CSE 30 Fall 2012 Final Exam

S-Series Sensor ASCII Protocol v8.1.0

Hardware. ( Not so hard really )

User s Manual. Xi3000 Scanner. Table of Contents

CSE 30 Fall 2006 Final Exam

Imperial College London Department of Computing

marson MT8200S 2D Handheld Scanner User Manual V / 6 / 25 - I -

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

MOBILE THERMAL PRINTER

Number System. Introduction. Decimal Numbers

Midterm Exam, Fall 2015 Date: October 29th, 2015

MK D Imager Barcode Scanner Configuration Guide

Chapter 7. Binary, octal and hexadecimal numbers

Five classic components

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Computer Control of the Advantage DRC 4+4

Transcription:

Simple Arithmetic [Arithm Notes] Number representations Signed numbers Sign-magnitude, ones and twos complement Arithmetic Addition, subtraction, negation, overflow MIPS instructions Logic operations MIPS instructions Galen H. Sasaki EE 361 University of Hawaii 1 Number Representations C language data types: unsigned int unsigned integer ordinary binary int signed integer twos complement char text character ASCII float real number floating point (cover later) double real number floating point (cover later) Galen H. Sasaki EE 361 University of Hawaii 2

Binary Numbers Let s look at decimal representation of unsigned integers first * 40321 string of symbols (digits) * symbol (digit) is from the set {0, 1,..., 9} * value of 40321 is 4*10 4 + 0 *10 3 + 3*10 2 + 2*10 1 + 1*10 0 More generally, a string of digits with a base (or radix) b = 10 d n-1 d n-2... d 0 (digit d k is from {0, 1,..., 9}, for k = 0, 1,..., n-1) represents the value d n-1 * b n-1 + d n-2 * b n-2 +... + d 0 * b 0 n-1 = d k * b k k=0 Galen H. Sasaki EE 361 University of Hawaii 3 Common Representations Representation Base Digits Binary 2 {0,1} Octal 8 {0, 1, 2, 3, 4, 5, 6, 7} Hexadecimal 16 {0, 1,..., 9, A, B, C, D, E, F} or {0, 1,..., 9, a, b, c, d, e, f} Binary is a natural representation of signals and states in an electronic circuit because a signal/state will usually have one of TWO values. Octal and Hexadecimal are useful because basically they provide shorter representations of binary numbers Example: instead of binary 1001010001010001 we can write hexadecimal 9451 Factoid: 0x prefix is often used to indicate a hexadecimal number. Galen H. Sasaki EE 361 University of Hawaii 4

Simple Conversions Octal to binary: expand each octal digit to a 3 bit string 307 = Hexadecimal to binary: expand each digit to a 4 bit string 0xa3f = Binary to octal: break up binary string to 3-bit chunks, starting from the right Then convert chunks into octal digits 01 010 110 = Binary to hexadecimal: break up binary string into 4-bit chunks, starting from the right. Then convert chunks into hexadecimal digits 101 0010 1100 0101 = Galen H. Sasaki EE 361 University of Hawaii 5 Binary Addition Let s do decimal addition first 1 9 8 5 + 3 0 2 7 Addition is done by o Going from right to left o Adding in a digit position o With carries to the next digit position (carry occurs if a digit addition at digit position yields a two digit number) Binary Addition: same thing except digits are bits 1 0 1 1 + 0 0 1 1 Galen H. Sasaki EE 361 University of Hawaii 6

ASCII Representation of text characters by a binary string (within a byte) See page 142 of textbook for ASCII conversion table (Figure 3.15) Examples: a = 97 (decimal) A = 65 (decimal) 32-127 are mostly text characters 0-31 are used for control such as 10 = line-feed (LF) 13 = carriage-return (CR) 8 = backspace (BS) 4 = end-of-transmission (EOT): used to delimit a byte string Galen H. Sasaki EE 361 University of Hawaii 7 ASCII Char Dec Oct Hex Char Dec Oct Hex Char Dec Oct Hex Char Dec Oct Hex ----------------------------------------------------------------------------------------------------------------- (nul) 0 0000 0x00 (sp) 32 0040 0x20 @ 64 0100 0x40 ` 96 0140 0x60 (soh) 1 0001 0x01! 33 0041 0x21 A 65 0101 0x41 a 97 0141 0x61 (stx) 2 0002 0x02 " 34 0042 0x22 B 66 0102 0x42 b 98 0142 0x62 (etx) 3 0003 0x03 # 35 0043 0x23 C 67 0103 0x43 c 99 0143 0x63 (eot) 4 0004 0x04 $ 36 0044 0x24 D 68 0104 0x44 d 100 0144 0x64 (enq) 5 0005 0x05 % 37 0045 0x25 E 69 0105 0x45 e 101 0145 0x65 (ack) 6 0006 0x06 & 38 0046 0x26 F 70 0106 0x46 f 102 0146 0x66 (bel) 7 0007 0x07 ' 39 0047 0x27 G 71 0107 0x47 g 103 0147 0x67 (bs) 8 0010 0x08 ( 40 0050 0x28 H 72 0110 0x48 h 104 0150 0x68 (ht) 9 0011 0x09 ) 41 0051 0x29 I 73 0111 0x49 i 105 0151 0x69 (nl) 10 0012 0x0a * 42 0052 0x2a J 74 0112 0x4a j 106 0152 0x6a (vt) 11 0013 0x0b + 43 0053 0x2b K 75 0113 0x4b k 107 0153 0x6b (np) 12 0014 0x0c, 44 0054 0x2c L 76 0114 0x4c l 108 0154 0x6c (cr) 13 0015 0x0d - 45 0055 0x2d M 77 0115 0x4d m 109 0155 0x6d (so) 14 0016 0x0e. 46 0056 0x2e N 78 0116 0x4e n 110 0156 0x6e (si) 15 0017 0x0f / 47 0057 0x2f O 79 0117 0x4f o 111 0157 0x6f (dle) 16 0020 0x10 0 48 0060 0x30 P 80 0120 0x50 p 112 0160 0x70 (dc1) 17 0021 0x11 1 49 0061 0x31 Q 81 0121 0x51 q 113 0161 0x71 (dc2) 18 0022 0x12 2 50 0062 0x32 R 82 0122 0x52 r 114 0162 0x72 (dc3) 19 0023 0x13 3 51 0063 0x33 S 83 0123 0x53 s 115 0163 0x73 (dc4) 20 0024 0x14 4 52 0064 0x34 T 84 0124 0x54 t 116 0164 0x74 (nak) 21 0025 0x15 5 53 0065 0x35 U 85 0125 0x55 u 117 0165 0x75 (syn) 22 0026 0x16 6 54 0066 0x36 V 86 0126 0x56 v 118 0166 0x76 (etb) 23 0027 0x17 7 55 0067 0x37 W 87 0127 0x57 w 119 0167 0x77 (can) 24 0030 0x18 8 56 0070 0x38 X 88 0130 0x58 x 120 0170 0x78 (em) 25 0031 0x19 9 57 0071 0x39 Y 89 0131 0x59 y 121 0171 0x79 (sub) 26 0032 0x1a : 58 0072 0x3a Z 90 0132 0x5a z 122 0172 0x7a (esc) 27 0033 0x1b ; 59 0073 0x3b [ 91 0133 0x5b { 123 0173 0x7b (fs) 28 0034 0x1c < 60 0074 0x3c \ 92 0134 0x5c 124 0174 0x7c (gs) 29 0035 0x1d = 61 0075 0x3d ] 93 0135 0x5d } 125 0175 0x7d (rs) 30 0036 0x1e > 62 0076 0x3e ^ 94 0136 0x5e ~ 126 0176 0x7e (us) 31 0037 0x1f? 63 0077 0x3f _ 95 0137 0x5f (del) 127 0177 0x7f Galen H. Sasaki EE 361 University of Hawaii 8

Signed Integer Representation Binary numbers can represent unsigned integers, but what about signed integers Example: +12-513 Three representations: sign-magnitude ones complement twos complement Galen H. Sasaki EE 361 University of Hawaii 9 Sign-Magnitude Our usual notation: + 12 sign magnitude Sign-magnitude: 0 1100 binary representation of the magitude sign bit: 0 = +, and 1 = - What are the 5 bit representations (including the sign bit) of the values: + 8, - 9, + 3, - 7 Galen H. Sasaki EE 361 University of Hawaii 10

Arithmetic Addition: X + Y Negation: - Y Subtraction: X - Y or X + (-Y), so we only need to look at addition and negation Negation for sign-magnitude: easy, flip sign bit 00011 = +3 10011 = -3 Addition: more complicated X + Y if signs are the same then magnitude = X + Y sign = sign of X (or sign of Y) if signs are different then magnitude = larger magnitude - smaller magnitude sign = sign of number with larger magnitude Galen H. Sasaki EE 361 University of Hawaii 11 Ones Complement Representation of positive integer + 3: 0 00011 sign bit magnitude in binary Representation of negative integer -3: 1 11100 complement bits of the ones representation of +3. Note, first bit indicates negative sign. What value does the following represent? 11010 10011 Galen H. Sasaki EE 361 University of Hawaii 12

Twos Complement Sign-magnitude: negation is easy, but addition is a pain. We would a representation where addition is easy, like ordinary binary addition. Twos complement 1. Positive numbers are represented in ordinary binary, with sign bit 0 Example: 001010 = +6 sign bit 2. We ll get to negative numbers in a moment. 3. Suppose we have an m-bit representation. To negate, * complement all bits * add 1, using ordinary binary addition * truncate to m bits (the least significant m bits). Galen H. Sasaki EE 361 University of Hawaii 13 Twos Complement 4. Negative numbers * ordinary binary representation of magnitude * negate 4-bit representation of - 7 0111 (+7) 1000 (negate) + 1 1001 (-7) 5. Addition operation: for m-bit representation * ordinary binary addition 0110 (+6) * truncate to m bits + 1101 (-3) 10011 0011 (+3) Galen H. Sasaki EE 361 University of Hawaii 14

Why Does This Work? Adding a number with its negative should give 0. (+6) + (-6) 0 0110 (+6) + 1010 (-6) zero 0110 (+6) 1001 + 0001 0110 1001 + 0001 1111 + 0001 1 0000 Galen H. Sasaki EE 361 University of Hawaii 15 Why Does This Work? More generally, b m-1 b m-2... b 0 b m-1 b m-2... b 0 + 1 negative of b m-1 b m-2... b 0 b m-1 b m-2... b 0 + 1 1 1... 1 + 1 1 0 0... 0 Galen H. Sasaki EE 361 University of Hawaii 16

Another Intepretation of Twos Complement Here s another view of twos complement The representation of a negative number - M with n bits is the binary representation of 2 n -M Example: - M = - 9 n = 5 2 5-9 = 32-9 = 23 Binary representation of 23 is 10111 Galen H. Sasaki EE 361 University of Hawaii 17 Why Does This Work? Using the wheel to compute X + Y 111 (7) 000 (0) 001 (1) 110 (6) ordinary binary (2) 010 (5) (3) 101 011 (4) 100 X X + Y Start at X Go clockwise Y steps Galen H. Sasaki EE 361 University of Hawaii 18

Why Does This Work? Using the wheel to compute X - Y 111 (7) 000 (0) 001 (1) X - Y 110 (6) ordinary binary (2) 010 Go counter clockwise Y steps (5) 101 (4) 100 (3) 011 X Start at X Galen H. Sasaki EE 361 University of Hawaii 19 Why Does This Work? Using the wheel to compute X - Y Go clockwise to X - Y the long way 111 (7) 110 (6) (5) 101 000 (0) ordinary binary (4) 100 001 (1) (2) 010 (3) 011 X X - Y How many steps? 2 3 steps has us circumventing the wheel and back at X. We want to stop short by Y steps, so the number of steps should be 2 3 -Y. Start at X Galen H. Sasaki EE 361 University of Hawaii 20

Example 5-bit representations + 5 binary sign-magnitude ones complement twos complement -5 Value for the representation binary sign-magnitude ones complement twos complement 01011 11011 Galen H. Sasaki EE 361 University of Hawaii 21 Comparisons Arithmetic Sign-magnitude: negation is easy addition is hard Twos complement: negation is harder addition is easy Ones complement: nobody uses this too much Range of numbers with m bits Sign-magnitude: { + 0, + 1, + 2,..., + 2 m-1-1} Ones complement: { + 0, + 1, + 2,..., + 2 m-1-1} Twos complement: { + 0, + 1, + 2,..., + 2 m-1-1, -2 m-1 } Example: m = 3 Sign magnitude and ones complement: {-3, -2, -1, 0, 1, 2, 3} Twos complement: {-4, -3, -2, -1, -, 1, 2, 3} Galen H. Sasaki EE 361 University of Hawaii 22

Overflow Overflow occurs when you have m bits, but the number you want to represent needs more. Example: Can you represent -7 with 3 bits?: {-4, -3, -2, -1, 0, 1, 2, 3} Overflow occurs after some arithmetic operation Negation: Addition: Ones complement and sign magnitude: no overflow Twos complement: for one number, the most negative number. For example: 1000 (-4) cannot be negated since there is no 4-bit representation of +4 Overflow can sometimes occur when the addition creates a number with larger magnitude Thus, no overflow if the numbers are of different sign. But overflow can possibly occur if the numbers are of the same sign. Galen H. Sasaki EE 361 University of Hawaii 23 Overflow Exceptions MIPS processor checks when overflow occurs during its additions and subtractions If an overflow occurs then the MIPS does something to alert the user that a possible error occurred. This something is called an exception or interrupt. When is overflow bad? When it occurs from data. Then you want an exception. add, sub, slt, addi, subi, slti lead to exceptions upon overflow. When is overflow not bad? When it occurs from addresses. Then you want to ignore overflow. addu, subu, sltu, addiu, subiu, sltiu ignore overflows. Galen H. Sasaki EE 361 University of Hawaii 24

Logic Operations AND X Y OR Truth Table X + Y Truth Table X Y X*Y 0 0 0 0 1 0 1 0 0 1 1 1 X Y X+Y 0 0 0 0 1 1 1 0 1 1 1 1 Bit-wise operations between two strings of bits of equal length Pairs of bits from each bit position are AND d or OR d together Bit-wise AND Bit-wise OR X = 001011 X = 001011 Y = 101101 Y = 101101 001001 101111 Galen H. Sasaki EE 361 University of Hawaii 25 Logic Operation Instructions and $r1,$r2,$r3 andi $r1,$r2,constant Similar to add or sub but bit-wise AND operation Of course, overflow doesn t make sense here. The format is R type Similar to addi or subi but bit-wise AND operation The format is I type with a 16-bit constant. The constant is zero-extended to 32 bits, which means the first 16 bits are set to zero. or and ori are similar to and and andi except with a bit-wise OR. Galen H. Sasaki EE 361 University of Hawaii 26

and is used to clear bits Applications Data = 00010011011 Mask = 10110110001 Result = 00010010001 0 => clear and 1 => leave alone Application: check if the value of a particular bit Data = 000100b1011 Mask = 00000010000 Result = 000000b0000 Result = 0 if and only if b = 0 or is used to set bits Data = 00010011011 Mask = 10110110001 Result = 10110111011 1 => set to 1 and 0 => leave alone Galen H. Sasaki EE 361 University of Hawaii 27 Review Representation of C language data types Char, unsigned int, and int Binary and twos complement arithmetic Logic operations and what they re used for New instructions addiu, subiu, sltiu, and, andi, or, ori Galen H. Sasaki EE 361 University of Hawaii 28