Data III & Integers I CSE 351 Winter 2018

Slides:



Advertisements
Similar presentations
CSCE 212 Computer Organization Lecture 2 Data Representation.
Advertisements

CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
Assembly Language and Computer Architecture Using C++ and Java
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CENG 311 Machine Representation/Numbers
CS 270: Computer Organization Bits, Bytes, and Integers
University of Washington Announcements Lab 1, how is it going? Accounts all set? Coursecast Readings! Before class! 1.
Bits and Bytes Topics Why bits? Tell me Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
University of Washington Integers The Hardware/Software Interface CSE351 Winter 2013.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
University of Washington Roadmap car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
Lecture 4: Digital Systems & Binary Numbers (4)
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Bits, Data Types, and Operations
CSE 220 – C Programming Expressions.
Final exam: Wednesday, March 20, 2:30pm
Today’s Instructor: Randy Bryant
Memory, Data, & Addressing II CSE 351 Autumn 2017
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
University of Washington
Instructor: David Ferry
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Section 2: Integer & Floating Point Numbers
The Hardware/Software Interface CSE351 Winter 2013
CSE351: Memory and data
Announcements VM on the website! Speedometer!
Data III & Integers I CSE 351 Autumn 2016
Memory, Data, & Addressing II CSE 410 Winter 2017
Data III & Integers I CSE 351 Winter 2017
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Bits and Bytes Topics Representing information as bits
Integers II CSE 410 Winter 2017 Instructor: Teaching Assistants:
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
Bits and Bytes Topics Representing information as bits
Memory, Data, & Addressing II CSE 351 Winter 2018
Bits, Bytes, and Integers 2nd Lectures
Bits and Bytes Topics Representing information as bits
“The Class That Gives CMU Its Zip!”
CS/COE0447 Computer Organization & Assembly Language
EEL 3705 / 3705L Digital Logic Design
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Memory, Data, & Addressing II CSE 351 Summer 2018
Bits and Bytes Topics Representing information as bits
CS/COE0447 Computer Organization & Assembly Language
Integers II CSE 351 Autumn 2018 Instructor: Teaching Assistants:
Java and C CSE 351 Winter 2018 Instructor: Mark Wyse
Memory, Data, & Addressing II CSE 351 Autumn 2018
Homework Homework Continue Reading K&R Chapter 2 Questions?
Integers II CSE 351 Summer 2018 Instructor: Justin Hsia
Memory, Data, & Addressing II CSE 351 Winter 2018
Number Representation & Operators
Comp Org & Assembly Lang
Comp Org & Assembly Lang
Comp Org & Assembly Lang
Data III & Integers I CSE 351 Winter 2018
Memory, Data, & Addressing II CSE 351 Winter 2019
Operations and Arithmetic
“The Class That Gives CMU Its Zip!”
OPERATORS in C Programming
“The Class That Gives CMU Its Zip!”
Lecture 2: Bits, Bytes, Ints
Integers II CSE 351 Spring 2019 Instructor: Teaching Assistants:
OPERATORS in C Programming
Presentation transcript:

Data III & Integers I CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants: Kevin Bi Parker DeWilde Emily Furst Sarah House Waylon Huang Vinny Palaniappan http://xkcd.com/257/

Administrivia Homework 1 due tonight Lab 1 released This is considered the hardest assignment of the class by many students Some progress due Monday 1/15, Lab 1 due Friday 1/19 Keep posting to Piazza! More posts encourages participation You are not the only person with the same question! Overload Enrollments See me after class or email me ASAP w/ UW Net ID and Name! Announcements: HW 1 due tonight! Lab 1 out! Run the tests and code style checkers!!! No reason that your code does not compile Read the assigned book readings!

Examining Data Representations Code to print byte representation of data Any data type can be treated as a byte array by casting it to char C has unchecked casts !! DANGER !! void show_bytes(char *start, int len) { int i; for (i = 0; i < len; i++) printf("%p\t0x%.2x\n", start+i, *(start+i)); printf("\n"); } %p: prefix “0x” automatically printed printf directives: %p Print pointer \t Tab %x Print value as hex \n New line

