Data III & Integers I

Similar documents
Data III & Integers I

Data III & Integers I

CSE351: Memory, Data, & Addressing I

Hardware: Logical View

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

Memory, Data, & Addressing I

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Bitwise Data Manipulation. Bitwise operations More on integers

Assembly Programming IV

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

Java and C. CSE 351 Autumn 2018

Java and C CSE 351 Spring

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

Structs and Alignment CSE 351 Spring

The Stack & Procedures

Arithmetic and Bitwise Operations on Binary Data

Arithmetic and Bitwise Operations on Binary Data

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

CSCI2467: Systems Programming Concepts

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

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

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

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

x86 Programming I CSE 351 Winter

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

Arrays. CSE 351 Autumn Instructor: Justin Hsia

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

L08: Assembly Programming II. Assembly Programming II. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Structs and Alignment

x86-64 Programming III & The Stack

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Arrays. CSE 351 Autumn Instructor: Justin Hsia

BITS, BYTES, AND INTEGERS

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Bits and Bytes January 13, 2005

We made it! Java: Assembly language: OS: Machine code: Computer system:

Caches II. CSE 351 Spring Instructor: Ruth Anderson

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Cache Example, System Control Flow

Floating Point II, x86 64 Intro

Building an Executable

Bits, Bytes and Integers Part 1

COMP2611: Computer Organization. Data Representation

Where we are going (today)

Structs and Alignment

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

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

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn 2018

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

Integer Representation

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Lecture 5-6: Bits, Bytes, and Integers

Bits, Bytes, and Integers

Bits, Bytes, and Integers

Announcements* Hardware:*Logical*View* Hardware:*SemiVLogical*View* Hardware:*Physical*View*

The Hardware/Software Interface CSE351 Spring 2015

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

Inf2C - Computer Systems Lecture 2 Data Representation

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

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

Bits, Bytes, and Integers Part 2

Chapter 4. Operations on Data

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

Integer Representation

EE292: Fundamentals of ECE

Where are we? Compiler. translating source code (C or Java) Programs to assembly language And linking your code to Library code

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

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

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

JAVA OPERATORS GENERAL

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

The Hardware/Software Interface CSE351 Spring 2015

Binary Values. CSE 410 Lecture 02

Assembly Programming IV

CS 261 Fall Mike Lam, Professor Integer Encodings

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

Review 1/2 MIPS assembly language instructions mapped to numbers in 3 formats. CS61C Negative Numbers and Logical Operations R I J.

Computer Systems CSE 410 Autumn Memory Organiza:on and Caches

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

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

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

University of Washington

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

LAB A Translating Data to Binary

Cache Wrap Up, System Control Flow

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

University*of*Washington*

Transcription:

Data III & Integers I CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis

Administrivia Everyone has VM or access to attu? LET US KNOW if not Section - Room changes for 9:30 and 10:30 sections Lab 0, due TONIGHT (4/3) @ 11:59pm Homework 1, due TONIGHT (4/3) @ 11:59pm Readings in CSAPP see schedule Office Hours see schedule LET US KNOW if you cannot make any of our posted office hours Lab 1 posted later today! 2

Memory, Data, and Addressing Representing information as bits and bytes Organizing and addressing data in memory Manipulating data in memory using C Boolean algebra and bit-level manipulations 3

Boolean Algebra Developed by George Boole in 19th Century Algebraic representation of logic (True 1, False 0) AND: A&B=1 when both A is 1 and B is 1 OR: A B=1 when either A is 1 or B is 1 XOR: NOT: ~A=1 when A is 0 and vice-versa DeMorgan s Law: ~(A B) = ~A & ~B ~(A&B) = ~A ~B A^B=1 when either A is 1 or B is 1, but not both AND OR XOR NOT & 0 1 0 1 ^ 0 1 ~ 0 0 0 0 0 1 0 0 1 0 1 1 0 1 1 1 1 1 1 0 1 0 4

