<<

Princeton University Science 217: Introduction to Programming Systems Goals of this Lecture

Help you learn (or refresh your memory) about: • The binary, , and systems Number Systems • Finite representation of unsigned and • Finite representation of signed integers • Finite representation of rational (floating-point) Number Representation Why? • A power programmer must know number systems and 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 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 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) use the binary number system 11 1011 27 11011 12 1100 28 11100 Terminology Why? 13 1101 29 11101 • : a binary digit 14 1110 30 11110 • : (typically) 8 15 1111 31 11111 ...... • (or nybble): 4 bits 5 6 Decimal-Binary Conversion Decimal-Binary Conversion

Integer Binary to decimal: using positional Binary to decimal: expand using

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 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

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” ( lsb=0) of • Logical OR corresponding bits unsigned “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 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 efficiently #include #include 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 -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 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 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 -8 1000 -6 1001 neg(x) = ~x -7 1001 -5 1010 neg(0101 ) = 1010 -6 1010 -4 1011 B B -5 1011 Definition neg(1010 ) = 0101 -3 1100 B B -4 1100 High-order bit has weight -8 -2 1101 -3 1101 -1 1110 -2 1110 1010B = (1*-8)+(0*4)+(1*2)+(0*1) -0 1111 -1 1111 = -6 0 0000 Similar pros and cons to 0 0000 0010B = (0*-8)+(0*4)+(1*2)+(0*1) 1 0001 sign-magnitude 1 0001 2 0010 2 0010 = 2 3 0011 3 0011 4 0100 4 0100 5 0101 5 0101 6 0110 6 0110 7 0111 7 0111 33 34

Two’s Complement (cont.) Two’s Complement (cont.)

Integer Rep -8 1000 Computing negative Almost all computers today use two’s complement -7 1001 to represent signed integers -6 1010 neg(x) = ~x + 1 -5 1011 neg(x) = onescomp(x) + 1 • Arithmetic is easy! Is it after 1980? -4 1100 neg(0101 ) = 1010 + 1 = 1011 OK, then we’re surely -3 1101 B B B two’s complement -2 1110 neg(1011B) = 0100B + 1 = 0101B -1 1111 0 0000 1 0001 Pros and cons 2 0010 - not symmetric 3 0011 4 0100 + one representation of zero 5 0101 + same adds unsigned numbers 6 0110 or signed numbers 7 0111 Hereafter, assume two’s complement 35 36 Adding Signed Integers Subtracting Signed Integers

pos + pos pos + pos (overflow) Perform subtraction Compute two䇻s comp 11 111 or 3 0011B 7 0111B with borrows and add + 3 + 0011B + 1 + 0001B ------

6 0110B -8 1000B 1 22

pos + neg 3 0011B 3 0011B - 4 - 0100 + -4 + 1100 1111 B B How would you ------3 0011B detect overflow -1 1111B -1 1111B + -1 + 1111B ------programmatically?

2 10010B neg + neg neg + neg (overflow) 111 -5 1011B -5 1011 11 1 1 - 2 - 0010B + -2 + 1110 -3 1101B -6 1010B ------+ -2 + 1110 + -5 + 1011 B B -7 1001B -7 11001 ------

-5 11011B 5 10101B 37 38

Negating Signed Ints: Math Subtracting Signed Ints: Math

Question: Why does two’s comp arithmetic work? Answer: [–b] mod 24 = [twoscomp(b)] mod 24 And so: [a – b] mod 24 = [a + twoscomp(b)] mod 24 [–b] mod 24 = [24 – b] mod 24 [a – b] mod 24 = [24 – 1 - b + 1] mod 24 = [a + 24 – b] mod 24 = [(24 – 1 - b) + 1] mod 24 = [a + 24 – 1 – b + 1] mod 24 = [onescomp(b) + 1] mod 24 = [a + (24 - 1 – b) + 1] mod 24 = [twoscomp(b)] mod 24 = [a + onescomp(b) + 1] mod 24 = [a + twoscomp(b)] mod 24 See Bryant & O’Hallaron book for much more info

See Bryant & O䇻Hallaron book for much more info 39 40

Shifting Signed Integers Shifting Signed Integers (cont.)

Bitwise left shift (<< in C): fill on right with zeros Bitwise logical right shift: fill on left with zeros

3 <= 1 << 6 6 ֜ 1 >> 3