Examining Data Representations Code to print byte representation of data Any data type can be treated as a byte array by casting it to char C has unchecked casts !! DANGER !! void show_bytes(char *start, int len) { int i; for (i = 0; i < len; i++) printf("%p\t0x%.2x\n", start+i, *(start+i)); printf("\n"); } %p: prefix “0x” automatically printed void show_int(int x) { show_bytes( (char *) &x, sizeof(int)); }

show_bytes Execution Example Result (Linux x86-64): Note: The addresses will change on each run (try it!), but fall in same general range int x = 12345; // 0x00003039 printf("int x = %d;\n“,x); show_int(x); // show_bytes((char *) &x, sizeof(int)); int x = 12345; 0x7fffb7f71dbc 0x39 0x7fffb7f71dbd 0x30 0x7fffb7f71dbe 0x00 0x7fffb7f71dbf 0x00

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 More fine-grained data manipulations.

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: A^B=1 when either A is 1 or B is 1, but not both NOT: ~A=1 when A is 0 and vice-versa DeMorgan’s Law: ~(A|B) = ~A & ~B ~(A&B) = ~A | ~B Also covered in CSE 311 AND OR XOR NOT & 1 | 1 ^ 1 ~ 1

General Boolean Algebras Operate on bit vectors Operations applied bitwise All of the properties of Boolean algebra apply Examples of useful operations: 𝑥 ^𝑥=0 𝑥 | 1=1, 𝑥 | 0=𝑥 01101001 & 01010101 01101001 | 01010101 01101001 ^ 01010101 ~ 01010101 01010101 ^ 01010101 00000000 Now, how do we use the boolean operators? Typically on ”bit vectors” (treat a byte, or multiple bytes) as a bit vector 01010101 | 11110000 11110101

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 Some bit-twiddling puzzles in Lab 1

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++ Avoids null pointer (0x0) access via early termination Short for: if (p) { *p++; } 0xCC && 0x33 -> 0x01 0x00 || 0x33 -> 0x01

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

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? Value comparison: War Same suit: Crazy Eights

Two possible representations 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 1 bit per suit (4), 1 bit per number (13): 2 bits set Pair of one-hot encoded values Easier to compare suits and values, but still lots of bits used Can we do better? low-order 52 bits of 64-bit word Can you think of a possible advantage to this approach? What if we wanted to represent multiple cards (i.e. a poker hand) 4 suits 13 numbers

Two better representations Binary encoding of all 52 cards – only 6 bits needed 2 6 =64≥52 Fits in one byte (smaller than one-hot encodings) How can we make value and suit comparisons easier? Separate binary encodings of suit (2 bits) and value (4 bits) Also fits in one byte, and easy to do comparisons low-order 6 bits of a byte suit value Notice the arbitrariness of binary encoding choices: A is 1 instead of 0, ordering of suits (CDHS or DCHS?) ♣ 00 ♦ 01 ♥ 10 ♠ 11 K Q J . . . 3 2 A 1101 1100 1011 ... 0011 0010 0001

