CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Similar documents
CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Integers Sep 3, 2002

Integers. Dr. Steve Goddard Giving credit where credit is due

Systems 1. Integers. Unsigned & Twoʼs complement. Addition, negation, multiplication

Integers. Today. Next time. ! Numeric Encodings! Programming Implications! Basic operations. ! Floats

Topics of this Slideset. CS429: Computer Organization and Architecture. Encoding Integers: Unsigned. C Puzzles. Integers

CS429: Computer Organization and Architecture

Bits, Bytes, and Integers

Representing and Manipulating Integers. Jo, Heeseung

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Systems Programming and Computer Architecture ( )

Bits, Bytes, and Integers August 26, 2009

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 270: Computer Organization. Integer Arithmetic Operations

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Representing Integers

Bits, Bytes, and Integers Part 2

CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

Lecture 5-6: Bits, Bytes, and Integers

Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Bits, Bytes and Integers Part 1

Representing and Manipulating Integers Part I

Manipulating Integers

Integer Encoding and Manipulation

Page # CISC360. Integers Sep 11, Encoding Integers Unsigned. Encoding Example (Cont.) Topics. Twoʼs Complement. Sign Bit

Computer Systems CEN591(502) Fall 2011

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integer Arithmetic. CS 347 Lecture 03. January 20, 1998

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Arithmetic and Bitwise Operations on Binary Data

Integers II. CSE 351 Autumn 2018

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Foundations of Computer Systems

Page 1 CISC360. Encoding Integers Unsigned. Integers Sep 8, Numeric Ranges. Encoding Example (Cont.)

C Puzzles! Taken from old exams. Integers Sep 3, Encoding Integers Unsigned. Encoding Example (Cont.) The course that gives CMU its Zip!

Bits, Bytes and Integers

Computer Organization: A Programmer's Perspective

Integer Representation

Bits, Bytes, and Integers

Bits, Bytes, and Integers

Bits, Bytes, and Integers. Bits, Bytes, and Integers. The Decimal System and Bases. Everything is bits. Converting from Decimal to Binary

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

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

Bitwise Data Manipulation. Bitwise operations More on integers

Arithmetic and Bitwise Operations on Binary Data

BITS, BYTES, AND INTEGERS

Lecture 8: Addition, Multiplication & Division

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

CS 107 Lecture 3: Integer Representations

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

Arithmetic Processing

CO212 Lecture 10: Arithmetic & Logical Unit

CHW 261: Logic Design

COMP2611: Computer Organization. Data Representation

Learning Objectives. Binary over Decimal. In this chapter you will learn about:

9 Multiplication and Division

Arithmetic Operators. Portability: Printing Numbers

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

Information Science 1

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

Data Representations & Arithmetic Operations

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Module 2: Computer Arithmetic

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

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

LECTURE 3 C++ Basics Part 2

Organisasi Sistem Komputer

Simple Data Types in C. Alan L. Cox

SIGNED AND UNSIGNED SYSTEMS

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS367 Test 1 Review Guide

Signed Multiplication Multiply the positives Negate result if signs of operand are different

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

COMP 122/L Lecture 2. Kyle Dewey

Chapter 2 Data Representations

Programming in C++ 5. Integral data types

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

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

Chapter 5: Computer Arithmetic

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

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

CS 64 Week 1 Lecture 1. Kyle Dewey

Information Science 1

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

REMEMBER TO REGISTER FOR THE EXAM.

CS & IT Conversions. Magnitude 10,000 1,

COMPUTER ARITHMETIC (Part 1)

Computer Organization & Systems Exam I Example Questions

Chapter 10 - Computer Arithmetic

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

Chapter 3: Arithmetic for Computers

Transcription:

CS 33 Data Representation, Part 2 CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Signed Integers Two s complement b w-1 = 0 Þ non-negative number value = b w-1 = 1 Þ negative number one zero! value = CS33 Intro to Computer Systems VII 2 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Signed Integers Negating two s complement value = b w 1 2 w 1 + how to compute value? (~value)+1 w 2 i=0 b i 2 i CS33 Intro to Computer Systems VII 3 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Signed Integers Negating two s complement (continued) value + (~value + 1) = (value + ~value) + 1 = (2 w 1) + 1 = 2 w 0 0 0 0 0 0 w = 1 CS33 Intro to Computer Systems VII 4 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 1 We have a computer with 4-bit words that uses two s complement to represent negative numbers. What is the result of subtracting 0010 (2) from 0001 (1)? a) 0111 b) 1001 c) 1110 d) 1111 CS33 Intro to Computer Systems VII 5 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Signed vs. Unsigned in C char, short, int, and long signed integer types right shift (>>) is arithmetic unsigned char, unsigned short, unsigned int, unsigned long unsigned integer types right shift (>>) is logical CS33 Intro to Computer Systems VII 6 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Numeric Ranges Unsigned Values UMin = 0 000 0 UMax = 2 w 1 111 1 Two s Complement Values TMin = 2 w 1 100 0 TMax = 2 w 1 1 011 1 Other Values Minus 1 111 1 Values for W = 16 Decimal Hex Binary UMax 65535 FF FF 11111111 11111111 TMax 32767 7F FF 01111111 11111111 TMin -32768 80 00 10000000 00000000-1 -1 FF FF 11111111 11111111 0 0 00 00 00000000 00000000 CS33 Intro to Computer Systems VII 7 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Values for Different Word Sizes W 8 16 32 64 UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615 TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807 TMin -128-32,768-2,147,483,648-9,223,372,036,854,775,808 Observations TMin = TMax + 1» Asymmetric range UMax = 2 * TMax + 1 C Programming #include <limits.h> declares constants, e.g., ULONG_MAX LONG_MAX LONG_MIN values platform-specific CS33 Intro to Computer Systems VII 8 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 2 What is TMin (assuming two s complement signed integers)? a) TMin b) TMax c) 0 d) 1 CS33 Intro to Computer Systems VII 9 Copyright 2018 Thomas W. Doeppner. All rights reserved.

