Download Computer Science: Arithmetic, Data Representation, and Floating Point - Prof. Jiang Li and more Study notes Computer Architecture and Organization in PDF only on Docsity!
1
Arithmetic for Computers^ Dr. Jiang LiSlides adapted from various sources (e.g. VT, RPI, UCSB etc)
2
Data versus Information Bits are just bits (no inherent meaning) Conventions define relationship between bits andnumbers Given a sequence of bits, it is impossible to provide aninterpretation outside of a particular context Consider0010 0011 1011 1101 1111 1111 1111 1100 Interpreted as an integer: 599654396 Interpreted as a IEEE float: 1.99999952316284 x 2
^ Interpreted as a MIPS32 instruction: addi $29, $29, -4 ^ Interpreted as extended ASCII*: #
╜^ ⁿ
4
The Base of Numeral Systems Humans prefer base 10 Why? The relation between a number and the base Base 10^4 3 12345 = 1^ ^10 + 2^ ^10 + 3
(^2 1) 10 + 4^ ^10 + 5^ ^10 0 ^ Base^ n^ ( n^ is an integer)m-1^ d^ d...d^ = d^ ^ n^ + d^12 m^1
m-2^1 n+ … + d^ ^ n^2 m-^ 0 + d^ n^ (0^ ^ d^ < n)m^ i^ (^4) 12345(base 6) = 1 6 3 2 + 2^ ^6 + 3^ ^6 + 4^
1 0 6 + 5^ ^6 (base 10) = 1865 (base 10)(Every digit in a base n system is less than n) Notation 12345(base 10): 12345^ or 12345^ ten^10 12345(base 6): 12345or 12345six^6
12345(base 2)
Jiang Li
Binary (Base-2)^5
^ Digits: 0, 1^1011 =^ (1^ two^
3 2 2 + 0^ ^2 + 1^
(^1 0) 2 + 1^ ^2 )= 11ten^
ten
1001 =^ ?ten^ two^
1101 =^ ?tentwo^
10000 =^ ?ten^ two^
11001 =^ ?tentwo^
^ m bitsdd...d= d^1 2 m^1
m-1^ m-2^ 2 + d^22
(^1) + … + d 2 + dm- (^0) (^2) m
(dis 0 or 1)i^ Least significant bit: the rightmost bit Most significant bit: the leftmost bit
Conversion BetweenBinary and Hexademcial^7
^ 4 binary digits
^ 1 hexadecimal digit Not enough binary digits? Add 0s to the left.
^11110010011110
^ 2F9^ =?^16
Conversion BetweenBinary and Hexademcial^8
^ 4 binary digits
^ 1 hexadecimal digit Not enough binary digits? Add 0s to the left.
^11110010011110
= 3C9E 2 16
0011 2F9^ =?^16
3 C
E
10
What is the range of numbers that a setof bits can represent?
11
Unsigned Integer Representation Unsigned integers in 3 bits: (totally 8 (=
3 )
numbers)^2 000 = (0^ ^2 + 0^2
1 0 2 + 0^ ^2 )= +0^10
10 (^2 001) = (0 2 + 0 2
1 0 2 + 1^ ^2 )= +1^10
10 (^2 010) = (0 2 + 1 2
1 0 2 + 0^ ^2 )= +2^10
10 (^2 011) = (0 2 + 1 2
1 0 2 + 1^ ^2 )= +3^10
10 (^2 100) = (1 2 + 0 2
1 0 2 + 0^ ^2 )= +4^10
10 (^2 101) = (1 2 + 0 2
1 0 2 + 1^ ^2 )= +5^10
10 (^2 110) = (1 2 + 1 2
1 0 2 + 0^ ^2 )= +6^10
10 (^2 111) = (1 2 + 1 2
1 0 2 + 1^ ^2 )= +7^10
10
13
All bit sequences are explainedas positive integers so far! How about negative integers, real numbers,characters etc?
14
Signed Integer Representation (1) Sign-magnitude ^ The representation consists of one sign bit and otherbits denoting the magnitude, or absolute value, of thenumber. ^ For example, using 4 bits:^0101 = + 5^2
1101 = – 5^2 10 Using 5 bits:? = + 10?= – 9 2 10 2 10
17
Signed Integer Representation (4) Sign magnitude in3 bits^ Two’s complement dominates today
One'scomplement:
- Signed Integer Representation (2) Ones' complement Representing a positive number Same as that in unsigned integer representation Representing a negative number Figure out the representation of the absolute value Invert each and every bit Math: Using n bits to represent a negative number (– x)n ^2 – x – 1 Example (using 4 bits)^0101 = + 5^10102
- = –
- Using 5 bits:?= + 10?^2 10
- Signed Integer Representation (3) Two’s complement Representing a positive number Same as that in unsigned integer representation Representing a negative number Figure out the representation of the absolute value Invert each and every bit, then add 1 Math: Using n bits to represent a negative number (– x)n ^2 – x Example (using 4 bits)^0101 = + 5^10112
- = –
- Using 5 bits:?= + 10?^2 10
- 000 = +0^2 Two'scomplement:
- 000 = +0^2
- 000 = +0^2
- 001 = +1^2
- 001 = +1^2
- 001 = +1^2
- 010 = +2^2
- 010 = +2^2
- 010 = +2^2
- 011 = +3^2
- 011 = +3^2
- 011 = +3^2
- 100 = -0^2
- 100 = -3^2
- 100 = -4^2
- 101 = -1^2
- 101 = -2^2
- 101 = -3^2
- 110 = -2^2
- 110 = -1^2
- 110 = -2^2
- 111 = -3^2
- 111 = -0^2
- 111 = -1^2
19
Shortcuts for Two’s Complement Negation shortcut ^ Negate a number by inverting the bits and adding one,ignoring final carry Sign extension shortcut ^ Convert a^ m^ bit binary number to a (
m+k ) bit binary number ( m,k^ > 0) If the number is positive, add 0 to the left If the number is negative, add 1 to the left Example: convert a 4 bit binary number to 8 bit 4 =0100= 00000100 102
= 11111000^2
^ Bounds check shortcut^ ^ Check if 0^ ≤
x < y Do an unsigned comparison between x and y If x y, then the result is false.
20
Addition of Unsigned Integers Most obvious approach would seem to be just like inelementary school, just add and carry:^ 0000 0101
: 5^2 10 + 1011 1101: 189 2 10 ------------------1100 0010: 194 2 10
^ But, what to do if the addition of the left-most digits resultsin a non-zero carry?^ ^ That's wasn't an issue in elementary school since there was no limiton how many digits an integer may have.^ ^ But at the hardware level we have a limited number of bits, whetherit's 16 or 32 or 64 or something larger. The bottom line is thatthere's nowhere to put the result of the final carry-out.^ ^ Usually ignored (the carry dropped) for unsigned addition