Compare Card Suits returns int equivalent 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. Compare Card Suits 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 int sameSuitP(char card1, char card2) { return (!((card1 & SUIT_MASK) ^ (card2 & SUIT_MASK))); //return (card1 & SUIT_MASK) == (card2 & SUIT_MASK); } returns int equivalent SUIT_MASK = 0x30 = 1 suit value

!(x^y) equivalent to x==y 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. Compare Card Suits #define SUIT_MASK 0x30 int sameSuitP(char card1, char card2) { return (!((card1 & SUIT_MASK) ^ (card2 & SUIT_MASK))); //return (card1 & SUIT_MASK) == (card2 & SUIT_MASK); } 🃂 🃎 1 1 & & 1 SUIT_MASK 1 = = 1 1 ^ ! !(x^y) equivalent to x==y 1

Compare Card Values char hand[5]; // represents a 5-card hand mask: a bit vector designed to achieve a desired behavior when used with a bitwise operator on another bit vector v. Compare Card Values 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 = 1 suit value

🃂 🃎 Compare Card Values & & = = 210 > 1310 0 (false) mask: a bit vector designed to achieve a desired behavior when used with a bitwise operator on another bit vector v. Compare Card Values #define VALUE_MASK 0x0F int greaterValue(char card1, char card2) { return ((unsigned int)(card1 & VALUE_MASK) > (unsigned int)(card2 & VALUE_MASK)); } 🃂 🃎 1 1 & & 1 VALUE_MASK 1 = = 1 1 210 > 1310 0 (false)

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

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 2 𝑤 distinct bit patterns Unsigned values: 0 ... 2 𝑤 –1 Signed values: − 2 𝑤−1 … 2 𝑤−1 –1 Example: 8-bit integers (e.g. char) Okay, so now let’s talk some more about how we represent numbers, or integers to be specific Multiple sizes of integers, using different numbers of bytes But for each size, there are two ways of interpreting the bits… -∞ +256 +128 −128 + 𝟐 𝟖 +𝟐 𝟖−𝟏 −𝟐 𝟖−𝟏 +∞ 𝟎

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 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? 63 + 8 71 00111111 +00001000 01000111 “1’s place”, the “2’s place”, “4’s place”… An important use of unsigned values in C is pointers - there are no negative memory addresses What if we want to represent negative numbers?

Sign and Magnitude Most Significant Bit 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 = 000000002 is non-negative, because the sign bit is 0 0x7F = 011111112 is non-negative (+12710) 0x85 = 100001012 is negative (-510) 0x80 = 100000002 is negative... ... zero???

Sign and Magnitude MSB is the sign bit, rest of the bits are magnitude Drawbacks? 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 0 – 1 – 2 – 3 – 4 – 5 – 6 – 7 Sign and Magnitude To help visualize these numbers, we’ll use this number wheel See how we start with +0 – +7 on right side Then the left side, which starts with 1, are all negative, starting with -0 .. -7 Unsigned

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

Sign and Magnitude MSB is the sign bit, rest of the bits are magnitude Drawbacks: Two representations of 0 (bad for checking equality) Arithmetic is cumbersome Example: 4-3 != 4+(-3) Negatives “increment” in wrong direction! 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 0 – 1 – 2 – 3 – 4 – 5 – 6 – 7 4 - 3 1 0100 - 0011 0001 4 + -3 -7 0100 + 1011 1111 Sign and Magnitude Arithmetic is cumbersome 4 – 3 should be 1, as we see on the left But 4 + -3 should be equivalent, but if we naively add the two together, it doesn’t work We have to make sure we change all plus-negatives to subtractions So, how do we solve these problems? It turns out there’s a really clever other way of interpreting the MSB ✓ ✗

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

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

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

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 ) 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 8 – 7 – 6 – 5 – 4 – 3 – 2 – 1 Two’s Complement

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 -4 -5 11 -3 We’re lost… A is not valid in the 3 systems we talked about today B is valid in Two’s Complement C is valid in Unsigned D is valid in Sign and Magnitude

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

Shift Operations Left shift (x<<n) bit vector x by n positions Throw away (drop) extra bits on left Fill with 0s on right Right shift (x>>n) bit-vector x by n positions Throw away (drop) extra bits on right Logical shift (for unsigned values) Fill with 0s on left Arithmetic shift (for signed values) Replicate most significant bit on left Maintains sign of x Book reading has more detail Useful operator when working with bit representations Especially Lab 1

Shift Operations Left shift (x<<n) Right shift (x>>n) 0010 0010 x<<3 0001 0000 logical: x>>2 0000 1000 arithmetic: Left shift (x<<n) Fill with 0s on right Right shift (x>>n) Logical shift (for unsigned values) Fill with 0s on left Arithmetic shift (for signed values) Replicate most significant bit on left Notes: Shifts by n<0 or n≥w (bit width of x) are undefined In C: behavior of >> is determined by compiler In gcc / C lang, depends on data type of x (signed/unsigned) In Java: logical shift is >>> and arithmetic shift is >> x 1010 0010 x<<3 0001 0000 logical: x>>2 0010 1000 arithmetic: 1110 1000