with contribtions from Dr. Bin Ren, College of William & Mary Addition, negation, mltiplication, shifting 1 Everything is bits The Decimal System and Bases Each bit is or 1 By encoding/interpreting sets of bits in varios ways Compters determine what to do (instrctions) and represent and maniplate nmbers, sets, strings, etc Why bits? Electronic Implementation Easy to store with bistable elements Reliably transmitted on noisy and inaccrate wires base 1 (decimal): digits -9 e.g., 31 1 3 1 + 1 1 1 + 1 3 + 1 + in the decimal system, 1 is the base, or radix any integer > 1 can be a base 1.1V.9V 1 base has two digits: and 1 bit binary digit 11 1 1 3 + + 1 1 + 1 111.V.V 3 Converting to Decimal Converting from Decimal to Binary Base eight (octal): digits -7 7 convert 193 to binary se a divisor of to obtain the following seqence of qotients and remainders Base 1 (hexadecimal): digits -9 and A-F 13C 1 Base (binary): digits, 1 111 In general, radix r representations se the first r chars in { 9, A...Z} and have the form d n-1 d n- d 1 d. Smming d n-1 r n-1 + d n- r n- + + d r converts to base 1. dividend qotient remainder 193 1 3 3 11 1 11 15 1 15 5 1 5 13 13 1 3 3 1 1 1 1 read remainders in reverse order 193 1 1111111 5 1
More Base Conversion Practice convert to base 1 by mltiplication of powers 11 5 ( ) 1 convert from base 1 by repeated division 3 1 ( ) converting base x to base y: convert base x to base 1 then convert base 1 to base y More Base Conversion Practice Convert from base 1 13 1 ( ) 3 and check 13 1 ( ) 1 and check Another way to convert from decimal to base n for n n n 7 n n 5 n n 3 n n 1 n 5 1 3 1 1 From LEFT TO RIGHT, ask how many and sbtract (19) 1 ( ) ( ) 1 7 Converting Between Hex and Binary Converting Between Hex and Binary chart of vales chart of vales decimal hex binary decimal hex binary 1 1 1 1 9 9 11 1 1 A 11 3 3 11 11 B 111 1 1 C 11 5 5 11 13 D 111 11 1 E 111 7 7 111 15 F 1111 to convert from binary to hex start at right of binary nmber convert each grop of digits into a hex vale e.g., convert 1111111 to hex binary: 11 111 11 hex: E C decimal hex binary decimal hex binary 1 1 1 1 9 9 11 1 1 A 11 3 3 11 11 B 111 1 1 C 11 5 5 11 13 D 111 11 1 E 111 7 7 111 15 F 1111 to convert from hex to binary replace each hex digit with its binary eqivalent e.g., convert A5 1 to binary hex: A 5 binary: 1 11 11 9 1 Octal Every Base is Base 1 1 and 3 power # of bits per hex/octal digit Binary to Hex every bits 1 hex digit Octal base digits -7 Binary to Octal Every 3 bits 1 octal digit DEC OCT HEX BIN Notes - 1 1 1 1 1 1 3 3 3 11 1 5 5 5 11 11 7 7 7 111 1 1 3 9 11 9 11 1 1 A 11 11 13 B 111 1 1 C 11 13 15 D 111 1 1 E 111 15 17 F 1111 http://cowbirdsinlove.com/3 In general, 1 X X 1 1 1 3 3 1 1 5 5 1 1 7 7 1 1 9 9 1 1 1 11 1
Other Binary Nmbers Base Nmber Representation Represent 1513 1 as 1111111111 Represent 1. 1 as 1.11111111[11] Represent 1.513 X 1 as 1.111111111 X 13 Encoding Byte Vales Byte bits Binary to 11111111 Decimal: 1 to 551 Hexadecimal 1 to FF1 sefl for writing binary vales concisely write FA1D37B1 in C as xfa1d37b xfa1d37b 1 1 1 1 3 3 11 1 5 5 11 11 7 7 111 1 9 9 11 A 1 11 B 11 111 C 1 11 D 13 111 E 1 111 F 15 1111 13 1 Example Data Representations Today: C Data Type Typical 3-bit Typical -bit x- char 1 1 1 short int long float doble long doble 1/1 Addition, negation, mltiplication, shifting pointer 15 1 Boolean Algebra General Boolean Algebras Developed by George Boole in 19th Centry Algebraic representation of logic Encode Tre as 1 and False as And A&B 1 when both A1 and B1 Or A B 1 when either A1 or B1 Operate on Bit Vectors Operations applied bitwise 1111 & 1111 11 1111 1111 111111 1111 ^ 1111 1111 ~ 1111 11 111111 1111 1111 All of the Properties of Boolean Algebra Apply Not ~A 1 when A Exclsive-Or (Xor) A^B 1 when either A1 or B1, bt not both 17 1 3
Example: Representing & Maniplating Sets Bit-Level Operations in C Representation Width w bit vector represents sbsets of {,, w 1} a j 1 if j A 1111 {, 3, 5, } 7531 1111 {,,, } 7531 Operations & Intersection 11 {, } Union 111111 {,, 3,, 5, } ^ Symmetric difference 1111 {, 3,, 5 } ~ Complement 1111 { 1, 3, 5, 7 } 19 Operations &,, ~, ^ available in C Apply to any integral data type long, int, short, char, nsigned View argments as bit vectors Argments applied bit-wise Examples (char data type) ~x1 xbe ~11 111111 ~x xff ~ 11111111 x9 & x55 x1 1111 & 1111 11 x9 x55 x7d 1111 1111 111111 Contrast: Logic Operations in C Contrast: Logic Operations in C Contrast to Bit-level Operators &&,,! View as False Anything nonzero as Tre Always retrn or 1 Early termination Examples (char data type)!x1 x!x x1!!x1 x1 Contrast to Bit-level Operators &&,,! View as False Anything nonzero as Tre Always retrn or 1 Early termination Examples (char data type)!x1 x!x C x1 programming!!x1 x1 Watch ot for && vs. & (and vs. ) one of the more common oopsies in x9 && x55 x1 x9 x55 x1 p && *p (avoids nll pointer access) x9 && x55 x1 x9 x55 x1 p && *p (avoids nll pointer access) 1 Shift Operations Left Shift: x << y Shift bit-vector x left y positions Throw away extra bits on left Fill with s on right Right Shift: x >> y Shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with s on left Arithmetic shift Replicate most significant bit on left Undefined Behavior Shift amont < or word size Argment x 111 << 3 1 Log. >> 11 Arith. >> 11 Argment x 111 << 3 1 Log. >> 11 Arith. >> 1111 Addition, negation, mltiplication, shifting 3
Finite Precision the space to store integers in a compter is limited forced to deal with finite precision different machines se a varying nmber of bits for its word size, from to 5 bits nominal size of integer and pointer data 3 and bits are the crrent preferred sizes in general, we can store n different vales with n bits 1 bit: vales ( and 1) bits: vales (, 1, 1, 11) bits: 1 vales we ve seen..15, bt no negative vales Nmber of Vales Address space depends on word size word-size-in-#bits Is it big enogh? -bit high-end machines becoming more prevalent Portability isses insensitive to sizes of different data types # bytes # bits # of vales ( #bits) low high 1 5 1 553 3 17771 3 9979 5 1.9951E+1.175E+1 7 5 7.57E+1 1.7E+19 9 7.737E+1 1 1.93E+ 11 3.95E+ 1 9 7.9E+ 13 1.E+31 1 11 5.193E+33 15 1 1.393E+3 1 1 3.E+3 5 Negative Vales so far, we ve seen the nmber of positive integers possible, bt no negative vales Encoding Integers BU(X) w 1 x i i i Two s Complement w BT(X) x w 1 w 1 x i i i common sense tells s to split the nmber of bit patterns into two grops of roghly the same size: one for positive vales and one for negative vales don t forget many ways to split these vales have been developed over the years two s complement is the most poplar nsigned only represents positive vales short int x 1513; short int y -1513; C short bytes long Decimal Hex Binary x 1513 3B D 11111 11111 y -1513 C 93 111 1111 Sign Bit For s complement, most significant bit indicates sign for nonnegative 1 for negative Sign Bit 7 Two-complement Encoding Example (Cont.) x 1513: 11111 11111 y -1513: 111 1111 Weight 1513-1513 1 1 1 1 1 1 1 1 1 1 1 3 1 3 1 1 1 1 5 1 5 51 1 51 1 1 1 1 9 1 9 19 1 19 13 1 13-37 1-37 Two s Complement Simple Conversion conversion sing a two-step process reverse the bits of the positive representation add 1 to the reslt e.g., 11 9 111111 reverse all bits 1111111 add 1-9 only one representation for 11111111 + 1 one more negative nmber than positive nmber Bryant and O Hallaron, Compter Systems: A Programmer s Sm Perspective, Third Edition 1513-1513 9 3 5
Two s Complement Alternate Conversion Nmeric Ranges alternate conversion sing a two-step process reading from right to left, copy all vales p to and inclding the first 1 reverse the remainder of the bits e.g., 111 1111 - Vales UMin UMax w 1 111 1 Vales for W 1 Two s Complement Vales TMin w 1 1 TMax w 1 1 11 1 Other Vales Mins 1 111 1 Decimal Hex Binary UMax 5535 FF FF 11111111 11111111 TMax 377 7F FF 1111111 11111111 TMin -37 1-1 -1 FF FF 11111111 11111111 31 3 Vales for Different Word Sizes W 1 3 UMax 55 5,535,9,97,95 1,,7,73,79,551,15 TMax 17 3,77,17,3,7 9,3,37,3,5,775,7 TMin -1-3,7 -,17,3, -9,3,37,3,5,775, Observations TMin TMax + 1 Asymmetric range UMax * TMax + 1 C Programming #inclde <limits.h> Declares constants, e.g., ULONG_MAX LONG_MAX LONG_MIN Vales platform specific & Signed Nmeric Vales X BU(X) BT(X) 1 1 1 1 11 3 3 1 11 5 5 11 111 7 7 1 11 9 7 11 1 111 11 5 11 1 111 13 3 111 1 1111 15 1 Eqivalence Same encodings for nonnegative vales Uniqeness Every bit pattern represents niqe integer vale Each representable integer has niqe bit encoding Can Invert Mappings UB(x) BU -1 (x) Bit pattern for nsigned integer TB(x) BT -1 (x) Bit pattern for two s comp integer 33 3 Mapping Between Signed & Addition, negation, mltiplication, shifting Two s Complement x x TU TB X BU Maintain Same Bit Pattern UT UB X BT Maintain Same Bit Pattern x Two s Complement x Mappings between nsigned and two s complement nmbers: Keep bit representations and reinterpret 35 3
Mapping Signed Mapping Signed Bits Signed Bits Signed 1 1 11 1 11 11 111 1 11 11 111 11 111 111 1111 1 3 5 7 - -7 - -5 - -3 - -1 TU UT 1 3 5 7 9 1 11 1 13 1 15 1 1 11 1 11 11 111 1 11 11 111 11 111 111 1111 1 3 5 7 - -7 - -5 - -3 - -1 +/- 1 1 3 5 7 9 1 11 1 13 1 15 37 3 Relation between Signed & Conversion Visalized Two s Complement x TB TU BU X x s Comp. Ordering Inversion Negative Big Positive UMax UMax 1 w 1 x + + + + + + x - + + + + + Large negative weight becomes Large positive weight Maintain Same Bit Pattern s Complement Range TMax 1 TMin TMax + 1 TMax Range 39 Signed vs. in C Constants By defalt are considered to be signed integers if have U as sffix U, 99759U Casting Explicit casting between signed & nsigned same as UT and TU int tx, ty; nsigned x, y; tx (int) x; y (nsigned) ty; Implicit casting also occrs via assignments and procedre calls tx x; y ty; 1 Casting Srprises Expression Evalation If there is a mix of nsigned and signed in single expression, signed vales implicitly cast to nsigned Inclding comparison operations <, >,, <, > Examples for W 3: TMIN -,17,3,, TMAX,17,3,7 Constant 1 Constant Relation Evalation U nsigned -1-1 < signed -1-1 U > nsigned 1737 1737-173 -1737-1 > signed 1737U -173-1737-1 < nsigned -1-1 - > signed (nsigned)-1-1 - > nsigned 1737 173U < nsigned 1737 1737 (int) 173U > signed 7
Smmary Casting Signed : Basic Rles Bit pattern is maintained Bt reinterpreted Can have nexpected effects: adding or sbtracting w Expression containing signed and nsigned int int is cast to nsigned!! Addition, negation, mltiplication, shifting 3 Sign Extension Task: Given w-bit signed integer x Convert it to w+k-bit integer with same vale Rle: Make k copies of sign bit: X x w 1,, x w 1, x w 1, x w,, x k copies of MSB X w X w Sign Extension Example short int x 1513; int ix (int) x; short int y -1513; int iy (int) y; Decimal Hex Binary x 1513 3B D 11111 11111 ix 1513 3B D 11111 11111 y -1513 C 93 111 1111 iy -1513 FF FF C 93 11111111 11111111 111 1111 Converting from smaller to larger integer data type C atomatically performs sign extension k 5 Smmary: Expanding, Trncating: Basic Rles Expanding (e.g., short int to int) : zeros added Signed: sign extension Both yield expected reslt Trncating (e.g., nsigned to nsigned short) /signed: bits are trncated Reslt reinterpreted : mod operation Signed: similar to mod For small nmbers yields expected behavior Addition, negation, mltiplication, shifting 7
Addition Operands: w bits Tre Sm: w+1 bits Standard Addition Fnction Ignores carry otpt Implements Modlar Arithmetic s UAdd w (, v) + v mod w + v + v Discard Carry: w bits UAdd w (, v) Visalizing (Mathematical) Integer Addition Integer Addition -bit integers, v Compte tre sm Add (, v) Vales increase linearly with and v Forms planar srface 3 1 1 Add (, v) Integer Addition 1 1 1 v 1 1 1 9 5 Visalizing Addition Two s Complement Addition Wraps Arond If tre sm w At most once Overflow UAdd (, v) Operands: w bits Tre Sm: w+1 bits + v + v Discard Carry: w bits TAdd w (, v) Tre Sm w+1 w Overflow Modlar Sm 1 1 1 1 1 1 1 1 1 1 v TAdd and UAdd have Identical Bit-Level Behavior Signed vs. nsigned addition in C: int s, t,, v; s (int) ((nsigned) + (nsigned) v); t + v Will give s t 51 5 TAdd Overflow Visalizing s Complement Addition Fnctionality Tre sm reqires w+1 bits Drop off MSB Treat remaining bits as s comp. integer 111 1 1 1 11 1 1 Tre Sm w 1 PosOver w 1 1 w 1 w NegOver TAdd Reslt 11 1 1 Vales -bit two s comp. Range from - to +7 Wraps Arond If sm w 1 Becomes negative At most once If sm < w 1 Becomes positive At most once NegOver - - - - - - - - TAdd (, v) - - - - v PosOver 53 5 9
Mltiplication Goal: Compting Prodct of w-bit nmbers x, y Either signed or nsigned Bt, exact reslts can be bigger than w bits : p to w bits Reslt range: x * y ( w 1) w w+1 + 1 Two s complement min (negative): Up to w-1 bits Reslt range: x * y ( w 1 )*( w 1 1) w + w 1 Two s complement max (positive): Up to w bits, bt only for (TMin w ) Reslt range: x * y ( w 1 ) w So, maintaining exact reslts wold need to keep expanding word size with each prodct compted is done in software, if needed e.g., by arbitrary precision arithmetic packages Mltiplication in C Operands: w bits Tre Prodct: *w bits v Discard w bits: w bits Standard Mltiplication Fnction Ignores high order w bits Implements Modlar Arithmetic UMlt w (, v) v mod w * v UMlt w (, v) 55 5 Signed Mltiplication in C Power-of- Mltiply with Shift Operands: w bits Tre Prodct: *w bits v Discard w bits: w bits Standard Mltiplication Fnction Ignores high order w bits Some of which are different for signed vs. nsigned mltiplication Lower bits are the same * v TMlt w (, v) Operation << k gives * k Both signed and nsigned Operands: w bits Tre Prodct: w+k bits k Discard k bits: w bits UMlt w (, k ) TMlt w (, k ) Examples << 3 * ( << 5) ( << 3) * Most machines shift and add faster than mltiply Compiler generates this code atomatically * k k 1 57 5 Power-of- Divide with Shift Qotient of by Power of >> k gives / k Uses logical shift k Binary Point Operands: / k 1 Division: / k. Reslt: / k Division Compted Hex Binary x 1513 1513 3B D 11111 11111 x >> 1 7.5 7 1D B 1111 11111 x >> 95.15 95 3 B 11 11111 x >> 59.5713 59 3B 11111 Addition, negation, mltiplication, shifting 59 1
Arithmetic: Basic Rles Why Shold I Use? Addition: /signed: Normal addition followed by trncate, same operation on bit level : addition mod w Mathematical addition + possible sbtraction of w Signed: modified addition mod w (reslt in proper range) Mathematical addition + possible addition or sbtraction of w Mltiplication: /signed: Normal mltiplication followed by trncate, same operation on bit level : mltiplication mod w Signed: modified mltiplication mod w (reslt in proper range) Don t se withot nderstanding implications Easy to make mistakes nsigned i; for (i cnt-; i > ; i--) a[i] + a[i+1]; Can be very sbtle #define DELTA sizeof(int) int i; for (i CNT; i-delta > ; i- DELTA)... 1 Conting Down with Why Shold I Use? (cont.) Proper way to se nsigned as loop index nsigned i; for (i cnt-; i < cnt; i--) a[i] + a[i+1]; See Robert Seacord, Secre Coding in C and C++ C Standard garantees that nsigned addition will behave like modlar arithmetic 1 UMax Even better size_t i; for (i cnt-; i < cnt; i--) a[i] + a[i+1]; Data type size_t defined as nsigned vale with length word size Code will work even if cnt UMax What if cnt is signed and <? 3 Do Use When Performing Modlar Arithmetic Mltiprecision arithmetic Do Use When Using Bits to Represent Sets Logical right shift, no sign extension Byte-Oriented Memory Organization Addition, negation, mltiplication, shifting Programs refer to data by address Conceptally, envision it as a very large array of bytes In reality, it s not, bt can think of it that way An address is like an index into that array and, a pointer variable stores an address Note: system provides private address spaces to each process Think of a process as a program being exected So, a program can clobber its own data, bt not that of others 5 11
Machine Words Any given compter has a Word Size Nominal size of integer-valed data and of addresses Until recently, most machines sed 3 bits ( bytes) as word size Limits addresses to GB ( 3 bytes) Increasingly, machines have -bit word size Potentially, cold have 1 EB (exabytes) of addressable memory That s 1. X 1 1 Machines still spport mltiple data formats Fractions or mltiples of word size Always integral nmber of bytes 7 Word-Oriented Memory Organization Addresses Specify Byte Locations Address of first byte in word Addresses of sccessive words differ by (3-bit) or (-bit) 3-bit Words Addr?? Addr?? Addr?? Addr 1?? -bit Words Addr?? Addr?? Bytes Addr. 1 3 5 7 9 1 11 1 13 1 15 Example Data Representations Byte Ordering C Data Type Typical 3-bit Typical -bit x- char 1 1 1 short int long float doble So, how are the bytes within a mlti-byte word ordered in memory? Conventions Big Endian: Sn, PPC Mac, Internet Least significant byte has highest address Little Endian: x, ARM processors rnning Android, ios, and Windows Least significant byte has lowest address long doble 1/1 pointer 9 7 Byte Ordering Example Example Variable x has -byte vale of x1357 Address given by &x is x1 Big Endian Little Endian x1 x11 x1 x13 1 3 5 7 x1 x11 x1 x13 7 5 3 1 Representing Integers int A 1513; IA3, x- D 3B Sn 3B D int B -1513; IA3, x- 93 C FF FF Sn FF FF C 93 Decimal: 1513 Binary: 11 111 11 111 Hex: 3 B D long int C 1513; IA3 D 3B x- D 3B Two s complement representation Sn 3B D 71 7 1
Examining Data Representations show_bytes Exection Example Code to Print Byte Representation of Data Casting pointer to nsigned char * allows treatment as a byte array typedef nsigned char *pointer; int a 1513; printf("int a 1513;\n"); show_bytes((pointer) &a, sizeof(int)); void show_bytes(pointer start, size_t len){ size_t i; for (i ; i < len; i++) printf( %p\tx%.x\n",start+i, start[i]); printf("\n"); } Printf directives: %p: Print pointer %x: Print Hexadecimal Reslt (Linx x-): int a 1513; x7fffb7f71dbc d x7fffb7f71dbd 3b x7fffb7f71dbe x7fffb7f71dbf 73 7 Representing Pointers Representing Strings int B -1513; int *P &B; Sn EF FF FB C IA3 AC F5 FF x- 3C 1B FE FD 7F char S[] "113"; Strings in C Represented by array of characters Each character encoded in ASCII format IA3 Sn Standard 7-bit encoding of character set 31 31 Character has code x3 3 3 Digit i has code x3+i 3 3 String shold be nll-terminated 31 31 Final character 33 33 Compatibility Byte ordering not an isse Different compilers & machines assign different locations to objects Even get different reslts each time rn program 75 7 13