Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary – Octal - Hexadecimal

Similar presentations


Presentation on theme: "Binary – Octal - Hexadecimal"— Presentation transcript:

1 Binary – Octal - Hexadecimal
Intro to Computer Math Binary – Octal - Hexadecimal B 0x B 4 C

2 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

3 Octal – Hexadecimal: What is it?
1s & 0s are tedious: Hexadecimal: 0x a 5 f 3 c e 1 Octal: 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

4 Why is it Useful? Uses include: UNIX Permissions: 740 = Self: Read, Write, Execute; Group: Read; Others: Null Data Communications: Meaning of: Transmission: 7E a b3 fd fa 4c 5d da a2 7f 7e Routing tables: 4a c0/26 Data: Setting flags, clearing bits, efficient storage Engineering: Designing and working with memory Debugging: Core dumps, reverse engineering

5 Look at Base 10 - Decimal Observations 0 1 2 3 4 5 6 7 8 9
Decimal System Observations Modulo = = = = (1x103) + (2x102) + (3x101) + 4

6 Understanding Different Bases
Other Bases 10 = = = = B 10 = // Base 2 B 11 = B 110 = B 111 = x10 = // Base 16 0x11 = x 111 = 16*

7 Look at Base 2 - Binary Observations
Binary System Observations 1 10 11 Modulo 2 10 = 21 = = 22 = = 23 = = (1x23) + (0x22) + (1x21) + 1 = 1110

8 Look at Base 8 - Octal Observations 0 1 2 3 4 5 6 7
Octal System Observations Modulo 8 10 = 81 = = 82 = = 83 = = (1x83) + (2x82) + (3x81) + 4 = 66810

9 Look at Base 16 - Hexadecimal
Hexadecimal System Observations a b c d e f a 1b 1c 1d 1e 1f a 2b 2c 2d 2e 2f f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff a 10b 10c 10d 10e 10f Modulo = 161 = = 162 = = 163 = a3f16 = (1x163) + (ax162) + (3x161) + f =

10 Working with Base 2: Binary
Each binary digit is a double of the digit to its right: Binary can be noted as: B1011 or as 10112 So converting binary to decimal: (~=NOT) B1011 = 8 + ~ = 11 B = 32 + ~ ~ ~1 = = 42 What is B ?

11 Equations with Carries
Adding with Binary Equations with Carries Obvious Equations B B B 1 + 1 = = = 1101

12 Equations with Carries
Adding with Binary Equations with Carries Obvious Equations carry: B B B Sum: 1 + 1 = = = 1101

13 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

14 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

15 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

16 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

17 Anding/Oring Longer Numbers
AND Operation OR Operation & & | |

18 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

19 Convert Binary to Hexadecimal (Base 16)
Binary: B Step 1: Separate into 4 bits from the right: Binary: B Step 2: Now convert to Base 16: Hexadecimal: 0x193A77 Convert Base 16 to Binary: 0x193A77= B Easy to remember patterns, binary -> decimal B1010 = 0xA = 10 B1111 = 0xF = 15 B1100 = 0xC = 12

20 Converting Base 16 -> Base 10
Method 1: Convert to Binary, then Decimal: Method 2: Use division remainders: 0x1af = = = = 43110 0x456 = = = =  Convert from base 10 to base N (Example base 2): Number / 2 ->remainder=>digit0 -> quotient / 2 -> remainder =>digit1 -> quotient / 2 -> remainder =>digit2

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

22 Forming a negative Number
1's Complement 2's Complement 0= =1111? 1= =1110 2= =1101 3= =1100 4= =1011 Total: +7 -7 0=0000 1= =1111 2= =1110 3= =1101 4= =1100 Total: +7 -8

23 2s Comp 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0000

24 Signed & Unsigned Numbers
Binary Signed Unsigned 1 2 +126 +127 -128 +128 -127 +129 -126 +130 -2 +254 -1 +255 Assumes 1-byte storage

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

26 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: = = = -86 = = = 85

27 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: = = -85 invert: add 1: sum: = = 85

28 Changing Signs: Two’s Compliment
Why does this work? Identity + Inverse = -1 Number: Inverse: Sum: = -1 If x + x’ = -1 Then x + (x’ + 1) = 0 And x’ + 1 = -x

29 Shifting Bits Shift Left Shift Right E.g.: 0xa9 = Shift left 1: Shift left 1: Shift left 2: New hexadecimal value: E.g.: 0xa9 = Shift right 1: Shift right 1: Shift right 2: New hexadecimal value:

30 Shifting is useful Move bits into position Extract a field Example: 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

31 Short Cuts: Binary <-> Decimal
B1010 = 0xA =1010 B1011 = B1010+1=1110 B1111 = 0xF = 1510 B1110 = B1111-1 B1100 = 0xC =1210 B1101 = B

32 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


Download ppt "Binary – Octal - Hexadecimal"

Similar presentations


Ads by Google