Number Systems & Binary Arithmetic

Download Report

Transcript Number Systems & Binary Arithmetic

Number Systems & Binary
Arithmetic
Slides adapted from:
Dr. Wood, Univ. of Maryland
Prof. Orr, Indiana Univ.
Encoding Information
We use encoding schemes to represent and
store information.
• Roman Numerals: I, IV, XL, MMIV
• Acronyms: UMW, CPSC-125, SSGN-726
• Postal codes: 22401, M5W 1E6
Encoding schemes are only useful if the stored
information can be retrieved.
Introduction to Numbering Systems
• We are all familiar with the decimal number
system (Base 10). Some other number systems
that we will work with are:
– Binary  Base 2
– Octal  Base 8
– Hexadecimal  Base 16
Characteristics of Numbering Systems
1) The digits are consecutive.
2) The number of digits is equal to the size of
the base.
3) Zero is always the first digit.
4) The base number is never a digit.
5) When 1 is added to the largest digit, a sum
of zero and a carry of one results.
6) Numeric values determined by the number
have implicit positional values of the digits.
Decimal Notation
base 10 or radix 10 ... uses 10 symbols
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Position represents powers of 10
• 547310 or 5473
(5 * 103) + (4 * 102) + (7 * 101) + (3 * 100)
• 73.1910 or 73.19
(7 * 101) + (3 * 100) + (1 * 10-1) + (9 * 10-2)
Why we use a Decimal Number System
Binary Notation
Binary Notation
• base 2 ... uses only 2 symbols
0, 1
• Position represents powers of 2
• 110102
(1 * 24) + (1 * 23) + (0 * 22) + (1 * 21) + (0 * 20)
• 10.112
(1 * 21) + (0 * 20) + (1 * 2-1) + (1 * 2-2)
Binary Number System
• Also called the “Base 2 system”
• The binary number system is used to model
the series of electrical signals computers use
to represent information
• 0 represents the no voltage or an off state
• 1 represents the presence of voltage or an
on state
Why do we need the Binary System?
“On” 1
“Off” 0
True
False
Octal Notation
base 8 ... uses 8 symbols
0, 1, 2, 3, 4, 5, 6, 7
• Position represents power of 8
• 15238
(1 * 83) + (5 * 82) + (2 * 81) + (3 * 80)
• 56.728
(5 * 81) + (6 * 80) + (7 * 8-1) + (2 * 8-2)
Hexadecimal Notation
base 16 or ‘hex’ ... uses 16 symbols
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
• Position represents powers of 16
• B65F16 or 0xB65F
(11 * 163) + (6 * 162) + (5 * 161) + (15 * 160)
• F3.A916 or 0xF3.A9
(15 * 161) + (3 * 160) + (10 * 16-1) + (9 * 16-2)
Why do we need Hex and Octal ?
• Humans are accustomed to decimal
• Computers use binary
• So why ever use octal or hexadecimal?
1. Binary numbers can be long with a lot of digits
2. Longer number make for more confusion
3. Copying longer numbers allows greater chance for
error
4. Octal represents binary in 1/3 the number of digits
5. Hexadecimal represents binary in 1/4 the number of
digits
Conversions
•
•
•
•
Binary , Octal , Hex to Decimal
Decimal to Binary , Octal , Hex
Binary to Octal , Octal to Binary
Binary to Hex , Hex to Binary
Base B to Decimal (Base 10)
• The easiest method for converting a number
in base B to its decimal equivalent is to use
the Multiplication Algorithm
• Multiply the binary digits by increasing powers
of B, starting from the right
• Then, to find the decimal number equivalent,
sum those products
Binary to Decimal Example
Convert (10101101)2 to its decimal equivalent:
Binary
Positional Values
Products
1 0 1 0 1 1 0 1
x x x x x x x x
27 26 25 24 23 22
128 + 32 + 8 + 4 + 1
17310
21 20
Octal to Decimal Example
Convert 6538 to its decimal equivalent:
Octal Digits
Positional Values
Products
6
5
3
82
81
80
x
x
x
384 + 40 + 3
42710
Hex to Decimal Example
Convert 3B4F16 to its decimal equivalent:
Hex Digits
Positional Values
Products
3
x
B
x
4
x
F
x
163 162 161 160
12288 +2816 + 64 +15
15,18310
Decimal (Base 10) to Base B
• The easiest way to convert a decimal number to its
base B equivalent is to use the Division Algorithm
• This method repeatedly divides a decimal number by
B and records the quotient and remainder
– The remainder digits form the Base B equivalent in least
significant to most significant digit sequence
Decimal to Binary Example
Convert 67 to its binary equivalent:
6710 = x2
Step 1: 67 / 2 = 33 R 1
Step 2: 33 / 2 = 16 R 1
Divide 67 by 2. Record quotient in next row
Again divide by 2; record quotient in next row
Step 3: 16 / 2 = 8 R 0
Repeat again
Step 4: 8 / 2 = 4 R 0
Repeat again
Step 5: 4 / 2 = 2 R 0
Repeat again
Step 6: 2 / 2 = 1 R 0
Repeat again
Step 7: 1 / 2 = 0 R 1
STOP when quotient equals 0
1 0 0 0 0 1 12
Decimal to Octal Example
Convert 42710 to its octal equivalent:
427 / 8 = 53 R3
53 / 8 = 6 R5
6 / 8 = 0 R6
Divide by 8; R is LSD
Divide Q by 8; R is next digit
Repeat until Q = 0
6538
Decimal to Hex Example
Convert 83010 to its hexadecimal equivalent:
= E in Hex
830 / 16 = 51 R14
51 / 16 = 3 R3
3 / 16 = 0 R3
33E16
Binary to Octal Example
Binary to Octal – Group in 3’s
• 11001101112
• (12) (1002) (1102) (1112)
• (18) (48) (68) (78)
• 14678
Octal to Binary Example
Octal to Binary – Write each octal digit as a 3 bit
binary equivalent
• 14678
• (18) (48) (68) (78)
• (0012) (1002) (1102) (1112)
• 11001101112
Binary to Hex Example
Binary to Hex – Group in 4’s
• 10110110010111112
• (10112) (01102) (01012) (11112)
• (B16) (616) (516) (F16)
• B65F16
Hex to Binary Example
Hex to Binary – write each hex digit as a 4 bit
binary equivalent
• B65F16
• (B16) (616) (516) (F16)
• (10112) (01102) (01012) (11112)
• 10110110010111112
Computing Systems
• Computers were originally designed for the
primary purpose of doing numerical calculations.
Abacus, counting machines, calculators
• We still do numerical operations, but not
necessarily as a primary function.
• Computers are now “information processors”
and manipulate primarily nonnumeric data.
Text, GUIs, sounds, information structures, pointers
to data addresses, device drivers
Codes
• A code is a scheme for representing
information.
• Computers use codes to store types of
information.
• Examples of codes:
1. Alphabet
2. DNA (biological coding scheme)
3. Musical Score
4. Mathematical Equations
Codes
• Two Elements (always)
1) A group of symbols
2) A set of rules for interpreting these symbols
• Place-Value System (common)
- Information varies based on location
- Notes of a musical staff
- Bits in a binary number
Computers and Codes
Computers are built from two-state
electronics.
• Each memory location (electrical, magnetic,
optic) has two states (On or Off)
• Computers must represent all information
using only two symbols
• On (1)
• Off (0)
• Computers rely on binary coding schemes
Computers and Binary
• Decimal
109
• Binary
11011012
• Computer 16-bit word size
0000 0000 0110 11012
• Computer 32-bit word size
0000 0000 0000 0000 0000 0000 0110 11012
64 bit words are now common
Binary Addition
4 Possible Binary Addition Combinations:
(1)
Carry
(3)
0
+0
00
1
+0
01
(2)
Sum
(4)
0
+1
01
1
+1
10
Note that leading
zeroes are frequently
dropped.
Negative Numbers
4 ways to represent negative numbers
• Sign Magnitude Format
• One’s Complement Format
• Two’s Complement Format
• Excess – N Format
We study the first 3 here
Sign Magnitude format
Sign-Magnitude Format
• Uses the highest order bit as a ‘sign’ bit. All
other bits are used to store the absolute value
(magnitude)
• Negative numbers have the sign bit set
• -10910 in 16-bit representation using a sign bit
1000 0000 0110 11012
Consequences of Sign Magnitude
• Two ways to represent 0
+0 = 0000 00002
-0 = 1000 00002
• Reduces the range of values that can be
stored.
One’s Complement
One’s Complement Format
• Exact opposite of the sequence for the positive
value. Each bit is “complemented” or “flipped”
• 109 in 16-bit representation
0000 0000 0110 11012
• -109 in 16-bit One’s Complement
1111 1111 1001 00102
Consequences of One’s Complement
• Two ways to represent 0
+0 = 0000 00002
-0 = 1111 11112
• This makes mathematical operations difficult
Two’s Complement
Two’s Complement Format
• Add 1 to One’s Complement
• 109 in 16-bit representation
0000 0000 0110 11012
• -109 in 16-bit One’s Complement
1111 1111 1001 00102
• -109 in 16-bit Two’s Complement
1111 1111 1001 00112
Consequences of Two’s Complement
• In two's-complement, there is only one zero
(00000000)
• Mathematical operations are much simpler
Next Time : Programming Languages