representing data with bits - cs.wellesley.educs240/f18/slides/bits.pdf · why do 240 students...
TRANSCRIPT
RepresentingDatawithBits
bits,bytes,numbers,andnotation
positionalnumberrepresentation
• Base determines:– Maximumdigit(base– 1).Minimumdigitis0.– Weightofeachposition.
• Eachpositionholdsadigit.• Representedvalue=sumofallpositionvalues– Positionvalue=digitvaluexbaseposition
4
2 4 0100 10 1102 101 1002 1 0
=2x102 +4x101 +0x100
positionweight
binary=base2
Whenambiguous,subscriptwithbase:10110 Dalmatians (movie)
1012-SecondRule (folkwisdomforfoodsafety)
5
1 0 1 18 4 2 123 22 21 203 2 1 0
=1x23 +0x22 +1x21 +1x20
positionweight
irony
Powersof2:memorizeupto≥210 (inbaseten)
ex
conversionandarithmetic1910 =?2 10012 =?10
24010 =?2 110100112 =?10
1012 +10112 =?2 10010112 x210 =?2
8
Showpowers,strategies.
ex
numbersand wires
Onewirecarriesonebit.Howmanywirestorepresentagivennumber?
WhatifIwanttobuildacomputer(andnotchangethehardwarelater)?
1001
10001001
byte =8bitsSmallestunitofdatausedbyatypicalmoderncomputer
Binary 000000002 -- 111111112Decimal00010-- 25510Hexadecimal 0016 -- FF16
Programmer’shexnotation(C,etc.):0xB4 =B416
Octal(base8)alsouseful.Whydo240studentsoftenconfuseHalloweenandChristmas?
0 0 00001 1 00012 2 00103 3 00114 4 01005 5 01016 6 01107 7 01118 8 10009 9 1001A 10 1010B 11 1011C 12 1100D 13 1101E 14 1110F 15 1111 10
Whatdoyoucall4bits?
a.k.a.octet
Byte=2hexdigits!
Hexencodingpracticeex
char:representingcharacters
AC-stylestringisrepresentedbyaseriesofbytes(chars).— One-byteASCIIcodes foreachcharacter.— ASCII=AmericanStandardCodeforInformationInterchange
32 space 48 0 64 @ 80 P 96 ` 112 p33 ! 49 1 65 A 81 Q 97 a 113 q34 ” 50 2 66 B 82 R 98 b 114 r35 # 51 3 67 C 83 S 99 c 115 s36 $ 52 4 68 D 84 T 100 d 116 t37 % 53 5 69 E 85 U 101 e 117 u38 & 54 6 70 F 86 V 102 f 118 v39 ’ 55 7 71 G 87 W 103 g 119 w40 ( 56 8 72 H 88 X 104 h 120 x41 ) 57 9 73 I 89 Y 105 I 121 y42 * 58 : 74 J 90 Z 106 j 122 z43 + 59 ; 75 K 91 [ 107 k 123 {44 , 60 < 76 L 92 \ 108 l 124 |45 - 61 = 77 M 93 ] 109 m 125 }46 . 62 > 78 N 94 ^ 110 n 126 ~47 / 63 ? 79 O 95 _ 111 o 127 del
word|wərd|, n.Naturalunitofdatausedbyprocessor.– Fixedsize (e.g.32bits,64bits)• DefinedbyISA:InstructionSetArchitecture
– machineinstructionoperands– wordsize=registersize =addresssize
13
Java/Cint =4bytes:11,501,584
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
MSB:mostsignificantbit LSB:leastsignificantbit
(size inbytes)JavaDataTypeCDataType 32-bit64-bit
boolean 1 1byte char 1 1char 2 2short shortint 2 2int int 4 4float float 4 4
longint 4 8double double 8 8long longlong 8 8
longdouble 8 16
fixed-sizedatarepresentations
14
Dependsonwordsize!
bitwise operatorsBitwiseoperatorsonfixed-width bitvectors.
AND&OR| XOR^ NOT~
LawsofBooleanalgebraapplybitwise.e.g.,DeMorgan’s Law:~(A|B)=~A&~B
01101001&0101010101000001
01101001|01010101
01101001^01010101 ~01010101
01010101^01010101
15
ex
Aside:setsas bitvectorsRepresentation:n-bitvectorgivessubsetof{0,…,n–1}.
ai =1 ≡i Î A
01101001 {0,3,5,6}76543210
01010101 {0,2,4,6}76543210
BitwiseOperations SetOperations?& 01000001 {0,6} Intersection| 01111101 {0,2,3,4,5,6} Union^ 00111100 {2,3,4,5} Symmetricdifference~ 10101010 {1,3,5,7} Complement
16
ex
bitwise operatorsinC& | ^ ~ applytoanyintegral datatype
long,int,short,char, unsigned
Examples(char)~0x41 =
~0x00 =
0x69 & 0x55 =
0x69 | 0x55 =
Manybit-twiddlingpuzzlesinupcomingassignment
17
ex
logical operationsinC&&||! applytoany"integral"datatype
long,int,short,char, unsigned
0is false nonzerois true resultalways 0or1
earlytermination a.k.a.short-circuitevaluation
Examples(char)!0x41 =!0x00 =!!0x41 =
0x69 && 0x55 =0x69 || 0x55 =
18
ex
Encodeplayingcards.52cardsin4suits
Howdoweencodesuits,facecards?
Whatoperationsshouldbeeasytoimplement?GetandcomparerankGetandcomparesuit
19
Twopossiblerepresentations
52cards– 52bitswithbitcorrespondingtocardsetto1
“One-hot”encodingHardtocomparevaluesandsuitsindependentlyNotspaceefficient
4bitsforsuit,13bitsforcardvalue– 17bitswithtwosetto1
Pairofone-hotencodedvaluesEasiertocomparesuitsandvaluesindependentlySmaller,butstillnotspaceefficient
20
52bitsin2x32-bitwords
TwobetterrepresentationsBinaryencodingofall52cards– only6bitsneeded
Numbercardsuniquelyfrom0Smallerthanone-hotencodings.Hardtocomparevalueandsuit
Binaryencodingofsuit(2bits)andvalue(4bits)separately
NumbereachsuituniquelyNumbereachvalueuniquelyStillsmallEasysuit,valuecomparisons
21
low-order6bitsofabyte
suit value
mask: abitvectorthat,whenbitwiseANDed withanotherbitvectorv,turnsallbut thebitsofinterestinv to0
CompareCardSuits
char hand[5]; // represents a 5-card handchar card1, card2; // two cards to compare...if ( sameSuit(hand[0], hand[1]) ) { ... }
22
#define SUIT_MASK 0x30
int sameSuit(char card1, char card2) {return !((card1 & SUIT_MASK) ^ (card2 & SUIT_MASK));
//same as (card1 & SUIT_MASK) == (card2 & SUIT_MASK);}
0 0 1 1 0 0 0 0
suit value
mask: abitvectorthat,whenbitwiseANDed withanotherbitvectorv,turnsallbut thebitsofinterestinv to0
CompareCardValues
#define VALUE_MASK
int greaterValue(char card1, char card2) {
}
char hand[5]; // represents a 5-card handchar card1, card2; // two cards to compare...if ( greaterValue(hand[0], hand[1]) ) { ... }
23
suit value
ex
Bitshifting
24
10011001
x<<2 1001100100fillwithzeroesonright
x
logicalshiftleft2
00 10011001losebitsonright
10011001
x>>2
x
losebitsonleft
logical shiftright2
11 1 0011001arithmetic shiftright2
fillwithzeroesonleft
fillwithcopiesofMSBonleftx>>2
ShiftgotchasLogicalorarithmeticshiftright:howdowetell?C:compilerchooses
Usuallybasedontype:raincheck!
Java: >>isarithmetic,>>>islogical
Shiftann-bittypebyatleast0andnomorethann-1.C:othershiftdistancesareundefined.
anything couldhappen
Java:shiftdistanceisusedmodulonumberofbitsinshiftedtypeGivenint x:x<<34==x<<2
!!!
ShiftandMask: extractabitfieldWriteCcode:extract2nd mostsignificantbyte froma32-bitinteger.
givenshouldreturn:
26
01100001011000100110001101100100x =
00000000000000000000000001100010
Desiredbitsinleastsignificantbyte.Allotherbitsarezero.
ex