General Boolean Algebras Operate on bit vectors Operations applied bitwise All of the properties of Boolean algebra apply 01101001 & 01010101 Examples of useful operations: x ^x = 0 01101001 01010101 01101001 ^ 01010101 ~ 01010101 01010101 ^ 01010101 00000000 x 1 = 1, x 0 = x 01010101 11110000 11110101 5

Representing & Manipulating Sets Representation A w-bit vector represents subsets of {0,, w 1} a j = 1 iff j A 01101001 { 0, 3, 5, 6 } 76543210 01010101 { 0, 2, 4, 6 } 76543210 Operations & Intersection 01000001 { 0, 6 } Union 01111101 { 0, 2, 3, 4, 5, 6 } ^ Symmetric difference 00111100 { 2, 3, 4, 5 } ~ Complement 10101010 { 1, 3, 5, 7 } 6

Bit-Level Operations in C & (AND), (OR), ^ (XOR), ~ (NOT) View arguments as bit vectors, apply operations bitwise Apply to any integral data type long, int, short, char, unsigned Examples with char a, b, c; a = (char) 0x41; // 0x41->0b 0100 0001 b = ~a; // 0b ->0x a = (char) 0x69; // 0x69->0b 0110 1001 b = (char) 0x55; // 0x55->0b 0101 0101 c = a & b; // 0b ->0x a = (char) 0x41; // 0x41->0b 0100 0001 b = a; // 0b 0100 0001 c = a ^ b; // 0b ->0x 7

Contrast: Logic Operations Logical operators in C: && (AND), (OR),! (NOT) 0 is False, anything nonzero is True Always return 0 or 1 Early termination (a.k.a. short-circuit evaluation) of &&, Examples (char data type)!0x41 -> 0x00 0xCC && 0x33 -> 0x01!0x00 -> 0x01 0x00 0x33 -> 0x01!!0x41 -> 0x01 p && *p++ Avoids null pointer (0x0) access via early termination Short for: if (p) { *p++; } 8

Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp %rbp 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111 Java: Car c = new Car(); c.setmiles(100); c.setgals(17); float mpg = c.getmpg(); OS: Memory & data Integers & floats x86 assembly Procedures & stacks Executables Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C Computer system: 9

But before we get to integers. Encode a standard deck of playing cards 52 cards in 4 suits, 13 cards per suit How do we encode suits, face cards? What operations do we want to make easy to implement? Which is the higher value card? Are they the same suit? 10

Two possible representations 1) 1 bit per card (52): bit corresponding to card set to 1 One-hot encoding (similar to set notation) Drawbacks: Hard to compare values and suits Large number of bits required low-order 52 bits of 64-bit word 2) 1 bit per suit (4), 1 bit per number (13): 2 bits set 13 numbers Pair of one-hot encoded values 4 suits Easier to compare suits and values, but still lots of bits used Can we do better? 11

Two better representations 3) Binary encoding of all 52 cards only 6 bits needed 2 6 = 64 52 low-order 6 bits of a byte Fits in one byte (smaller than one-hot encodings) How can we make value and suit comparisons easier? 4) Separate binary encodings of suit (2 bits) and value (4 bits) suit value Also fits in one byte, and easy to do comparisons K Q J... 3 2 A 1101 1100 1011... 0011 0010 0001 00 01 10 11 12

Compare Card Suits char hand[5]; #define SUIT_MASK 0x30 L04: Data III & Integers I mask: a bit vector designed to achieve a desired behavior when used with a bitwise operator on another bit vector v. Here we turn all but the bits of interest in v to 0. // represents a 5-card hand char card1, card2; // two cards to compare card1 = hand[0]; card2 = hand[1];... if ( samesuitp(card1, card2) ) {... } int samesuitp(char card1, char card2) { return (!((card1 & SUIT_MASK) ^ (card2 & SUIT_MASK))); //return (card1 & SUIT_MASK) == (card2 & SUIT_MASK); } returns int SUIT_MASK = 0x30 = 0 0 1 1 0 0 0 0 equivalent suit value 13

