
Princeton University Computer Science 217: Introduction to Programming Systems Goals of this Lecture Help you learn (or refresh your memory) about: • The binary, hexadecimal, and octal number systems Number Systems • Finite representation of unsigned integers and • Finite representation of signed integers • Finite representation of rational (floating-point) numbers Number Representation Why? • A power programmer must know number systems and data representation to fully understand C䇻s primitive data types Q: Why do computer programmers confuse Christmas and Halloween? A: Because 25 Dec = 31 Oct Primitive values and the operations on them 1 2 Agenda The Decimal Number System Name 䇾decem䇿 (Latin) ֜ ten • Number Systems Characteristics Finite representation of unsigned integers • Ten symbols Finite representation of signed integers • 0 1 2 3 4 5 6 7 8 9 • Positional Finite representation of rational (floating-point) numbers • 2945 ≠ 2495 • 2945 = (2*103) + (9*102) + (4*101) + (5*100) (Most) people use the decimal number system Why? 3 4 The Binary Number System Decimal-Binary Equivalence binary Decimal Binary Decimal Binary adjective: being in a state of one of two mutually exclusive conditions such as 0 0 16 10000 on or off, true or false, molten or frozen, presence or absence of a signal. 1 1 17 10001 From Late Latin bīnārius (“consisting of two”). 2 10 18 10010 3 11 19 10011 Characteristics 4 100 20 10100 • Two symbols 5 101 21 10101 • 0 1 6 110 22 10110 7 111 23 10111 • Positional 8 1000 24 11000 • 1010B ≠ 1100B 9 1001 25 11001 10 1010 26 11010 Most (digital) computers use the binary number system 11 1011 27 11011 12 1100 28 11100 Terminology Why? 13 1101 29 11101 • Bit: a binary digit 14 1110 30 11110 • Byte: (typically) 8 bits 15 1111 31 11111 ... ... • Nibble (or nybble): 4 bits 5 6 Integer Decimal-Binary Conversion Decimal-Binary Conversion Integer Binary to decimal: expand using positional notation Binary to decimal: expand using positional notation 5 4 3 2 1 0 5 4 3 2 1 0 100101B = (1*2 )+(0*2 )+(0*2 )+(1*2 )+(0*2 )+(1*2 ) 100101B = (1*2 )+(0*2 )+(0*2 )+(1*2 )+(0*2 )+(1*2 ) = 32 + 0 + 0 + 4 + 0 + 1 = 32 + 0 + 0 + 4 + 0 + 1 = 37 = 37 These are integers Most-significant They exist as their pure selves bit (msb) no matter how we might choose to represent them with our Least-significant fingers or toes bit (lsb) 7 8 Integer-Binary Conversion Integer-Binary Conversion Integer to binary: do the reverse Integer to binary shortcut • Determine largest power of 2 ≤ number; write template • Repeatedly divide by 2, consider remainder 37 = (?*25)+(?*24)+(?*23)+(?*22)+(?*21)+(?*20) 37 / 2 = 18 R 1 • Fill in template 18 / 2 = 9 R 0 9 / 2 = 4 R 1 Read from bottom 37 = (1*25)+(0*24)+(0*23)+(1*22)+(0*21)+(1*20) 4 / 2 = 2 R 0 to top: 100101B -32 2 / 2 = 1 R 0 5 1 / 2 = 0 R 1 -4 1 100101B -1 0 9 10 The Hexadecimal Number System Decimal-Hexadecimal Equivalence Name Decimal Hex Decimal Hex Decimal Hex 䇾hexa䇿 (Greek) ֜ six 0 0 16 10 32 20 • 䇾decem䇿 (Latin) ֜ ten 1 1 17 11 33 21 • 2 2 18 12 34 22 Characteristics 3 3 19 13 35 23 • Sixteen symbols 4 4 20 14 36 24 5 5 21 15 37 25 • 0 1 2 3 4 5 6 7 8 9 A B C D E F 6 6 22 16 38 26 • Positional 7 7 23 17 39 27 • A13DH ≠ 3DA1H 8 8 24 18 40 28 9 9 25 19 41 29 Computer programmers often use hexadecimal Why? 10 A 26 1A 42 2A • In C: 0x prefix (0xA13D, etc.) 11 B 27 1B 43 2B 12 C 28 1C 44 2C 13 D 29 1D 45 2D 14 E 30 1E 46 2E 15 F 31 1F 47 2F ... ... 11 12 Integer-Hexadecimal Conversion Binary-Hexadecimal Conversion Hexadecimal to integer: expand using positional notation Observation: 161 = 24 • Every 1 hexadecimal digit corresponds to 4 binary digits 1 0 25H = (2*16 ) + (5*16 ) = 32 + 5 Binary to hexadecimal = 37 Digit count in binary number 1010000100111101 ֜ B not a multiple of 4 A 1 3 DH Integer to hexadecimal: use the shortcut pad with zeros on left Hexadecimal to binary 37 / 16 = 2 R 5 Read from bottom A 1 3 D Discard leading zeros 2 / 16 = 0 R 2 to top: 25 H H 1010000100111101B from binary number if appropriate Is it clear why programmers often use hexadecimal? 13 14 iClicker Question The Octal Number System Q: Convert binary 101010 into decimal and hex Name 䇾octo䇿 (Latin) ֜ eight • A. 21 decimal, 1A hex Characteristics • Eight symbols B. 42 decimal, 2A hex • 0 1 2 3 4 5 6 7 • Positional • 1743O ≠ 7314O C. 48 decimal, 32 hex Computer programmers often use octal (so does Mickey!) • In C: 0 prefix (01743, etc.) D. 55 decimal, 4G hex Why? 16 Agenda Integral Types in Java vs. C Java C Number Systems unsigned char /* 8 bits */ unsigned short Unsigned types char // 16 bits Finite representation of unsigned integers unsigned (int) unsigned long Finite representation of signed integers byte // 8 bits signed char /* Note 2 */ short // 16 bits (signed) short Signed types Finite representation of rational (floating-point) numbers int // 32 bits (signed) int long // 64 bits (signed) long float Floating-point float // 32 bits double types double // 64 bits long double 1. Not guaranteed by C, but on courselab, char = 8 bits, short = 16 bits, int = 32 bits, long = 64 bits, float = 32 bits, double = 64 bits 2. Not guaranteed by C, but on courselab, char is signed To understand C, must consider representation of both 17 unsigned and signed integers 18 Representing Unsigned Integers Representing Unsigned Integers Mathematics On pretend computer Unsigned • Range is 0 to ∞ Integer Rep 0 0000 Computer programming 1 0001 • Range limited by computer䇻s word size 2 0010 Word size is n bits ֜ range is 0 to 2n – 1 3 0011 • 4 0100 • Exceed range overflow 0101 5 ֜ Typical computers today 6 0110 7 0111 32 64 • n = 32 or 64, so range is 0 to 2 – 1 or 2 – 1 (huge!) 8 1000 9 1001 Pretend computer 10 1010 • n = 4, so range is 0 to 24 – 1 (15) 11 1011 12 1100 Hereafter, assume word size = 4 13 1101 • All points generalize to word size = 64, word size = n 14 1110 15 1111 19 20 Adding Unsigned Integers Subtracting Unsigned Integers Addition Subtraction 1 Start at right column Start at right column 3 0011B 111 + 10 + 1010B Proceed leftward Proceed leftward 10 1010B -- ---- Carry 1 when necessary - 7 - 0111B Borrow when necessary 13 1101B -- ---- 3 0011B 1 7 0111B Beware of overflow 1 + 10 + 1010B 3 0011B Beware of overflow -- ---- - 10 - 1010B 1 0001B -- ---- 9 1001B How would you How would you detect overflow detect overflow 4 4 Results are mod 2 programmatically? Results are mod 2 programmatically? 21 22 Shifting Unsigned Integers Other Operations on Unsigned Ints Bitwise right shift (>> in C): fill on left with zeros Bitwise NOT (~ in C) • Flip each bit ֜ 10 >> 1 5 What is the effect 1010B 0101B arithmetically? ~10 ֜ 5 (No fair looking ahead) 1010B 0101B 2 ֜ 2 << 10 1010 0010 B B Bitwise AND (& in C) Bitwise left shift (<< in C): fillill on right i ht with ith zeros • Logical AND corresponding bits 1010 10 10 ֜ 1 >> 5 What is the effect B 0101B 1010B & 7 & 0111B Useful for setting arithmetically? -- ---- (No fair looking ahead) selected bits to 0 0010B 2 12 ֜ 2 >> 3 0011B 1100B Results are mod 24 23 24 Other Operations on Unsigned Ints iClicker Question Bitwise OR: (| in C) Q: How do you set bit “n” (counting lsb=0) of • Logical OR corresponding bits unsigned variable “u” to zero? 10 1010B | 1 | 0001B Useful for setting A. u &= (0 << n); -- ---- selected bits to 1 11 1011B B. u |= (1 << n); Bitwise exclusive OR (^ in C) • Logical exclusive OR corresponding bits C. u &= ~(1 << n); 10 1010B ^ 10 ^ 1010B x ^ x sets D. u |= ~(1 << n); -- ---- all bits to 0 0 0000B E. u = ~u ^ (1 << n); 25 Aside: Using Bitwise Ops for Arith Aside: Example C Program Can use <<, >>, and & to do some arithmetic efficiently #include <stdio.h> #include <stdlib.h> x * 2y == x << y Fast way to multiply What does it int main(void) 2 ?by a power of 2 { unsigned int n; write 12 ֜ 2>>3 = 2*3 = 4*3 • y unsigned int count = 0; x / 2 == x >> y Fast way to divide printf("Enter an unsigned integer: "); (unsigned by power of 2 if (scanf("%u", &n) != 1 3 ֜ 2<<13 = 13/22 = 13/4 • { fprintf(stderr, "Error: Expect unsigned int.\n"); y y x % 2 == x & (2 -1) Fast way to mod exit(EXIT_FAILURE); • 13%4 = 13%22 = 13&(22-1) by a power of 2 } (while (n > 0 1 ֜ 3&13 = { count += (n & 1); 13 1101B Many compilers will n = n >> 1; & 3 & 0011B } How could you -- ---- do these transformations printf("%u\n", count); express this more 1 0001B automatically! return 0; succinctly? } 27 282 Agenda Sign-Magnitude Integer Rep Number Systems -7 1111 -6 1110 Finite representation of unsigned integers -5 1101 -4 1100 Definition Finite representation of signed integers -3 1011 High-order bit indicates sign -2 1010 Finite representation of rational (floating-point) numbers -1 1001 0 ֜ positive negative ֜ 1 1000 -0 0 0000 Remaining bits indicate magnitude 1 0001 2 0010 1101B = -101B = -5 3 0011 0101B = 101B = 5 4 0100 5 0101 6 0110 7 0111 29 30 Sign-Magnitude (cont.) Ones䇻 Complement Integer Rep Integer Rep -7 1111 -7 1000 -6 1110 Computing negative -6 1001 -5 1101 neg(x) = flip high order bit of x -5 1010 -4 1100 neg(0101 ) = 1101 -4 1011 Definition -3 1011 B B -3 1100 High-order bit has weight -7 -2 1010 neg(1101B) = 0101B -2 1101 -1 1001 -1 1110 1010B = (1*-7)+(0*4)+(1*2)+(0*1) -0 1000 Pros and cons -0 1111 = -5 0 0000 0 0000 0010 = (0*-7)+(0*4)+(1*2)+(0*1) 1 0001 + easy for people to understand 1 0001 B 2 0010 + symmetric 2 0010 = 2 3 0011 - two representations of zero 3 0011 4 0100 4 0100 5 0101 - need different algorithms to add 5 0101 6 0110 signed and unsigned numbers 6 0110 7 0111 7 0111 31 32 Ones䇻 Complement (cont.) Two䇻s Complement Integer Rep Integer Rep -7 1000 Computing negative
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages9 Page
-
File Size-