Instructor: Reza...
Transcript of Instructor: Reza...
Fall 2010
Instructor: Reza Entezari-Maleki
Email: [email protected]
Sahrif University of Technology 1
Introduction to Programmingsession 4
Outlines
ó Binary numbers
ó Binary addition and subtraction
ó One’s and two’s complement
ó Bitwise operators
ó Binary multiplication and division
2
Binary numbersó Binary numbersó Why binary?ó Computers are built using digital circuitsó Inputs and outputs can have only two valuesó True (high voltage) or false (low voltage)
ó Converting base 10 to base 2ó Octal and hexadecimal
ó Integersó Unsigned integersó Integer additionó Signed integers
ó C bit operatorsó And, or, not, and xoró Shift-left and shift-right
3
Base 10 and Base 2
ó Base 10ó Each digit represents a power of 10ó 4173 = 4*103 + 1*102 + 7*101 + 3*100
ó Base 2ó Each bit represents a power of 2ó 10110 = 1*24 + 0*23 + 1*22 + 1*21 + 0*20 = 22
ó Question:ó What is the binary representation of number12 ?
ó Response:ó 1100
4
Base 8
ó Octal (base 8)ó Digits 0, 1, …, 7
ó Thus the 12 bit binary 110 010 101 001number converted to Oct is:
6251
5
000 = 0001 = 1010 = 2011 = 3100 = 4101 = 5110 = 6111 = 7
Base 8ó Question:ó What is the octal representation of number 118 (in base 10) ?
ó Response:ó 166
ó Question:ó What is the octal representation of number 1011000101 (in base 2) ?
ó Response:ó 1305
ó Question:ó What is the binary representation of number 1472 (in base 8) ?
ó Response:ó 1100111010
6
Base 16
ó Hexadecimal (base 16)ó Digits 0, 1, …, 9, A, B, C, D, E, F
ó Thus the 16-bit binary number 1011 0010 1010 1001converted to Hex is: B2A9
7
0000 = 0 1000 = 80001 = 1 1001 = 90010 = 2 1010 = A0011 = 3 1011 = B0100 = 4 1100 = C0101 = 5 1101 = D0110 = 6 1110 = E0111 = 7 1111 = F
Base 16ó Question:ó What is the hexadecimal representation of number 375 (in base 10) ?
ó Response:ó 177
ó Question:ó What is the hexadecimal representation of number 1011000101(in base 2) ?
ó Response:ó 2C5
ó Question:ó What is the binary representation of number 6A4D2 (in base 16) ?
ó Response:ó 01101010010011010010
8
ó Fixed number of bits in memoryó Short: usually 16 bitsó Int: 16 or 32 bitsó Long: 32 bits
ó Unsigned integeró No sign bitó Always positive or 0ó All arithmetic is modulo 2n
ó Example of unsigned intó 00000001 è 1ó 00001111 è 15ó 00010000 è 16ó 00100001 è 33ó 11111111 è 255
9
Integers
10
Decimal Addition
3 7 5 8+ 4 6 5 7
1) Add 8 + 7 = 15Write down 5, carry 1
1
8
1 1
4 1 5 4) Add 3 + 4 + 1 = 8Write down 8
3) Add 7 + 6 + 1 = 14Write down 4, carry 1
2) Add 5 + 5 + 1 = 11Write down 1, carry 1
Add 3758 to 4657:
Binary Addition
Rules:ó 0 + 0 = 0ó 0 + 1 = 1ó 1 + 0 = 1 (just like in decimal)
n 1 + 1 = 210= 102 = 0 with 1 to carry
n 1 + 1 + 1 = 310= 112 = 1 with 1 to carry
11
Binary Addition …
1 1 0 1 1 1 + 0 1 1 1 0 0
1
1
111
0 1 0 0 11
Example 1: Add
binary 110111 to 11100
Col 1) Add 1 + 0 = 1Write 1
Col 2) Add 1 + 0 = 1Write 1
Col 3) Add 1 + 1 = 2 (10 in binary)Write 0, carry 1
Col 4) Add 1+ 0 + 1 = 2Write 0, carry 1
Col 6) Add 1 + 1 + 0 = 2Write 0, carry 1
Col 5) Add 1 + 1 + 1 = 3 (11 in binary)Write 1, carry 1
Col 7) Bring down the carried 1Write 112
Binary Addition …
Verification1101112à 5510
+0111002 + 2810
8310
64 32 16 8 4 2 11 0 1 0 0 1 1
= 64 + 16 + 2 +1= 8310
1 1 0 1 1 1 + 0 1 1 1 0 0 1 0 1 0 0 11
You can always check youranswer by converting thefigures to decimal, doing theaddition, and comparing theanswers.
13
Decimal Subtraction
8 0 2 5- 4 6 5 7
Subtract 4657 from 8025:
7 9 1111
8633
1) Try to subtract 5 – 7 à can’t.Must borrow 10 from next column.
4) Subtract 7 – 4 = 33) Subtract 9 – 6 = 3
2) Try to subtract 1 – 5 à can’t.Must borrow 10 from next column.
But next column is 0, so must go to column after next to borrow.
Add the borrowed 10 to the original 0.Now you can borrow 10 from this column.
Add the borrowed 10 to the original 5.Then subtract 15 – 7 = 8.
Add the borrowed 10 to the original 1..Then subract 11 – 5 = 6
14
Decimal Subtraction …
§So when you cannot subtract, you borrow from the column to the left. §The amount borrowed is 1 base unit, which in decimal is 10. §The 10 is added to the original column value, so you will be able to subtract.
8633
8 0 2 5- 4 6 5 7
15
Binary Subtraction
• In binary, the base unit is 2
• So when you cannot subtract, you borrow from the column to the left.
• The amount borrowed is 2.
• The 2 is added to the original column value, so you will be able to subtract.
16
Binary Subtraction …
1 1 0 0 1 1- 1 1 1 0 0
Example 1: Subtractbinary 11100 from 110011
20 0 2
12
1101
Col 1) Subtract 1 – 0 = 1
Col 5) Try to subtract 0 – 1 à can’t.Must borrow from next column.
Col 4) Subtract 1 – 1 = 0
Col 3) Try to subtract 0 – 1 à can’t.Must borrow 2 from next column.
But next column is 0, so must go to column after next to borrow.
Add the borrowed 2 to the 0 on the right.Now you can borrow from this column(leaving 1 remaining).
Col 2) Subtract 1 – 0 = 1
Add the borrowed 2 to the original 0.Then subtract 2 – 1 = 1
1Add the borrowed 2 to the remaining 0.
Then subtract 2 – 1 = 1Col 6) Remaining leading 0 can be ignored.
17
Binary Subtraction …
Verification1100112à 5110
- 111002 - 2810
2310
64 32 16 8 4 2 11 0 1 1 1
= 16 + 4 + 2 + 1= 2310
1 1 0 0 1 1- 1 1 1 0 0
20 0 2
12
1101 1
Subtract binary 11100 from 110011:
18
Binary Subtraction …
1 0 1 0 0 1- 1 0 1 0 0
Example 2: Subtractbinary 10100 from 101001
20 0 2
1101 0
Verification1010012à 4110
- 101002 - 2010
2110
64 32 16 8 4 2 11 0 1 0 1
= 16 + 4 + 1= 2110
19
ó Consider only numbers in a rangeó E.g., five-digit car odometer: 0, 1, …, 99999ó E.g., eight-bit numbers 0, 1, …, 255
ó Roll-over when you run out of spaceó E.g., car odometer goes from 99999 to 0, 1, …ó E.g., eight-bit number goes from 255 to 0, 1, …
20
One’s and Two’s Complement
21
One’s and Two’s Complement …ó One’s complement: flip every bitó E.g., b is 01000101 (i.e., 69 in base 10)ó One’s complement is 10111010ó That’s simply 255-69
ó Subtracting from 11111111 is easy (no carry needed!)
ó Two’s complementó Add 1 to the one’s complementó E.g., (255 – 69) + 1 è 1011 1011
- 0100 01011111 1111
1011 1010
b
one’s complement
ó Computing “a – b” for unsigned integersó Same as “a + 256 – b”ó Same as “a + (255 – b) + 1”ó Same as “a + onecomplement(b) + 1”ó Same as “a + twocomplement(b)”
ó Example: 172 – 69ó The original number 69: 0100 0101ó One’s complement of 69: 1011 1010 ó Two’s complement of 69: 1011 1011ó Add to the number 172: 1010 1100ó The sum comes to: 0110 0111ó Equals: 103 in base 10
22
One’s and Two’s Complement …
1010 1100+ 1011 1011
1 0110 0111
ó Sign-magnitude representationó Use one bit to store the signó Zero for positive numberó One for negative number
ó Examplesó E.g., 0010 1100 è 44ó E.g., 1010 1100 è -44
ó Hard to do arithmetic this way, so it is rarely usedó Complement representationó One’s complementó Flip every bitó E.g., 1101 0011 è -44
ó Two’s complementó Flip every bit, then add 1ó E.g., 1101 0100 è -44
23
One’s and Two’s Complement …
Bitwise Operators
ó Bitwise AND (&) ó Bitwise OR (|)
&
0
1
0 1
0 0
0 1
|
0
1
0 1
0 1
1 1
ó Bitwise XOR (^)
^
0
1
0 1
0 1
1 0
ó Bitwise NOT (~)
~
0 1
1 0
24
25
Bitwise Operators … ó Shift left (<<): Multiply by powers of 2ó Shift some # of bits to the left, filling the blanks with 0
ó Shift right (>>): Divide by powers of 2ó Shift some # of bits to the rightó For unsigned integer, fill in blanks with 0
óExercise: What about signed integers?
0 0 1 1 0 1 0 052
1 1 0 1 0 0 0 052<<2
0 0 1 1 0 1 0 052
0 0 0 0 1 1 0 152>>2
ó SHR # bits means to shift right # times
Ex 1: SHR 3 1 0 0 1 1 1 0 00 0 0 1 0 0 1 1 1 0 0 = 0 0 0 1 0 0 1 1
Ex 2: SHR 2 1 1 1 0 0 1 1 0= 0 0 1 1 1 0 0 1
26
Bitwise Operators …
Bitwise Operators …
ROLROL (rotate left) shifts each bit one place to the left. The originalleftmost bit is shifted into the rightmost position. No bits are lost.
Ex 1. ROL 1 1 0 1
Ex 2. ROL 1 1 0 0= 1 0 0 1
1 0 1 1
27
28
Bitwise Operators …
ó ROL # bits means to rotate left # times
Ex 1: ROL 3 1 0 0 1 1 1 0 0= 1 1 1 0 0 1 0 0
Ex 2: ROL 2 1 1 1 0 0 1 1 0= 1 0 0 1 1 0 1 1
Bitwise Operators …
RORROR (rotate right) shifts each bit one place to the right. Theoriginal rightmost bit is shifted into the leftmost position. Nobits are lost.
Ex 1. ROR 1 0 1 1
Ex 2. ROR 0 0 1 1= 1 0 0 1
1 0 11
29
ó Multiplication in the binary system works the same way as in the decimal system: ó 1*1=1 ó 1*0=0 ó 0*1=0
ó Example: 101
* 11______
101 1010
______ 1111
30
Binary Multiplication
ó Follow the same rules as in decimal division.ó For Example: 111011/11ó Example:
111011 1111 10011
______ 0101
11______
010 111
____010
31
Binary Division
óExerciseHow we can implement binary multiplication anddivision using shift and addition (or subtraction)operations?
32
Binary Multiplication and Division