Compare Card Suits #define SUIT_MASK 0x30 L04: Data III & Integers I mask: a bit vector designed to achieve a desired behavior when used with a bitwise operator on another bit vector v. Here we turn all but the bits of interest in v to 0. int samesuitp(char card1, char card2) { return (!((card1 & SUIT_MASK) ^ (card2 & SUIT_MASK))); //return (card1 & SUIT_MASK) == (card2 & SUIT_MASK); } 0 0 0 1 0 0 1 0 0 0 0 1 1 1 0 1 & 0 0 1 1 0 0 0 0 SUIT_MASK 0 0 1 1 0 0 0 0 = 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0!(x^y) equivalent to x==y ^ 0 0 0 0 0 0 0 0! 0 0 0 0 0 0 0 1 & = 14

Compare Card Values L04: Data III & Integers I mask: a bit vector designed to achieve a desired behavior when used with a bitwise operator on another bit vector v. char hand[5]; // represents a 5-card hand char card1, card2; // two cards to compare card1 = hand[0]; card2 = hand[1];... if ( greatervalue(card1, card2) ) {... } #define VALUE_MASK 0x0F int greatervalue(char card1, char card2) { return ((unsigned int)(card1 & VALUE_MASK) > (unsigned int)(card2 & VALUE_MASK)); } VALUE_MASK = 0x0F = 0 0 0 0 1 1 1 1 suit value 15

Compare Card Values #define VALUE_MASK 0x0F L04: Data III & Integers I int greatervalue(char card1, char card2) { return ((unsigned int)(card1 & VALUE_MASK) > (unsigned int)(card2 & VALUE_MASK)); } 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 & 0 0 0 0 1 1 1 1 VALUE_MASK 0 0 0 0 1 1 1 1 = 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 1 2 10 > 13 10 0 (false) & = mask: a bit vector designed to achieve a desired behavior when used with a bitwise operator on another bit vector v. 16

Integers Binary representation of integers Unsigned and signed Casting in C Consequences of finite width representation Overflow, sign extension Shifting and arithmetic operations 17

Encoding Integers The hardware (and C) supports two flavors of integers unsigned only the non-negatives signed both negatives and non-negatives Cannot represent all integers with w bits Only 2 w distinct bit patterns Unsigned values: 0... 2 w 1 Signed values: 2 w 1 2 w 1 1 Example: 8-bit integers (e.g. char) - 2 8 1 0 +2 8 1 +2 8 128 0 +128 +256 + 18

Unsigned Integers Unsigned values follow the standard base 2 system b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 = b 7 2 7 + b 6 2 6 + + b 1 2 1 + b 0 2 0 Add and subtract using the normal carry and borrow rules, just in binary 63 + 8 71 00111111 +00001000 01000111 Useful formula: 2 N 1 + 2 N 2 + + 2 + 1 = 2 N 1 i.e. N ones in a row = 2 N 1 How would you make signed integers? 19

Sign and Magnitude Designate the high-order bit (MSB) as the sign bit sign=0: positive numbers; sign=1: negative numbers Benefits: Using MSB as sign bit matches positive numbers with unsigned All zeros encoding is still = 0 Examples (8 bits): 0x00 = 00000000 2 is non-negative, because the sign bit is 0 0x7F = 01111111 2 is non-negative (+127 10 ) 0x85 = 10000101 2 is negative (-5 10 ) 0x80 = 10000000 2 is negative... zero??? Most Significant Bit 20

Sign and Magnitude MSB is the sign bit, rest of the bits are magnitude Drawbacks? 15 0 14 1111 0000 1 13 1110 0001 1101 0010 12 1100 Unsigned 0011 11 1011 1010 0100 0101 10 1001 0110 9 1000 0111 6 8 7 2 3 4 5 4 5 3 2 6 1101 1100 1011 1 1010 7 1110 1001 0 1111 0000 Sign and Magnitude 1000 0111 + 0 0001 0110 + 7 + 1 0010 0011 0100 0101 + 6 + 2 + 5 + 3 + 4 21