0011B 0110B What is the effect 0110B 0011B What is the effect arithmetically? arithmetically??? 5 <= 1 << -6 -6 ֜ 1 >> -3

1101B -1010B 1010B 0101B Bitwise arithmetic right shift: fill on left with sign bit

In C, right shift (>>) could be logical or arithmetic 3 ֜ 1 *<< 6

0110B 0011B What is the effect • Not specified by C90 standard arithmetically? • Compiler designer decides (typically it’s logical right shift) -3 ֜ 1 *<< -6

1010B 1101B Best to avoid shifting signed integers Results are mod 24 * The C language does not provide an arithmetic right shift operator, although the hardware (machine language) does. 41 42 Other Operations on Signed Ints Agenda

Bitwise NOT (~ in C) • Same as with unsigned ints Number Systems Bitwise AND (& in C) • Same as with unsigned ints Finite representation of unsigned integers Bitwise OR: (| in C) Finite representation of signed integers • Same as with unsigned ints Finite representation of rational (floating-point) numbers Bitwise exclusive OR (^ in C) • Same as with unsigned ints

Best to avoid with signed integers

43 44

Rational Numbers Floating Point Numbers

Like : e.g., c is 2.99792458 u 108 m/s Mathematics • A is one that can be expressed This has the form as the of two integers (multiplier) u (base)(power) • Unbounded range and precision In the computer, Computer science • Multiplier is called mantissa • Finite range and precision • Base is almost always 2 • Approximate using floating point number • Power is called exponent

45

IEEE Floating Point Representation Floating Point Example

Common finite representation: IEEE floating point • More precisely: ISO/IEEE 754 standard Sign (1 bit): 11000001110110110000000000000000 Using 32 bits (type float in C): • 1 ֜ negative 32-bit representation (bit: sign (0֜positive, 1֜negative 1 • • 8 bits: exponent + 127 Exponent (8 bits): • 23 bits: binary of the form 1.bbbbbbbbbbbbbbbbbbbbbbb • 10000011B = 131 • 131 – 127 = 4 Using 64 bits (type double in C): ”bit: sign (0֜positive, 1֜negative) Fraction (23 bits): also called “mantissa 1 • • 11 bits: exponent + 1023 • 1.10110110000000000000000B • 1 + (1*2-1)+(0*2-2)+(1*2-3)+(1*2-4)+(0*2-5)+(1*2- • 52 bits: binary fraction of the form 6 -7 1.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb )+(1*2 ) = 1.7109375 Number: • -1.7109375 * 24 = -27.375

47 48 When was floating-point invented? Floating Point Consequences Answer: long before computers!

mantissa “Machine epsilon”: smallest positive number you can add noun decimal part of a , 1865, from Latin mantisa “a worthless to 1.0 and get something other than 1.0 , makeweight,” perhaps a Gaulish word introduced into Latin via 7 Etruscan (cf. Old Irish meit, Welsh maint "size"). For float: H | 10 • No such number as 1.000000001 • Rule of thumb: “almost 7 digits of precision” For double: H | 2 u 1016 • Rule of thumb: “not quite 16 digits of precision” These are all relative numbers

Floating Point Consequences, cont iClicker Question

Decimal number system can Decimal Rational Q: What does the following code print? represent only some rational Approx Value .3 3/10 double sum = 0.0; numbers with finite digit count .33 33/100 .333 333/1000 int i; • Example: 1/3 cannot be represented ... for (i = 0; i < 10; i++) sum += 0.1; Binary number system can Binary Rational if (sum == 1.0) represent only some rational Approx Value printf("All good!\n"); numbers with finite digit count 0.0 0/2 else 0.01 1/4 printf("Yikes!\n"); • Example: 1/5 cannot be represented 0.010 2/8 0.0011 3/16 0.00110 6/32 A. All good! Beware of roundoff error 0.001101 13/64 • Error resulting from inexact 0.0011010 26/128 0.00110011 51/256 B. Yikes! representation ... • Can accumulate C. Code crashes • Be careful when comparing two floating-point numbers for D. Code enters an infinite loop 51

Summary

The binary, hexadecimal, and octal number systems Finite representation of unsigned integers Finite representation of signed integers Finite representation of rational (floating-point) numbers

Essential for proper understanding of • C primitive data types • • Machine language

53