integer representation but first encode deck of cards
play

Integer Representation But first, encode deck of cards. - PowerPoint PPT Presentation

9/8/15 WELLESLEY CS 240 Integer Representation But first, encode deck of cards. Representation of integers: unsigned and signed 52 cards in 4 suits How do we encode


  1. 9/8/15 WELLESLEY CS 240 Integer ¡Representation But ¡first, ¡encode ¡deck ¡of ¡cards. Representation ¡of ¡integers: ¡unsigned ¡and ¡signed 52 ¡cards ¡in ¡4 ¡suits How ¡do ¡we ¡encode ¡suits, ¡face ¡cards? Sign ¡extension What ¡operations ¡should ¡be ¡easy ¡to ¡implement? Arithmetic ¡ and ¡shifting Get ¡and ¡compare ¡rank Casting Get ¡and ¡compare ¡suit 2 3 Two ¡possible ¡representations Two ¡better ¡representations Binary ¡ encoding ¡of ¡all ¡ 52 ¡cards ¡– only ¡6 ¡bits ¡needed 52 ¡cards ¡– 52 ¡bits ¡with ¡bit ¡corresponding ¡to ¡card ¡set ¡to ¡1 Number ¡each ¡card Fits ¡in ¡one ¡byte 52 ¡bits ¡in ¡2 ¡x ¡32-­‑bit ¡words low-­‑order ¡6 ¡bits ¡of ¡a ¡byte “One-­‑hot” ¡encoding Smaller ¡than ¡one-­‑hot ¡encodings. Two ¡32-­‑bit ¡words How ¡can ¡we ¡make ¡value ¡and ¡suit ¡comparisons ¡easier? Hard ¡to ¡compare ¡values ¡and ¡suits Large ¡number ¡of ¡bits ¡required Binary ¡ encoding ¡of ¡suit ¡(2 ¡bits) ¡and ¡value ¡(4 ¡bits) ¡separately Number ¡each ¡suit 4 ¡bits ¡for ¡suit, ¡13 ¡bits ¡for ¡card ¡value ¡– 17 ¡bits ¡with ¡two ¡set ¡to ¡1 Number ¡each ¡value suit value Fits ¡in ¡one ¡byte Pair ¡of ¡one-­‑hot ¡encoded ¡values Easy ¡suit, ¡value ¡comparisons Fits ¡in ¡one ¡32-­‑bit ¡word Easier ¡to ¡compare ¡suits ¡and ¡values Still ¡space-­‑inefficient 4 5 1

  2. 9/8/15 WELLESLEY CS 240 Compare ¡Card ¡Suits Compare ¡Card ¡Values mask: a ¡bit ¡vector ¡that, ¡when ¡bitwise ¡ mask: a ¡bit ¡vector ¡that, ¡when ¡bitwise ¡ ANDed with ¡another ¡bit ¡vector ¡ v , ¡turns ¡ ANDed with ¡another ¡bit ¡vector ¡ v , ¡turns ¡ all ¡ but the ¡bits ¡of ¡interest ¡in ¡ v to ¡0 all ¡ but the ¡bits ¡of ¡interest ¡in ¡ v to ¡0 static final SUIT_MASK = 0x30; static final VALUE_MASK = 0x0F; works ¡even ¡if ¡value ¡ is ¡stored ¡in ¡high ¡bit s boolean sameSuit(char card1, char card2) { boolean greaterValue(char card1, char card2) { return 0 == ((card1 & SUIT_MASK) ^ (card2 & SUIT_MASK)); return (card1 & VALUE_MASK) > (card2 & VALUE_MASK)); // return (card1 & SUIT_MASK) == (card2 & SUIT_MASK); } } 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 SUIT_MASK = 0x30 = equivalent VALUE_MASK = 0x0F = suit value suit value char hand[5]; // represents a 5-card hand char hand[5]; // represents a 5-card hand char card1, card2; // two cards to compare char card1, card2; // two cards to compare card1 = hand[0]; card1 = hand[0]; card2 = hand[1]; card2 = hand[1]; ... ... if ( sameSuit(card1, card2) ) { ... } if ( greaterValue(card1, card2) ) { ... } 6 7 Encoding ¡Integers in ¡a ¡fixed ¡number ¡of ¡bits Unsigned ¡modular ¡arithmetic, ¡overflow Positional ¡representation, ¡ Two ¡flavors: Examples ¡in ¡4-­‑bit ¡unsigned ¡representation. fixed ¡# ¡of ¡positions. unsigned ( ⊂ ) – non-­‑negatives ¡only 15 0 signed ( ⊂ ) – both ¡negatives ¡and ¡non-­‑negatives 11 ¡+ ¡2 ¡= ¡ 13 ¡+ ¡5 ¡= ??? 14 1 1111 0000 fixed-­‑width representations: ¡ W ¡bits wide ¡(W ¡for ¡word ¡or ¡width) 1110 0001 13 2 1101 0010 Only ¡2 W distinct ¡bit ¡patterns... 12 3 1100 0011 Cannot ¡represent ¡all ¡the ¡integers 1011 0100 11 4 Unsigned ¡values: ¡0 ¡... ¡2 W -­‑1 1010 0101 Signed ¡values: ¡-­‑2 W -­‑1 ... ¡2 W -­‑1 -­‑1 10 5 1001 0110 1000 0111 9 6 Terminology: 8 7 x + y in ¡N-­‑bit ¡unsigned ¡arithmetic ¡ is ¡ (x ¡+ ¡y) ¡mod ¡2 N in ¡math “Most-­‑significant” ¡or ¡ “Least-­‑significant” ¡or ¡ “high-­‑order” ¡bit(s) “low-­‑order” ¡bit(s) unsigned ¡overflow = ¡"wrong" ¡answer ¡= ¡wrap-­‑around 0110010110101001 MSB LSB = ¡carry ¡1 ¡out ¡of ¡MSB = ¡math ¡answer ¡too ¡big ¡to ¡fit 9 11 2

  3. 9/8/15 WELLESLEY CS 240 !!! Signed ¡Integers: ¡Sign-­‑Magnitude? Overflow: ¡Unsigned Addition ¡ overflows if ¡and ¡only ¡if ¡a ¡carry ¡bit ¡is ¡dropped. Most-­‑significant ¡ bit ¡(MSB) ¡is ¡ sign ¡bit 0 ¡means ¡non-­‑negative 1 ¡ ¡1 ¡ ¡1 15 1111 1 ¡means ¡negative 15 0 Rest ¡of ¡bits ¡are ¡an ¡unsigned ¡magnitude 14 1 1111 0000 + ¡2 + ¡0010 1110 0001 13 2 17 10001 1101 0010 8-­‑bit ¡sign-­‑and-­‑magnitude: 12 3 1100 0011 Overflow. 0x00 ¡= ¡00000000 ¡represents ¡_____________ 1 1011 0100 11 4 0x7F ¡= ¡01111111 ¡represents ¡_____________ 1010 0101 0x85 ¡= ¡10000101 ¡represents ¡_____________ 10 5 1001 0110 0x80 ¡= ¡10000000 ¡represents ¡_____________ 1000 0111 9 6 8 7 Max ¡and ¡min ¡for ¡N-­‑bit ¡sign-­‑magnitude? Modular ¡Arithmetic What ¡is ¡weird ¡about ¡sign-­‑magnitude ¡ representation? 12 13 !!! Sign-­‑Magnitude ¡Negatives Two’s ¡complement ¡representation for ¡signed ¡integers Another ¡problem: ¡ cumbersome ¡arithmetic. Example: 4 ¡-­‑ 3 ¡!= ¡4 ¡+ ¡(-­‑3) w -­‑bit ¡representation _ … _ … _ _ _ _ 0100 +1011 2 i … – 7 + ¡0 -­‑2 ( w -­‑1) … 2 3 2 2 2 1 2 0 weight – 6 + ¡1 1111 0000 w -­‑1 … i … 3 2 1 0 1110 0001 position – 5 + ¡2 1101 0010 – 4 + ¡3 What ¡about ¡zero? 1100 0011 Positional ¡representation, ¡ but 1011 0100 – 3 + ¡4 1010 0101 most ¡significant ¡position ¡ has ¡ negative ¡ weight . – 2 + ¡5 1001 0110 1000 0111 – 1 + ¡6 – 0 + ¡7 Maybe ¡sign-­‑magnitude ¡is ¡not ¡such ¡a ¡good ¡idea... 14 17 3

  4. 9/8/15 WELLESLEY CS 240 8-­‑bit ¡representations 4-­‑bit ¡unsigned ¡ ¡vs. ¡ ¡4-­‑bit ¡two’s ¡complement 1 ¡ ¡ ¡0 ¡ ¡ ¡1 ¡ ¡ ¡1 0 ¡0 ¡0 ¡0 ¡1 ¡0 ¡0 ¡1 1 ¡0 ¡0 ¡0 ¡0 ¡0 ¡0 ¡1 1 x ¡2 3 ¡ + ¡0 x ¡2 2 + 1 x ¡2 1 + 1 x ¡2 0 1 x ¡ -­‑2 3 + ¡0 x ¡2 2 + 1 x ¡2 1 + 1 x ¡2 0 11 (math) ¡difference ¡= ¡16 = ¡2 4 -­‑5 – 1 0 15 0 14 1 – 2 + ¡1 1111 0000 1111 0000 1110 0001 1110 0001 13 2 – 3 + ¡2 1 ¡1 ¡1 ¡1 ¡1 ¡1 ¡1 ¡1 0 ¡0 ¡1 ¡0 ¡0 ¡1 ¡1 ¡1 1101 0010 1101 0010 12 3 – 4 + ¡3 1100 0011 1100 0011 1011 0100 1011 0100 11 4 – 5 + ¡4 1010 0101 1010 0101 – 6 + ¡5 10 5 1001 0110 1001 0110 1000 0111 1000 0111 9 6 – 7 + ¡6 8 7 – 8 + ¡7 18 20 Two’s ¡complement: ¡addition ¡Just ¡Works Overflow: ¡Two’s ¡Complement 1 1 ¡ ¡1 Addition ¡ overflows if ¡and ¡only ¡if ¡the ¡inputs ¡have ¡the ¡same ¡sign ¡but ¡the ¡output ¡does ¡not. 2 0010 -­‑2 1110 if ¡and ¡only ¡if ¡the ¡carry ¡in ¡and ¡out ¡of ¡the ¡sign ¡bit ¡differ . 1 ¡ ¡1 ¡ ¡1 + ¡3 + ¡0011 + ¡-­‑3 + ¡1101 -­‑1 1111 – 1 0 – 2 + ¡1 5 0101 -­‑5 11011 1111 0000 + ¡2 + ¡0010 1110 0001 – 3 + ¡2 – 1 0 1 10001 1101 0010 – 2 + ¡1 1111 0000 – 4 + ¡3 1100 0011 1110 0001 – 3 + ¡2 No ¡overflow. 1101 0010 1011 0100 – 4 + ¡3 – 5 + ¡4 1100 0011 1 ¡ ¡1 1 ¡ ¡1 ¡ ¡1 1010 0101 6 0110 2 0010 -­‑2 1110 1011 0100 – 5 + ¡4 – 6 + ¡5 1001 0110 1010 0101 1000 0111 + ¡3 + ¡0011 + ¡-­‑3 + ¡1101 + ¡3 + ¡0011 – 6 + ¡5 – 7 + ¡6 1001 0110 1000 0111 – 8 + ¡7 – 7 + ¡6 9 1001 -­‑1 1111 1 10001 – 8 + ¡7 Modular ¡Arithmetic -­‑7 Overflow. Modular ¡Arithmetic Some ¡CPUs ¡raise ¡exceptions ¡ on ¡overflow C ¡and ¡Java ¡cruise ¡along ¡silently... ¡Oops? 21 22 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend