Number Systems Number of symbols base of the
Number Systems Number of symbols = base of the system n Most intuitive -- base 10 (decimal system) l counting on fingertips l symbols -- 0 1 2 3 4 5 6 7 8 9 n Computers use base 2 (binary system) l only two symbols - 0 / 1 l Many physical reasons for choosing n Other useful bases l 8 - octal l 16 - hexadecimal – 1– 52011
Representing a number in base B General expression : A number U 3 U 2 U 1 U 0 in base B is effectively U 3 x B 3 + U 2 x B 2 + U 1 x B 1 + U 0 x B 0 Examples : 234 in base 10 is 2 x 102 + 3 x 101 + 4 x 100 1011 in base 2 is 1 x 22 + 0 x 22 + 1 x 21 + 1 x 20 = 1 x 101 + 1 x 100 in base 10 – 2– 52011
Why Don’t Computers Use Base 10? Implementing Electronically n n n Hard to store Hard to transmit Messy to implement digital logic functions Binary Electronic Implementation n Easy to store with bistable elements Reliably transmitted on noisy and inaccurate wires Straightforward implementation of arithmetic functions 0 1 0 3. 3 V 2. 8 V 0. 5 V 0. 0 V – 3– 52011
Octal and hexadecimal Octal uses 0 -7 for representing numbers Hexadecimal uses 0 -9, a, b, c, d, e and f as digits a = decimal 10, b=decimal 11 and so on They are useful because translation from binary is easy Consider 10110101 rewrite as 101 100 110 101 and you get 5465 in octal rewrite as 1011 0101 and you get b 35 in hex Much more readable in octal and hex than in binary Also much easier to calculate equivalent decimal value It will really pay to learn to interpret hex numbers in this course – 4– 52011
Encoding Byte Values Byte = 8 bits n n n Binary 00002 Decimal: 010 Hexadecimal 0016 to to to 11112 25510 FF 16 l Base 16 number representation l Use characters ‘ 0’ to ‘ 9’ and ‘A’ to ‘F’ l Write FA 1 D 37 B 16 in C as 0 x. FA 1 D 37 B o Or 0 xfa 1 d 37 b – 5– al y im ar x c n He De Bi 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 52011
Machine Words Machine Has “Word Size” n Nominal size of integer-valued data l Including addresses n Most current machines are 32 bits (4 bytes) or 64 bits l 32 bits Limits addresses to 4 GB l Becoming too small for memory-intensive applications n High-end systems are 64 bits (8 bytes) l Potentially address 1. 8 X 1019 bytes n Machines support multiple data formats l Fractions or multiples of word size l Always integral number of bytes – 6– 52011
Data Representations Sizes of C Objects (in Bytes) n C Data Type Compaq Alpha l int l long int l char l short l float l double l long double l char * 4 8 1 2 4 8 8 8 Typical 32 -bit Intel IA 32 4 4 1 2 4 8 8 4 4 4 1 2 4 8 10/12 4 o Or any other pointer – 7– 52011
Byte Ordering How should bytes within multi-byte word be ordered in memory? Conventions n Sun’s, Mac’s are “Big Endian” machines l Least significant byte has highest address n Alphas, PC’s are “Little Endian” machines l Least significant byte has lowest address – 8– 52011
Byte Ordering Example Big Endian n Least significant byte has highest address Little Endian n Least significant byte has lowest address Example n n Variable x has 4 -byte representation 0 x 01234567 Address given by &x is 0 x 100 Big Endian 0 x 100 0 x 101 0 x 102 0 x 103 01 Little Endian 45 67 0 x 100 0 x 101 0 x 102 0 x 103 67 – 9– 23 45 23 01 52011
Reading Byte-Reversed Listings Disassembly n n Text representation of binary machine code Generated by program that reads the machine code Example Fragment Address 8048365: 8048366: 804836 c: Instruction Code 5 b 81 c 3 ab 12 00 00 83 bb 28 00 00 Assembly Rendition pop %ebx add $0 x 12 ab, %ebx cmpl $0 x 0, 0 x 28(%ebx) Deciphering Numbers n n – 10 – Value: Pad to 4 bytes: Split into bytes: Reverse: 0 x 12 ab 0 x 000012 ab 00 00 12 ab ab 12 00 00 52011
Representing Integers int A = 15213; int B = -15213; long int C = 15213; Linux/Alpha A 6 D 3 B 00 00 Linux/Alpha B 93 C 4 FF FF – 11 – Decimal: 15213 Binary: 0011 1011 0110 1101 Hex: 3 B 6 D Sun A Linux C Alpha C Sun C 00 00 3 B 6 D 6 D 3 B 00 00 00 3 B 6 D Sun B FF FF C 4 93 Two’s complement representation (Covered later) 52011
Alpha P Representing Pointers int B = -15213; int *P = &B; Alpha Address Hex: 1 Binary: Sun P EF FF FB 2 C F F F C A 0 0001 1111 1111 1100 1010 0000 A 0 FC FF FF 01 00 00 00 Sun Address Hex: Binary: E F F B 2 C 1110 1111 1011 0010 1100 Linux P D 4 F 8 FF BF Linux Address Hex: Binary: B F F 8 D 4 1011 1111 1000 1101 0100 Different compilers & machines assign different locations to objects – 12 – 52011
Representing Floats Float F = 15213. 0; Linux/Alpha F 00 B 4 6 D 46 Sun F 46 6 D B 4 00 IEEE Single Precision Floating Point Representation Hex: Binary: 15213: 4 6 6 D B 4 0 0 0100 0110 1101 1011 0100 0000 1110 1101 1011 01 Not same as integer representation, but consistent across machines Can see some relation to integer representation, but not obvious – 13 – 52011
Representing Strings char S[6] = "15213"; Strings in C n n Represented by array of characters Each character encoded in ASCII format l Standard 7 -bit encoding of character set l Other encodings exist, but uncommon l Character “ 0” has code 0 x 30 o Digit i has code 0 x 30+i n String should be null-terminated Linux/Alpha S Sun S 31 35 32 31 33 00 l Final character = 0 Compatibility n Byte ordering not an issue l Data are single byte quantities n – 14 – Text files generally platform independent 52011 l Except for different conventions of line termination character(s)!
Boolean Algebra Developed by George Boole in 19 th Century n Algebraic representation of logic l Encode “True” as 1 and “False” as 0 And n Or A&B = 1 when both A=1 and B=1 Not n – 15 – ~A = 1 when A=0 n A|B = 1 when either A=1 or B=1 Exclusive-Or (Xor) n A^B = 1 when either A=1 or B=1, but not both 52011
Application of Boolean Algebra Applied to Digital Systems by Claude Shannon n n 1937 MIT Master’s Thesis Reason about networks of relay switches l Encode closed switch as 1, open switch as 0 A&~B A Connection when ~B A&~B | ~A&B ~A&B – 16 – = A^B 52011
Algebra Integer Arithmetic n n n Addition is “sum” operation Multiplication is “product” operation – is additive inverse 0 is identity for sum 1 is identity for product Boolean Algebra n n n – 17 – {0, 1}, |, &, ~, 0, 1 forms a “Boolean algebra” Or is “sum” operation And is “product” operation ~ is “complement” operation (not additive inverse) 0 is identity for sum 1 is identity for product 52011
Boolean Algebra Integer Ring n n n Commutativity A|B = B|A A&B = B&A Associativity (A | B) | C = A | (B | C) (A & B) & C = A & (B & C) Product distributes over sum A & (B | C) = (A & B) | (A & C) Sum and product identities A|0 = A A&1 = A Zero is product annihilator A&0 = 0 Cancellation of negation ~ (~ A) = A – 18 – A+B = B+A A*B = B*A (A + B) + C = A + (B + C) (A * B) * C = A * (B * C) A * (B + C) = A * B + B * C A+0 = A A*1 =A A*0 = 0 – (– A) = A 52011
Boolean Algebra Integer Ring n n Boolean: Sum distributes over product A | (B & C) = (A | B) & (A | C) A + (B * C) (A + B) * (B + C) Boolean: Idempotency A|A = A A +A A l “A is true” or “A is true” = “A is true” n A&A = A Boolean: Absorption A | (A & B) = A A *A A A + (A * B) A l “A is true” or “A is true and B is true” = “A is true” n A & (A | B) = A Boolean: Laws of Complements A | ~A = 1 A * (A + B) A A + –A 1 l “A is true” or “A is false” n Ring: Every element has additive inverse A | ~A 0 A + –A = 0 – 19 – 52011
Boolean Ring n n n Properties of & and ^ {0, 1}, ^, &, , 0, 1 Identical to integers mod 2 is identity operation: (A) = A A^A=0 Property n n n n n – 20 – Commutative sum Commutative product Associative sum Associative product Prod. over sum 0 is sum identity 1 is prod. identity 0 is product annihilator Additive inverse Boolean Ring A^B = B^A A&B = B&A (A ^ B) ^ C = A ^ (B ^ C) (A & B) & C = A & (B & C) A & (B ^ C) = (A & B) ^ (B & C) A^0 = A A&1 = A A&0=0 A^A = 0 52011
Relations Between Operations De. Morgan’s Laws n Express & in terms of |, and vice-versa l A & B = ~(~A | ~B) o A and B are true if and only if neither A nor B is false l A | B = ~(~A & ~B) o A or B are true if and only if A and B are not both false Exclusive-Or using Inclusive Or l A ^ B = (~A & B) | (A & ~B) o Exactly one of A and B is true l A ^ B = (A | B) & ~(A & B) o Either A is true, or B is true, but not both – 21 – 52011
General Boolean Algebras Operate on Bit Vectors n Operations applied bitwise 01101001 & 0101 01000001 01101001 | 0101 01111101 01101001 ^ 0101 00111100 ~ 0101 10101010 All of the Properties of Boolean Algebra Apply – 22 – 52011
Bit-Level Operations in C Operations &, |, ~, ^ Available in C n Apply to any “integral” data type l long, int, short, char n n View arguments as bit vectors Arguments applied bit-wise Examples (Char data type) n ~0 x 41 --> ~010000012 0 x. BE n ~0 x 00 --> 101111102 ~00002 0 x. FF n 0 x 69 & 0 x 55 --> 11112 --> 0 x 41 011010012 & 01012 --> 010000012 n 0 x 69 | 0 x 55 --> 0 x 7 D 011010012 | 01012 --> 011111012 – 23 – 52011
Contrast: Logic Operations in C Contrast to Logical Operators n &&, ||, ! l l View 0 as “False” Anything nonzero as “True” Always return 0 or 1 Early termination Examples (char data type) n n n – 24 – !0 x 41 --> !0 x 00 --> !!0 x 41 --> 0 x 00 0 x 01 0 x 69 && 0 x 55 --> 0 x 01 0 x 69 || 0 x 55 --> 0 x 01 p && *p (avoids null pointer access) 52011
Shift Operations Left Shift: n x << y Shift bit-vector x left y positions l Throw away extra bits on left l Fill with 0’s on right Right Shift: x >> y n Shift bit-vector x right y positions l Throw away extra bits on right n Logical shift l Fill with 0’s on left n Arithmetic shift l Replicate most significant bit on Argument x 01100010 << 3 00010000 Log. >> 2 00011000 Arith. >> 2 00011000 Argument x 10100010 << 3 00010000 Log. >> 2 00101000 Arith. >> 2 11101000 right l Useful with two’s complement integer representation – 25 – 52011
Cool Stuff with Xor n n void funny(int *x, int *y) { *x = *x ^ *y; /* #1 */ *y = *x ^ *y; /* #2 */ *x = *x ^ *y; /* #3 */ } Bitwise Xor is form of addition With extra property that every value is its own additive inverse A^A=0 – 26 – *x *y Begin A B 1 A^B B 2 A^B (A^B)^B = A 3 (A^B)^A = B A End B A 52011
Storing negative integers sign-magnitude n n n of n bits in the word, the most significant bit is sign 1 indicates negative number, 0 a positive number remaining n-1 bits determine the magnitude 0 has two representations The range of valid values is -(2(n-1)-1) to (2(n-1)-1) l Examples o 8 bit words can represent -127 to +127 o 16 bit words can represent -32767 to +32767 n Advantages l Very straightforward l Simple to understand n Disadvantage l different machinery for addition and subtraction l multiple representations of zero. – 27 – 52011
One’s complement n n n The most significant bit is effectively sign bit The range of numbers the same as signed magnitude Positive numbers stored as such Negative numbers are bit-wise inverted Example l 4 bit storage – range -7 to +7 l l – 28 – o -7 = 1000, +7 = 0111, +3 = 0011, -4 = 1011 8 bit storage – range = -127 t 0 128 Zero has two representations : o 0000 o 1111 Hardly ever used for actual storage Useful for understanding 2’s compliment and some other operations 52011
Two’s Complement Unsigned Two’s Complement short int x = 15213; short int y = -15213; n Sign Bit C short 2 bytes long Sign Bit n For 2’s complement, most significant bit indicates sign l 0 for nonnegative l 1 for negative – 29 – 52011
Two’s compliment is effectively one’s compliment with a “ 1” added to it. n It is a number’s additive inverse l Number added to its own two’s compliment results in zero n n Same circuitry can do arithmetic operations for positive and negative numbers The range is (-2 n-1) to (2 n-1 -1) Example : 13 + (-6) 13 in 4 bit binary is 1101 6 is 0110 It’s one’s complement is 1001 Two’s complement is 1010 Add 1101 and 1010 in 4 bits, ignoring the carry The answer is 0111, with carry of 1, which in decimal is 7 – 30 – 52011
Power-of-2 Multiply with Shift Operation n u << k gives u * 2 k n Both signed and unsigned Operands: w bits * True Product: w+k bits u · 2 k Discard k bits: w bits u k • • • 2 k 0 • • • 0 1 0 • • • 0 0 • • • UMultw(u , 2 k) • • • 0 • • • 0 0 TMultw(u , 2 k) Examples n u << 3 == u << 5 - u << 3 == n Most machines shift and add much faster than multiply n – 31 – u * 8 u * 24 52011
Unsigned Power-of-2 Divide with Shift Quotient of Unsigned by Power of 2 n u >> k gives u / 2 k n Uses logical shift Operands: Division: Result: – 32 – k u / 2 k • • • Binary Point • • • 0 • • • 0 1 0 • • • 0 0 u / 2 k 0 • • • u / 2 k 0 • • • 52011
Signed Power-of-2 Divide with Shift Quotient of Signed by Power of 2 n x >> k gives x / 2 k n Uses arithmetic shift Rounds wrong direction when u k< 0 n Operands: Division: Result: – 33 – x / 2 k Round. Down(x / 2 k) • • • Binary Point • • • 0 • • • 0 1 0 • • • 0 0 0 • • • 52011
Correct Power-of-2 Divide Quotient of Negative Number by Power of 2 n n Want x / 2 k (Round Toward 0) Compute as (x+2 k-1)/ 2 k l In C: (x + (1<<k)-1) >> k l Biases dividend toward 0 Case 1: No rounding u Dividend: +2 k +– 1 k 1 / 2 k u / 2 k 0 • • • 0 0 1 • • • 1 1 1 Divisor: • • • 1 • • • 1 1 Binary Point 0 • • • 0 1 0 • • • 0 0 0 • • • 1 1 • • • . 1 • • • 1 1 Biasing has no effect – 34 – 52011
Correct Power-of-2 Divide (Cont. ) Case 2: Rounding k Dividend: x +2 k +– 1 1 • • • 0 • • • 0 0 1 • • • 1 1 1 • • • Incremented by 1 Divisor: / 2 k x / 2 k 0 • • • 0 1 0 • • • 0 0 0 • • • 1 1 Biasing adds 1 to final result – 35 – Binary Point • • • Incremented by 1 52011
Fractional Binary Numbers 2 i 2 i– 1 4 2 1 bi bi– 1 • • • b 2 b 1 b 0. b– 1 b– 2 b– 3 1/2 1/4 1/8 • • • b–j • • • 2–j Representation – 36 – n Bits to right of “binary point” represent fractional powers of 2 n Represents rational number: 52011
Frac. Binary Number Examples Value 5 -3/4 2 -7/8 63/64 Representation 101. 112 10. 1112 0. 1111112 Observations Divide by 2 by shifting right n Multiply by 2 by shifting left n Numbers of form 0. 111111… 2 just below 1. 0 n l 1/2 + 1/4 + 1/8 + … + 1/2 i + … 1. 0 – 37 – 52011
Representable Numbers Limitation Can only exactly represent numbers of the form x/2 k n Other numbers have repeating bit representations n Value 1/3 1/5 1/10 – 38 – Representation 0. 010101[01]… 2 0. 00110011[0011]… 2 0. 000110011[0011]… 2 52011
- Slides: 38