Binary – Octal - Hexadecimal 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 (or 128) 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, reverse engineering
Look at Base 10 - Decimal Observations 0 1 2 3 4 5 6 7 8 9 Decimal System Observations 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 Modulo 10 10 = 101 100 = 102 1000 = 103 1234 = (1x103) + (2x102) + (3x101) + 4
Understanding Different Bases Other Bases 10 = 10 + 0 12 = 10 + 2 110 = 100 + 10 + 0 115 = 100 + 10 + 5 B 10 = 2 + 0 // Base 2 B 11 = 2 + 1 B 110 = 4 + 2 + 0 B 111 = 4 + 2 + 1 0x10 = 16 + 0 // Base 16 0x11 = 16 + 1 0x 111 = 16*16 + 16 + 1
Look at Base 2 - Binary Observations Binary System Observations 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 10100 10101 10110 10111 Modulo 2 10 = 21 = 2 100 = 22 = 4 1000= 23 = 8 10112 = (1x23) + (0x22) + (1x21) + 1 = 1110
Look at Base 8 - Octal Observations 0 1 2 3 4 5 6 7 Octal System Observations 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 Modulo 8 10 = 81 = 8 100 = 82 = 64 1000= 83 = 512 12348 = (1x83) + (2x82) + (3x81) + 4 = 66810
Look at Base 16 - Hexadecimal Hexadecimal System Observations 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 Modulo 16 10 = 161 = 16 100 = 162 = 256 1000= 163 = 4096 1a3f16 = (1x163) + (ax162) + (3x161) + f = 671910
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?
Equations with Carries Adding with Binary Equations with Carries Obvious Equations 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 1 + 1 = 10 100 + 11 = 111 1001 + 100 = 1101
Equations with Carries Adding with Binary Equations with Carries Obvious Equations 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 1 + 1 = 10 100 + 11 = 111 1001 + 100 = 1101
NOT Operation ~ Examples: NOT NOT Truth Table The opposite of 0 is 1 Not_flag() If flag = false flag = true else flag = false NOT ~ Input Output 1 ~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”); AND & 1 1 & 1 = 1 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 If either result is false, the result is false. If either input is false, result is false
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”); OR | 1 1 | 1 = 1 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 If either is true, the result is true. If either input is true, result is true
XOR Operation - ⊕ Examples: XOR XOR Truth Table If (a!=b ) print(“no match = 1”); else // match print(“match = 0”); Uses: Encryption, etc. XOR ⊕ 1 1 ⊕ 1 = 0 0 ⊕ 0 = 0 1 ⊕ 0 = 1 0 ⊕ 1 = 1
Anding/Oring Longer Numbers AND Operation OR Operation 1011 0111 & 0111 0000 0011 0000 1100 0011 &1111 0000 1011 0111 | 0111 0000 1111 0111 0000 0000 | 0101 0000
Why ANDs and ORs? AND Or Useful for turning off bits Clear a field Clear a flag Useful for turning on bits Set a flag Set a value into a field Sign 1 bit Exponent 8 bits Fraction 23 bits
Convert Binary to Hexadecimal (Base 16) Binary: B 110010011101001110111 Step 1: Separate into 4 bits from the right: Binary: B 1 1001 0011 1010 0111 0111 Step 2: Now convert to Base 16: Hexadecimal: 0x193A77 Convert Base 16 to Binary: 0x193A77= B0001 1001 0011 1010 0111 0111 Easy to remember patterns, binary -> decimal B1010 = 0xA = 10 B1111 = 0xF = 15 B1100 = 0xC = 12
Converting Base 16 -> Base 10 Method 1: Convert to Binary, then Decimal: Method 2: Use division remainders: 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 Convert from base 10 to base N (Example base 2): Number / 2 ->remainder=>digit0 -> quotient / 2 -> remainder =>digit1 -> quotient / 2 -> remainder =>digit2
Converting Base 16 -> Base 10 Example Method 2: Method 2: Use division remainders: 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 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 = 2 416
Forming a negative Number 1's Complement 2's Complement 0=0000 -0=1111? 1=0001 -1=1110 2=0010 -2=1101 3=0011 -3=1100 4=0100 -4=1011 Total: +7 -7 0=0000 1=0001 -1=1111 2=0010 -2=1110 3=0011 -3=1101 4=0100 -4=1100 Total: +7 -8
2s Comp 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0000
Signed & Unsigned Numbers Binary Signed Unsigned 00000000 00000001 1 00000010 2 01111110 +126 01111111 +127 10000000 -128 +128 10000001 -127 +129 10000010 -126 +130 11111110 -2 +254 11111111 -1 +255 Assumes 1-byte storage
Signed & Unsigned 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 left 1: 0101 0010 Shift left 1: Shift left 2: New hexadecimal value: E.g.: 0xa9 = 1010 1001 Shift right 1: 0101 0100 Shift right 1: Shift right 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 Sign 1 bit Exponent 8 bits Fraction 23 bits
Short Cuts: Binary <-> Decimal B1010 = 0xA =1010 B1011 = B1010+1=1110 B1111 = 0xF = 1510 B1110 = B1111-1 B1100 = 0xC =1210 B1101 = B1100+1 1100
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