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

Similar documents
CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VII 1 Copyright 2018 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

Bits, Bytes, and Integers

Bits, Bytes, and Integers Part 2

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

CS429: Computer Organization and Architecture

Bits, Bytes, and Integers August 26, 2009

Representing and Manipulating Integers. Jo, Heeseung

Systems Programming and Computer Architecture ( )

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

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

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

CS 270: Computer Organization. Integer Arithmetic Operations

Lecture 5-6: Bits, Bytes, and Integers

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

Representing Integers

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

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

Computer Systems CEN591(502) Fall 2011

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

Representing and Manipulating Integers Part I

Bits, Bytes and Integers Part 1

Foundations of Computer Systems

Manipulating Integers

Integer Encoding and Manipulation

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

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Bits, Bytes and Integers

Arithmetic and Bitwise Operations on Binary Data

Computer Organization: A Programmer's Perspective

Integers II. CSE 351 Autumn 2018

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

Integer Representation

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

Bits, Bytes, and Integers

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

Bitwise Data Manipulation. Bitwise operations More on integers

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

CS367 Test 1 Review Guide

BITS, BYTES, AND INTEGERS

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

Arithmetic and Bitwise Operations on Binary Data

Simple Data Types in C. Alan L. Cox

REMEMBER TO REGISTER FOR THE EXAM.

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

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CIS133J. Working with Numbers in Java

CS 107 Lecture 3: Integer Representations

Module 2: Computer Arithmetic

Information Science 1

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

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

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Computer Organization & Systems Exam I Example Questions

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

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

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CHW 261: Logic Design

Arithmetic Processing

CS Programming In C

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

CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Data Representations & Arithmetic Operations

Arithmetic Operators. Portability: Printing Numbers

Lecture 8: Addition, Multiplication & Division

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

Information Science 1

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

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

Computer Systems Programming. Practice Midterm. Name:

System Programming CISC 360. Floating Point September 16, 2008

Bits, Bytes, and Integer

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

SIGNED AND UNSIGNED SYSTEMS

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

15213 Recitation 2: Floating Point

Chapter 2 Data Representations

CO212 Lecture 10: Arithmetic & Logical Unit

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

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

COMP2611: Computer Organization. Data Representation

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

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

Operations, Operands, and Instructions

9 Multiplication and Division

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

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

Chapter 7. Basic Types

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

Transcription:

CS 33 Data Representation, Part 2 CS33 Intro to Computer Systems VIII 1 Copyright 2017 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 VIII 2 Copyright 2017 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 VIII 3 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

4-Bit Computer Arithmetic CS33 Intro to Computer Systems VIII 5 Copyright 2017 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 VIII 6 Copyright 2017 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 > signed 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 VIII 7 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 2 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 VIII 8 Copyright 2017 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 VIII 9 Copyright 2017 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 VIII 10 Copyright 2017 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 VIII 11 Copyright 2017 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 VIII 12 Copyright 2017 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 VIII 13 Copyright 2017 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 u * 2 k Examples 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 VIII 14 Copyright 2017 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 VIII 15 Copyright 2017 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 VIII 16 Copyright 2017 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 VIII 17 Copyright 2017 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 VIII 18 Copyright 2017 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 VIII 19 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Byte-Oriented Memory Organization Programs refer to data by address conceptually, envision it as a very large array of bytes» in reality, it s not, but can think of it that way an address is like an index into that array» and, a pointer variable stores an address Note: system provides private address spaces to each process think of a process as a program being executed so, a program can clobber its own data, but not that of others CS33 Intro to Computer Systems VIII 20 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Machine Words Any given computer has a word size nominal size of integer-valued data» and of addresses until recently, most machines used 32 bits (4 bytes) as word size» limits addresses to 4GB (2 32 bytes)» becomes too small for memory-intensive applications leading to emergence of computers with 64-bit word size machines still support multiple data formats» fractions or multiples of word size» always integral number of bytes CS33 Intro to Computer Systems VIII 21 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Word-Oriented Memory Organization 32-bit Words 64-bit Words Bytes Addr. Addresses specify byte locations address of first byte in word addresses of successive words differ by 4 (32-bit) or 8 (64-bit) Addr = 0000?? Addr = 0004?? Addr = 0008?? Addr = 0012?? Addr = 0000?? Addr = 0008?? 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 CS33 Intro to Computer Systems VIII 22 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Byte Ordering Four-byte integer 0x76543210 Stored at location 0x100 which byte is at 0x100? which byte is at 0x103? 10 32 54 76 Little-endian? 0x100 0x101 0x102 0x103 76 54 32 10 Big-endian 0x100 0x101 0x102 0x103 CS33 Intro to Computer Systems VIII 23 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Byte Ordering (2) Big Endian 00 00 00 01 Little Endian CS33 Intro to Computer Systems VIII 24 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 3 int main() { long x=1; proc((int *)&x); return 0; } void proc(int *arg) { printf("%d\n", *arg); } What value is printed on a big-endian 64-bit computer? a) 0 b) 1 c) 2 32 d) 2 32-1 CS33 Intro to Computer Systems VIII 25 Copyright 2017 Thomas W. Doeppner. All rights reserved.