300 likes | 458 Views
Intro to Computer Math. Binary – Octal - Hexadecimal. B 1011 0100 1100 0x B 4 C. Binary: What is it???. Electronics: 0,1 is easy numbering system Data is stored as 32 or 64 bits of 0s and 1s: Registers, Memory, Disk, CDs, DVDs, ROM, Data Communications Bit = 1 binary digit: 1 or 0
E N D
Intro to Computer Math Binary – Octal - Hexadecimal B 1011 0100 1100 0x B 4 C
Binary: What is it??? • Electronics: 0,1 is easy numbering system • Data is stored as 32 or 64 bits of 0s and 1s: • Registers, Memory, Disk, CDs, DVDs, ROM, Data Communications • Bit = 1 binary digit: 1 or 0 • Boolean: True = 1, False = 0 • ASCII: 8 bits • Number = 8 or 16 or 32 or 64 bits
Octal – Hexadecimal: What is it? • 1s & 0s are tedious: • 1010 0101 1111 0011 1100 0000 1001 0110 1110 0001 • Hexadecimal: 0x a 5 f 3 c 0 9 6 e 1 • Octal: 5 1 3 7 1 7 0 0 4 5 5 6 0 4 • Octal and Hexadecimal are compatible; powers of 2; easy representations of binary • Numbering Systems: • Base 2: Binary • Base 8: Octal • Base 10: Decimal • Base 16: Hexadecimal
Why is it Useful? • Uses include: • UNIX Permissions: • 740 = Self: Read, Write, Execute; Group: Read; Others: Null • Data Communications: Meaning of: • Transmission: 7E 24 35 37 3a b3 fd 00 23 33 fa 4c 5d da 98 62 31 11 a2 7f 7e • Routing tables: 4a.62.33.c0/26 • Data: Setting flags, clearing bits, efficient storage • Engineering: Designing and working with memory • Debugging: Core dumps
Look at Base 10 - Decimal Decimal System Observations Modulo 10 10 = 101 100 = 102 1000 = 103 1234 = (1x103) + (2x102) + (3x101) + 4 • 0 1 2 3 4 5 6 7 8 9 • 10 11 12 13 14 15 16 17 18 19 • 21 22 23 24 25 26 27 28 29 • 30 31 32 33 34 35 36 37 38 39
Look at Base 2 - Binary Binary System Observations Modulo 2 10 = 21 = 2 100 = 22 = 4 1000= 23 = 8 10112 = (1x23) + (0x22) + (1x21) + 1 = 1110 • 0 • 1 • 10 11 • 100 101 110 111 • 1000 1001 1010 1011 • 1100 1101 1110 1111 • 10000 10001 10010 10011 • 10100 10101 10110 10111
Look at Base 8 - Octal Octal System Observations Modulo 8 10 = 81 = 8 100 = 82 = 64 1000= 83 = 512 12348 = (1x83) + (2x82) + (3x81) + 4 = 66810 • 0 1 2 3 4 5 6 7 • 10 11 12 13 14 15 16 17 • 21 22 23 24 25 26 27 • 31 32 33 34 35 36 37 • … • 71 72 73 74 75 76 77 • 100 101 102 103 104 105 106
Look at Base 16 - Hexadecimal Hexadecimal System Observations Modulo 16 10 = 161 = 16 100 = 162 = 256 1000= 163 = 4096 1a3f16 = (1x163) + (ax162) + (3x161) + f = 671910 • 0 1 2 3 4 5 6 7 8 9 a b c d e f • 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f • 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f • … • f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff • 100 101 102 103 104 105 106 107 108 109 10a 10b 10c 10d 10e 10f
Working with Base 2: Binary • Each binary digit is a double of the digit to its right: • 1 1 1 1 1 1 1 1 1 • 256 128 64 32 16 8 4 2 1 • Binary can be noted as: B1011 or as 10112 • So converting binary to decimal: (~=NOT) • B1011 = 8 + ~4 + 2 + 1 = 11 • B101010 = 32 + ~16 + 8 + ~4 + 2 + ~1 = 32 + 8 + 2 = 42 • What is B01010101?
Adding with Binary Equations with Carries Obvious Equations 1 + 1 = 10 100 + 11 = 111 1001 + 100 = 1101 • B 1 1 1 1 1 1 1 1 • +B 1 0 0 1 1 1 0 0 • B11 0 0 1 1 0 1 1
Adding with Binary Equations with Carries Obvious Equations 1 + 1 = 10 100 + 11 = 111 1001 + 100 = 1101 • carry:1 1 1 1 1 • B 1 1 1 1 1 1 1 1 • +B 1 0 0 1 1 1 0 0 • B11 0 0 1 1 0 1 1 • Sum: 3 2 2 3 3 2 1 1
NOT Operation ~ Examples: NOT NOT Truth Table • The opposite of 0 • is 1 • Not_flag() • If flag = false • flag = true • else flag = false ~0 = 1 ~1 = 0 ~1011=0100 ~0101=1010
AND Operation - & Examples: AND AND Truth Table • If (you are >=18 AND • you are registered to • vote) then • You can vote • If (a==b && a==c) • print(“a = b = c”); 1 & 1 = 1 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0
OR Operation - | Examples: OR OR Truth Table • If (you are born in US OR you pass Citizen exam) then • You are US Citizen • If (a==b | a==c) • print(“a is b or c”); 1 | 1 = 1 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1
XOR Operation - ⊕ Examples: XOR XOR Truth Table • If (a!=b ) • print(“no match = TRUE”); • Uses: Encryption, etc. 1 ⊕ 1 = 0 0 ⊕ 0 = 0 1 ⊕ 0 = 1 0 ⊕ 1 = 1
Anding/Oring Longer Numbers AND Operation OR Operation 1011 0111 | 0111 0000 1111 0111 0000 0000 | 0101 0000 • 1011 0111 • & 0111 0000 • 0011 0000 • 1100 0011 • &1111 0000
Why ANDs and ORs? AND Or Useful for turning on bits Set a flag Set a value into a field • Useful for turning off bits • Clear a field • Clear a flag
Converting Base 16 -> Base 10 Method 1: Convert to Binary, then Decimal: Method 2: Use division remainders: Convert from base 10 to base N (Example base 2): Number / 2 ->remainder=>digit0 -> quotient / 2 -> remainder =>digit1 -> quotient / 2 -> remainder =>digit2 • 0x1af = 0001 1010 1111 • = 20 + 21 + 22 + 23 + 25 + 27 + 28 • = 1 + 2 + 4 + 8 + 32 + 128 + 256 • = 43110 • 0x456 = 0100 0101 0110 • = 210+26+24+22+21 • = 1024 + 64 + 16 + 4 + 2 • = 111010
Converting Base 16 -> Base 10 Example Method 2: Method 2: Use division remainders: Convert from base 10 to base N Number / n ->remainder=>digit0 -> quotient / n -> remainder =>digit1 -> quotient / n -> remainder =>digit2 Example 2: Convert 3610 into base 16: 36/16 ->4 2/16 ->2 3610 = 2416 • Example 1: Convert 3610 into binary: • Quotient/2 ->Remainder • 36/2 ->0 • 18/2 ->0 • 9/2 ->1 • 4/2 ->0 • 2/2 ->0 • 1/2 ->1 • 3610 = 1001002
Forming a negative Number 1s Complement 2s Complement 0=0000 1=0001 -1=1111 2=0010 -2=1110 3=0011 -3=1101 4=0100 -4=1100 Total: +7 -8 • 0=0000 -0=1111? • 1=0001 -1=1110 • 2=0010 -2=1101 • 3=0011 -3=1100 • 4=0100 -4=1011 • Total: +7 -7
Signed & Unsigned Numbers Assumes 1-byte storage
Signed & Unsigned Integers • Signed integers • Use when numbers may be negative • To create negative numbers, the high-order (top) bit is the signed bit. • 0=Positive Number • 1=Negative Number • Unsigned integers • Use when all numbers are POSITIVE. • No overflow to negative numbers are possible then • Accuracy - Security: • Do you want positive only – or positive & negative? • Signed: Incrementing goes from large positive to large negative.
Converting to Decimal: Powers of Two • The sign bit (bit 7) indicates both sign and value: • If top N bit is ‘0’, sign & all values are positive: top set value: 2N • If top N bit is ‘1’, sign is negative: -2N • Remaining bits are calculated as positive values: • 10101010 = -27+ 25 + 23 + 21 = -128 + 32 + 8 + 2 = -86 • 01010101 = 26+ 24 + 22 + 20 = 64 + 16 + 4 + 1 = 85
Changing Signs: Two’s Compliment • A positive number may be made negative and vice versa using this technique • Method: Take the inverse of the original number and add 1. • Original: 01010101 = 85 10101011 = -85 • invert: 10101010 01010100 • add 1: +1 +1 • sum: 10101011 = -85 01010101 = 85
Changing Signs: Two’s Compliment • Why does this work? • Identity + Inverse = -1 • Number: 10101010 • Inverse: 01010101 • Sum: 11111111 = -1 • If x + x’ = -1 • Then x + (x’ + 1) = 0 • And x’ + 1 = -x
Shifting Bits Shift Left Shift Right E.g.: 0xa9 = 1010 1001 Shift right 1: 0101 0100 Shift right 1: Shift right 2: New hexadecimal value: • E.g.: 0xa9 =1010 1001 • Shift left 1: 0101 0010 • Shift left 1: • Shift left 2: • New hexadecimal value:
Shifting is useful • Move bits into position • Extract a field • Example: • 1.0111011 x 2101 • Sign = 0 (positive) Exponent = B101 = 5 • Shift and Or all fields together to get float value
Conclusion Binary – Octal - Hexadecimal • Computers operate in binary • It is important to ‘speak’ binary and hexadecimal • Base 2 & 16 will be used in a number of other courses