4-Bit Computer Arithmetic 13-3 14-2 1101 1110 15-1 1111 0 0 0000 1 1 0001 0010 2 2 12-4 1100 0011 3 3 11-5 1011 0100 4 4 1010 0101 5 5-6 10 9 1001-7 1000 8-8 0111 7 7 0110 6 6 CS33 Intro to Computer Systems VII 10 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Signed vs. Unsigned in C Constants by default are considered to be signed integers unsigned if have U as suffix 0U, 4294967259U Casting explicit casting between signed & unsigned int tx, ty; unsigned ux, uy; // unsigned means unsigned int tx = (int) ux; uy = (unsigned int) ty; implicit casting also occurs via assignments and procedure calls tx = ux; uy = ty; CS33 Intro to Computer Systems VII 11 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Casting Surprises Expression evaluation if there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned including comparison operations <, >, ==, <=, >= examples for W = 32: TMIN = -2,147,483,648, TMAX = 2,147,483,647 Constant 1 Constant 2 Relation Evaluation 0 0U == unsigned -1 0 < signed -1 0U > unsigned 2147483647-2147483647-1 > signed 2147483647U -2147483647-1 < unsigned -1-2 > signed (unsigned)-1-2 > unsigned 2147483647 2147483648U < unsigned 2147483647 (int)2147483648u > signed CS33 Intro to Computer Systems VII 12 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 3 What is the value of (long)ulong_max - (unsigned long)-1??? a) -1 b) 0 c) 1 d) ULONG_MAX CS33 Intro to Computer Systems VII 13 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Sign Extension Task: given w-bit signed integer x convert it to w+k-bit integer with same value Rule: make k copies of sign bit: X = x w 1,, x w 1, x w 1, x w 2,, x 0 k copies of MSB X w X k w CS33 Intro to Computer Systems VII 14 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Sign Extension Example short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y; Decimal Hex Binary x 15213 3B 6D 00111011 01101101 ix 15213 00 00 3B 6D 00000000 00000000 00111011 01101101 y -15213 C4 93 11000100 10010011 iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011 Converting from smaller to larger integer data type C automatically performs sign extension CS33 Intro to Computer Systems VII 15 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Does it Work? val w = 2 w 1 + w 2 b i 2 i i=0 w 2 i=0 val w+1 = 2 w + 2 w 1 + b i 2 i w 2 = 2 w 1 + b i 2 i i=0 w 2 i=0 w 2 i=0 val w+2 = 2 w+1 + 2 w + 2 w 1 + b i 2 i = 2 w + 2 w 1 + b i 2 i w 2 = 2 w 1 + b i 2 i i=0 CS33 Intro to Computer Systems VII 16 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Unsigned Multiplication Operands: w bits True Product: 2*w bits Discard w bits: w bits u * v u * v UMult w (u, v) Standard multiplication function ignores high order w bits Implements modular arithmetic UMult w (u, v) = u v mod 2 w CS33 Intro to Computer Systems VII 17 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Signed Multiplication Operands: w bits True Product: 2*w bits Discard w bits: w bits u * v u * v TMult w (u, v) Standard multiplication function ignores high order w bits some of which are different from those of unsigned multiplication lower bits are the same CS33 Intro to Computer Systems VII 18 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Power-of-2 Multiply with Shift Operation u << k gives u * 2 k both signed and unsigned operands: w bits true product: w+k bits Examples u * 2 k u << 3 == u * 8 u << 5 - u << 3 == u * 24 most machines shift and add faster than multiply» compiler generates this code automatically * u 2 k discard k bits: w bits UMult w (u, 2 k ) TMult w (u, 2 k ) k 0 0 1 0 0 0 0 0 0 0 0 0 CS33 Intro to Computer Systems VII 19 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Unsigned Power-of-2 Divide with Shift Quotient of unsigned by power of 2 u >> k gives ë u / 2 k û uses logical shift operands: division: u 2 k / u / 2 k k 0 0 1 0 0 0 0 0 0. binary point result: ë u / 2 k û 0 0 0 Division Computed Hex Binary x 15213 15213 3B 6D 00111011 01101101 x >> 1 7606.5 7606 1D B6 00011101 10110110 x >> 4 950.8125 950 03 B6 00000011 10110110 x >> 8 59.4257813 59 00 3B 00000000 00111011 CS33 Intro to Computer Systems VII 20 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Signed Power-of-2 Divide with Shift Quotient of signed by power of 2 x >> k gives ë x / 2 k û uses arithmetic shift rounds wrong direction when x < 0 operands: division: result: x 2 k / x / 2 k RoundDown(x / 2 k ) 0 0 1 0 0 0 CS33 Intro to Computer Systems VII 21 Copyright 2018 Thomas W. Doeppner. All rights reserved. k 0. 0 binary point Division Computed Hex Binary y -15213-15213 C4 93 11000100 10010011 y >> 1-7606.5-7607 E2 49 11100010 01001001 y >> 4-950.8125-951 FC 49 11111100 01001001 y >> 8-59.4257813-60 FF C4 11111111 11000100

Correct Power-of-2 Divide Quotient of negative number by power of 2 want é x / 2 k ù (round toward 0) compute as ë (x+2 k -1)/ 2 k û» in C: (x + (1<<k)-1) >> k» biases dividend toward 0 Case 1: no rounding dividend: divisor: u +2 k 1 / 2 k é u / 2 k ù k 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 01 1 1 1. binary point 1 1 1 Biasing has no effect CS33 Intro to Computer Systems VII 22 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Correct Power-of-2 Divide (Cont.) Case 2: rounding dividend: k 1 x +2 k 1 0 0 0 1 1 1 1 incremented by 1 binary point divisor: 2 k / é x / 2 k ù 0 0 1 0 0 0 01 1 1 1. incremented by 1 Biasing adds 1 to final result CS33 Intro to Computer Systems VII 23 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Why Should I Use Unsigned? Don t use just because number nonnegative easy to make mistakes unsigned i; for (i = cnt-2; i >= 0; i--) a[i] += a[i+1]; can be very subtle #define DELTA sizeof(int) int i; for (i = CNT; i-delta >= 0; i-= DELTA)... Do use when performing modular arithmetic multiprecision arithmetic Do use when using bits to represent sets logical right shift, no sign extension CS33 Intro to Computer Systems VII 24 Copyright 2018 Thomas W. Doeppner. All rights reserved.