Data III & Integers I

Similar documents
Data III & Integers I

Data III & Integers I

Hardware: Logical View

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

Structs and Alignment

Memory, Data, & Addressing I

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Cache Example, System Control Flow

Java and C. CSE 351 Autumn 2018

Bitwise Data Manipulation. Bitwise operations More on integers

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

CSE351: Memory, Data, & Addressing I

Java and C CSE 351 Spring

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

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

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

Arithmetic and Bitwise Operations on Binary Data

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Arithmetic and Bitwise Operations on Binary Data

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

Structs and Alignment CSE 351 Spring

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

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

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

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

x86 Programming I CSE 351 Winter

Arrays. CSE 351 Autumn Instructor: Justin Hsia

x86-64 Programming III & The Stack

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

Floating Point II, x86 64 Intro

BITS, BYTES, AND INTEGERS

Structs and Alignment

Caches II. CSE 351 Autumn Instructor: Justin Hsia

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

CSCI2467: Systems Programming Concepts

Bits and Bytes January 13, 2005

Assembly Programming IV

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

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Building an Executable

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

Integers II. CSE 351 Autumn 2018

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

Bits, Bytes and Integers Part 1

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Where we are going (today)

Assembly Programming IV

Machine Programming. CSE 351 Autumn Instructor: Justin Hsia

Bits, Bytes, and Integers

Lecture 5-6: Bits, Bytes, and Integers

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

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

COMP2611: Computer Organization. Data Representation

Memory Allocation III

Bits, Bytes, and Integers

Integer Representation

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

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

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

Cache Wrap Up, System Control Flow

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

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

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

Bits, Bytes, and Integers Part 2

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

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

EE292: Fundamentals of ECE

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

Inf2C - Computer Systems Lecture 2 Data Representation

Computer Systems CSE 410 Autumn Memory Organiza:on and Caches

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

CS33 Project Gear Up. Data

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

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

JAVA OPERATORS GENERAL

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

The Hardware/Software Interface CSE351 Spring 2015

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

Chapter 4. Operations on Data

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

Integer Representation

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

University of Washington

The Hardware/Software Interface CSE351 Spring 2015

x86-64 Programming III

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

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

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

LAB A Translating Data to Binary

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

University*of*Washington*

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Transcription:

Data III & Integers I CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/257/

Administrivia Lab 1 has been released Based on material from today s and Friday s lecture Section 2 tomorrow New rooms for AC/AG (EEB 003) and BC (GUG 218) Good preparation for Lab 1 Make good use of office hours! Can go to any office hours for any TA 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 01000001 01101001 01010101 01111101 Examples of useful operations: 01101001 ^ 01010101 00111100 01010101 ^ 01010101 00000000 ~ 01010101 10101010 01010101 11110000 11110101 How does this relate to set operations? 5

Representing & Manipulating Sets Representation A bit vector represents subsets of {0,, 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 1011 1110->0xBE a = (char) 0x69; // 0x69->0b 0110 1001 b = (char) 0x55; // 0x55->0b 0101 0101 c = a & b; // 0b 0100 0001->0x41 a = (char) 0x41; // 0x41->0b 0100 0001 b = a; // 0b 0100 0001 c = a ^ b; // 0b 0000 0000->0x00 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!0x00 -> 0x01!!0x41 -> 0x01 p && *p++ 0xCC && 0x33 -> 0x01 0x00 0x33 -> 0x01 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 %rbp movq %rsp, %rbp... popq %rbp ret 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111 Java: Car c = new Car(); c.setmiles(100); c.setgals(17); float mpg = c.getmpg(); OS: Memory & data Integers & floats Machine code & C x86 assembly Procedures & stacks 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 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 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 L01: L04: Intro, L01: Data Combinational Introduction III & Integers Logic I char hand[5]; // represents a 5-card hand char card1, card2; // two cards to compare card1 = hand[0]; card2 = hand[1];... if ( samesuitp(card1, card2) ) {... } #define SUIT_MASK 0x30 mask: a bit vector designed to achieve a desired behavior when used with a bitwise operator on another bit vector v. Here we turns 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); } returns int SUIT_MASK = 0x30 = 0 0 1 1 0 0 0 0 equivalent suit value 13

Compare Card Suits #define SUIT_MASK 0x30 L01: L04: Intro, L01: Data Combinational Introduction III & Integers Logic I mask: a bit vector designed to achieve a desired behavior when used with a bitwise operator on another bit vector v. Here we turns 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 L01: L04: Intro, L01: Data Combinational Introduction III & Integers Logic 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 L01: L04: Intro, L01: Data Combinational Introduction III & Integers Logic 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 Representation of integers: unsigned and signed Casting Arithmetic and shifting Sign extension 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 bits (only ) Only distinct bit patterns Unsigned values: 0... 1 Signed values: 1 Example: 8 bit integers (i.e., char) 128 0 128 256 + 18

Unsigned Integers Unsigned values follow the standard base 2 system Add and subtract using the normal carry and borrow rules, just in binary 63 + 8 71 Useful formula: + + + + i.e., 00111111 +00001000 01000111 1 s in a row = 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 Positives: 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 1111 1110 1010 1001 1 7 0 0000 0001 Sign and Magnitude 1000 0111 + 0 0110 + 7 0010 0011 0100 0101 + 1 + 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 1111 1110 1010 1001 1 1101 7 0 0000 0001 Sign and Magnitude 1000 0111 + 0 0110 + 7 0010 0011 0100 0101 + 1 + 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 1101 1100 1011 1111 1110 1010 1001 1 7 0 0000 0001 Sign and Magnitude 1000 0111 + 0 0110 + 7 0010 0011 0100 0101 + 1 + 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 0001 0010 + 1 0011 + 2 + 3 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 ( to +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 has weight 2, other bits have usual weights 2 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 1111 1110 1010 1001 7 1101 1 8 0000 0001 Two s Complement 1000 0111 + 0 0110 + 7 0010 0011 0100 0101 + 1 + 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 1111 1110 1010 1001 7 1101 1 8 0000 0001 Two s Complement 1000 0111 + 0 0110 + 7 0010 0011 0100 0101 + 1 + 6 + 2 + 5 + 3 + 4 27

Peer Instruction 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 Vote at http://pollev.com/justinh (A) (B) (C) (D) 4 5 11 30

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 31