Binary & Hex Review
Bits & Bytes
Bits And Bit Patterns N bits gives 2N possible patterns 2 bits = 4 patterns 3 bits = 8 patterns
Bytes Smallest usable chunk of memory: byte : 8 bits Special names for large collections of bytes: Name Number of Bytes power of 2 byte 1 20 kilobyte (KB) 1024 210 megabyte (MB) 1,048,576 220 gigabyte (GB) 1,073,741,824 230 terabyte (TB) 1,099,511,627,776 240
Metric Units Standard metric units
Explaining Size 2TB Drive:
Bi units Metric prefixes May refer to powers of 2 or 10 Roughly equivalent GB GiB 109 = 1000000000 ~ 1073741824 = 230
Bi units Memory measured in powers of 2 Network / Processor in powers of 10 Disk Manufactures powers of 10 OS powers of 2 2 TB = 2 * 1012 2 * 1012 / 240 = 1.819 GiB
This Class Unless stated otherwise, assume all measurements are powers of 2
Powers of 2 How many bytes is 16GB?
Powers of 2 How many bytes is 16GB? = 16 * 230 = 24 * 230 = 234
Unsigned Binary
Bases Place based number representations: 1 2 5 9 1 ten thousands 104 103 hundreds 102 tens 101 ones 100 1 2 5 9 thirty-twos 25 sixteens 24 eights 23 fours 22 twos 21 ones 20 1
Specifying Base Specify base as subscript: 610 = 1102
Binary Number Interpretation Table Method: 64 + 32 + 8 + 1 = 105 011010012 = 10510 128 64 32 16 8 4 2 1
Leading 0's Leading 0's do not matter: 101 = 0101 = 00000101 Byte : fundamental storage unit : 8 bits Often write binary numbers in multiples 8 bits 128 64 32 16 8 4 2 1
Conversion With Division/ Multiplication Binary works in powers of 2 Multiplying/dividing by 2 shifts digits
Decimal -> Binary with Division Step 1: Start with a blank answer and the number your are converting Step 2: Divide your number by 2 to make a quotient and a remainder Step 3: Place your remainder on the left side of your answer Step 4: If your quotient is 0, you are done Otherwise, make the quotient your new number and go back to step 2 Convert 105 to binary: Number ÷ 2 Q R Answer 105
Decimal -> Binary with Division Step 1: Start with a blank answer and the number your are converting Step 2: Divide your number by 2 to make a quotient and a remainder Step 3: Place your remainder on the left side of your answer Step 4: If your quotient is 0, you are done Otherwise, make the quotient your new number and go back to step 2 Convert 105 to binary: Number ÷ 2 Q R Answer 105 ÷ 2 52 1
Decimal -> Binary with Division Step 1: Start with a blank answer and the number your are converting Step 2: Divide your number by 2 to make a quotient and a remainder Step 3: Place your remainder on the left side of your answer Step 4: If your quotient is 0, you are done Otherwise, make the quotient your new number and go back to step 2 Convert 105 to binary: Number ÷ 2 Q R Answer 105 ÷ 2 52 1
Decimal -> Binary with Division Step 1: Start with a blank answer and the number your are converting Step 2: Divide your number by 2 to make a quotient and a remainder Step 3: Place your remainder on the left side of your answer Step 4: If your quotient is 0, you are done Otherwise, make the quotient your new number and go back to step 2 Convert 105 to binary: Number ÷ 2 Q R Answer 105 ÷ 2 52 1 52 ÷ 2
Decimal -> Binary with Division Step 1: Start with a blank answer and the number your are converting Step 2: Divide your number by 2 to make a quotient and a remainder Step 3: Place your remainder on the left side of your answer Step 4: If your quotient is 0, you are done Otherwise, make the quotient your new number and go back to step 2 Convert 105 to binary: Number ÷ 2 Q R Answer 105 ÷ 2 52 1 52 ÷ 2 26 01
Decimal -> Binary with Division Step 1: Start with a blank answer and the number your are converting Step 2: Divide your number by 2 to make a quotient and a remainder Step 3: Place your remainder on the left side of your answer Step 4: If your quotient is 0, you are done Otherwise, make the quotient your new number and go back to step 2 Convert 105 to binary: Number ÷ 2 Q R Answer 105 ÷ 2 52 1 52 ÷ 2 26 01 26 ÷ 2 13 001
Decimal -> Binary with Division Step 1: Start with a blank answer and the number your are converting Step 2: Divide your number by 2 to make a quotient and a remainder Step 3: Place your remainder on the left side of your answer Step 4: If your quotient is 0, you are done Otherwise, make the quotient your new number and go back to step 2 Convert 105 to binary: Number ÷ 2 Q R Answer 105 ÷ 2 52 1 52 ÷ 2 26 01 26 ÷ 2 13 001 13 ÷ 2 6 1001 6 ÷ 2 3 01001 3 ÷ 2 101001 1 ÷ 2 1101001
Decimal -> Binary with Division Step 1: Start with a blank answer and the number your are converting Step 2: Divide your number by 2 to make a quotient and a remainder Step 3: Place your remainder on the left side of your answer Step 4: If your quotient is 0, you are done Otherwise, make the quotient your new number and go back to step 2 Convert 105 to binary: Number ÷ 2 Q R Answer 105 ÷ 2 52 1 52 ÷ 2 26 01 26 ÷ 2 13 001 13 ÷ 2 6 1001 6 ÷ 2 3 01001 3 ÷ 2 101001 1 ÷ 2 1101001 128 64 32 16 8 4 2 1 = 105
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 0 * 2
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 0 * 2 + 1
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 1 * 2
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 1 * 2 + 1
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 3
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 3 3 * 2
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 3 3 * 2 + 0
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 3 6
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 3 6 6 * 2
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 3 6 _ 6 * 2 + 1
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 3 6 _ 13
Binary -> Decimal with Multiplication Step 1: Start with the number your are converting and the answer of 0 Step 2: Multiply your answer by 2 Step 3: Remove the leftmost digit of number and add it to your answer Step 4: If number has no more digits, you are done Otherwise, go back to step 2 Convert 11012 to decimal: Number Answer 1101 101 1 01 3 6 _ 13 128 64 32 16 8 4 2 1 = 13
Unsigned Binary Math
Adding Adding base 2: 1 1 = 1 2 = 10 = keep 0 and carry 1 1 = 1 2 = 10 = keep 0 and carry 1 3 = 11 = keep 1 carry 1 1
Adding Adding base 2: 1 7 5 12 1 = 1 2 = 10 = keep 0 and carry 1 1 = 1 2 = 10 = keep 0 and carry 1 3 = 11 = keep 1 carry 1 7 5 12 1
Overflow What is 1111 + 0001? 1
Overflow What is 1111 + 0001? Overflow: number gets too big to hold in the digits we have 1
Signed Binary
Sign Magnitude?? - / + are two choices Ex: + 5 - 5 sign 4 2 1 sign 4 2 Use a bit value to represent sign Ex: + 5 - 5 sign 4 2 1 sign 4 2 1
Sign Magnitude?? Would need new math algorithm… 1 -3 1
2’s complement 2's Complement System: First bit indicates sign: 0 : Positive #: works like normal What is: 00110 Bit Values: 4+2 Value: 6
2’s complement 2's Complement System: First bit indicates sign: 1 : Negative #: defined as indicated value – 2n where n is number of bits What is: 11010 Bit Values: 16 + 8 + 2 = 26 2n : 25 = 32 Value: 26 – 32 = -6
2’s complement negation To negate a number, reverse bits and add 1 What is -3 as 4-bit 2's complement number?: 0011 start with 3 1100 reverse 1 + 1 1101 -3 in two's complement Check: 1101 = 8 + 4 + 1 = 13 2n = 24 = 16 13 – 16 = -3
2’s complement negation To negate a number, reverse bits and add 1 What is 1101 as a 4-bit 2’s complement number? 1101 start with negative pattern 0010 reverse 2 interpret 3 add one 1101 negated is 3, so it must be -3
More Examples What are? 1 1 1
More Examples What are? 1 1 1 Starts with 0 = normal: 4 + 2 + 1 7
More Examples What are? 4 + 2 + 1 7 1 -1 1 1 1 1 1 1 1 Starts with 0 = normal: 4 + 2 + 1 7 Starts with 1 = negative: Flip Bits Add one: Read number, make negative: 1 -1 1
More Examples What are? 4 + 2 + 1 7 1 -1 8 -8 1 1 1 1 1 1 1 1 1 Starts with 0 = normal: 4 + 2 + 1 7 Starts with 1 = negative: Flip Bits Add one: Read number, make negative: 1 -1 Starts with 1 = negative: Flip Bits Add one: Read number, make negative: 8 -8 1 1 1
Range 2's complement uses half of range for negative, half for 0/positive 4 bits: 24 = 16 values Unsigned 0 to 15 2's Complement -8 to 7
4 bits Biggest positive: 7 -1 : Lowest negative: -8 1 1 Flip (0) 1 1 1 Flip (0) +1 = 1 1 Flip (7) +1 = 8
8 bits Biggest positive: 127 -1 : Lowest negative: -128 1 1 Flip (0) 1 1 1 Flip (0) +1 = 1 1 Flip (127) +1 = 128
2’s complement addition Standard addition algorithm works fine: 1 + -3: 1 -3 1
2’s complement addition Standard addition algorithm works fine: 1 + -3: 1 -3 -2 1 ??? 110 Reverse Bits 001 Interpret 1 Add One 2 Value -2
2’s complement 2 + 4 = 1
2’s complement 2 + 4 = 6 Success – answer fits in four bits Ignore extra carry 1
2’s complement -3 + -2 = 1 3 0011 Reverse Bits 1100 Add one 1101 2 0010 Reverse Bits 1101 Add one 1110 -3 + -2 = 1
2’s complement ??? 1011 Reverse Bits 0100 Add one 0101 Bit Values 1 + 4 Value -5 -3 + -2 = -5 Success – answer fits in four bits Ignore extra carry 1
2’s complement 4 + 6 = 1
2’s complement 4 + 6 = -6!?!?! Overflowed the 4 bits! 1 ??? 1010 Reverse Bits 0101 Add one 0110 Bit Values 2 + 4 Value -6 4 + 6 = -6!?!?! Overflowed the 4 bits! 1
2’s complement -4 + -6 = 1 4 0100 Reverse Bits 1011 Add one 1100 6 0110 Reverse Bits 1001 Add one 1010 1
2’s complement -4 + -6 = 6??? Overflowed the 4 bits! 1
Overflow Big + numbers and Low – numbers can wrap!
2’s complement Final column carry out and in are == : OK Final column carry out and in are != : Overflow 1
Hexadecimal
Hexdecimal Base 16 Each column is a power of 16: = 4096 * 2 + 256 * 0 + 16 * 5 + 1 * 9 = 8192 + 80 + 9 205916 = 828110 4096’s 163 256’s 162 16’s 161 1’s 160 2 5 9
13? How do we represent 13??? 4096’s 163 256’s 162 16’s 161 1’s 160
13? How do we represent 13??? A-F = 10-15 4096’s 256’s 16’s 1’s 163 162 16’s 161 1’s 160
Hex Numbers 1A316 = 1 * 256 + 10 * 16 + 3 * 1 = 256 + 160 + 3 = 41910 4096 256 16 1 A 3
Decimal Hex Division Methods: ÷ 16 7 12 (C) C7 1C7 Current Value Quotient Remainder 455 ÷ 16 28 7 1 12 (C) C7 1C7
Hex Hexdecimal = base 16 4 bits = 1 hex digit
Summary Hex is a bridge: Easier for us Still just binary for computers System Base Symbols Used by humans? Used in computers? Decimal 10 0, 1, … 9 Yes No Binary 2 0, 1 Hex 16 0-9ABCDEF No, but better than binary No, but easily translates to binary