Sign and Magnitude MSB is the sign bit, rest of the bits are magnitude Drawbacks: Two representations of 0 (bad for checking equality) 4 5 3 2 6 1100 1011 1 1101 1010 7 1110 1001 0 1111 0000 Sign and Magnitude 1000 0111 + 0 0001 0110 + 7 + 1 0010 0011 0100 0101 + 6 + 2 + 5 + 3 + 4 22

Sign and Magnitude MSB is the sign bit, rest of the bits are magnitude Drawbacks: Two representations of 0 (bad for checking equality) 4-3 1 Arithmetic is cumbersome Example: 4-3!= 4+(-3) 0100-0011 0001 4 + -3-7 0100 + 1011 1111 Negatives increment in wrong direction! 4 5 3 2 6 1100 1011 1 1101 1010 7 1110 1001 0 1111 0000 Sign and Magnitude 1000 0111 + 0 0001 0110 + 7 + 1 0010 0011 0100 0101 + 6 + 2 + 5 + 3 + 4 23

Two s Complement Let s fix these problems: 1) Flip negative encodings so incrementing works 0 1 1111 2 1110 1101 3 1100 + 0 0000 + 1 0001 + 2 0010 + 3 0011 4 1011 1010 5 1001 0100 0101 0110 6 1000 0111 + 6 7 + 7 + 5 + 4 24

Two s Complement Let s fix these problems: 1) Flip negative encodings so incrementing works 2) Shift negative numbers to eliminate 0 MSB still indicates sign! This is why we represent one more negative than positive number (-2 N 1 to 2 N 1 1) 1 + 0 2 1111 0000 + 1 3 1110 0001 + 2 1101 0010 4 1100 0011 + 3 5 1011 1010 6 1001 0100 0101 0110 7 1000 0111 + 6 8 + 7 + 5 + 4 25

Two s Complement Negatives Accomplished with one neat mathematical trick! b w 1 has weight 2 w 1, other bits have usual weights +2 i b w-1 b w-2... b 0 4-bit Examples: 1010 2 unsigned: 1*2 3 +0*2 2 +1*2 1 +0*2 0 = 10 1010 2 two s complement: -1*2 3 +0*2 2 +1*2 1 +0*2 0 = 6-1 represented as: 1111 2 = -2 3 +(2 3 1) MSB makes it super negative, add up all the other bits to get back up to -1 4 3 5 6 2 1100 1011 7 1101 1010 1 1110 1001 8 1111 0000 Two s Complement 1000 0111 + 0 0001 0110 + 7 + 1 0010 0011 0100 0101 + 6 + 2 + 5 + 3 + 4 26

Why Two s Complement is So Great Roughly same number of (+) and ( ) numbers Positive number encodings match unsigned Single zero All zeros encoding = 0 Simple negation procedure: Get negative representation of any integer by taking bitwise complement and then adding one! ( ~x + 1 == -x ) 4 3 5 6 2 1100 1011 7 1101 1010 1 1110 1001 8 1111 0000 Two s Complement 1000 0111 + 0 0001 0110 + 7 + 1 0010 0011 0100 0101 + 6 + 2 + 5 + 3 + 4 27

Question Take the 4-bit number encoding x = 0b1011 Which of the following numbers is NOT a valid interpretation of x using any of the number representation schemes discussed today? Unsigned, Sign and Magnitude, Two s Complement A. -4 B. -5 C. 11 D. -3 28

Summary Bit-level operators allow for fine-grained manipulations of data Bitwise AND (&), OR ( ), and NOT (~) different than logical AND (&&), OR ( ), and NOT (!) Especially useful with bit masks Choice of encoding scheme is important Tradeoffs based on size requirements and desired operations Integers represented using unsigned and two s complement representations Limited by fixed bit width We ll examine arithmetic operations next lecture 29