Notes

advertisement
Agenda
• Shortcuts converting among numbering
systems
– Binary to Hex / Hex to Binary
– Binary to Octal / Octal to Binary
• Signed and unsigned binary numbers
• Addition / Subtraction of Binary Numbers
• Two’s Complement
Numbering System Shortcuts
It is very simple to convert binary
numbers to octal or hexadecimal
numbers since 8 is 2^3, and 16 is 2^4
In other words:
• 1 Octal digit = 3 binary digits
• 1 Hex digit - 4 binary digits
Binary to Octal
Notice the Pattern:
• Largest 3 digit binary is 111
• 1 octal digit will represent a 3 digit binary
number
• Highest Octal digit is 7
• Therefore: 1112 = 78
Binary to Octal
Relationship:
Octal
0
1
2
3
4
5
6
7
Binary
000
001
010
011
100
101
110
111
Does this table
look familiar?
Practical Example
• applying octal values of rwx the chmod
command (e.g., chmod 751).
•
•
•
•
chmod 777
chmod 755
chmod 711
chmod 644
Binary to Hexadecimal
Notice the Pattern:
• Largest 4 digit binary is 1111
• 1 hex digit will represent a 4 digit binary
number
• Highest hex digit is F
• Therefore: 11112 = F16
Binary to Hexadecimal
Relationship:
Hexadecimal
0
1
2
3
4
5
6
7
Binary
0000
0001
0010
0011
0100
0101
0110
0111
Hexadecimal
8
9
A
B
C
D
E
F
Binary
1000
1001
1010
1011
1100
1101
1110
1111
Convert Hex to Binary
Steps:
• Convert Hex number to groups of
powers of 2.
• Convert to Binary number (Remember
to drop leading zeros for first set of
binary numbers - i.e. first left set)
Convert Hex to Binary
11F616
= 1
Drop Leading zeros
1
F
6
000
= 000(1)
000(1) (8)(4)(2)(1) 0(4)(2)0
=1
0001
1111
0110
= 10001111101102
Convert Binary to Hex
Steps:
• Separate into 4 bit groups starting from
the right.
• Calculate decimal equivalent (in
placeholders in powers of 2)
• Convert to Hexadecimal number
Convert Binary to Hex
10001111101102
=1
0001 1111
= 0001 0001 1111
=1
1
=1
1
= 11F616
0110
0110
(8)(4)(2)(1) 0(4)(2)0
15
6
Converting Octal to Hexadecimal
• The easiest method to convert
between Octal and Hexadecimal is
to convert to binary as an
intermediate step
• Regroup binary in groups of 4 for
hexadecimal and 3 for octal
Storing Numbers
• Numeric information (stored as a non
negative number) is often store in a
computer in binary.
Eg.
• 1 byte (0 - 255 numbers)
• 2 bytes (0 - 65535)
• 4 bytes (0-4294967295)
Data Formats
Unsigned Binary
• Data stored as a binary number, with
no way to express a negative quantity
Data Formats
Signed Binary
• Data stored as a binary number, but
using a leading zero to represent a
positive number, and the two’s
complement of a binary number for a
negative number
Adding / Subtracting Binary
Numbers
• Addition:
0+0=0
0 + 1 = 1, 1 + 0 = 1
1 + 1+ = 10
• Subtraction:
0-0=0
1 - 1 = 0, 1 - 0 = 1
0 - 1 (Must borrow from next placeholder)
Therefore 10 - 1 = 1
Two’s Complement
• Simple method of subtracting two binary
numbers by adding.
• Two Complement
– Flip binary numbers (0 becomes 1, visa versa)
– then add 1
– Result becomes negative
• Therefore, short-hand method of
representing negative integers
Download