Computer Arithmetic Flashcards
Decimal to Binary
Divide the number by 2 and remainder (0 or 1), and stop when the result is 0. The answer is the remainders read from bottom to top.
Binary to Decimal
Multiply the first non-zero bit by 2 and add the bit on its right.
Or from base 2
so an 8-bit number would look like
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
0 0 0 0 0 1 0 0 = 2^2 = 4
Hexadecimal
Base 16
Notation: 3B_16 = 0x3B = $3B
0000_2 - 1001_2
0_16 - 9_16
1010_2 - 1111_2
A_16. - F_16
Binary Subtraction in relation to Addition
X - Y = X + (-Y)
Sign and Magnitude Representation
First bit represents the sign.
So,
00001101_2 = + 13_10
10001101_2 = - 13_10
To negate, just flip the sign bit.
Two’s Complement Arithmetic
N represents -N using 2^n - N
Example:
N = 6_10 = 0110_2
-N = 2^4 - 6 = 10_10 = 1010_2
Decimal to Two’s Complement:
- If it’s positive
- If it’s negative (How to complement N)
Ex.
Convert 6 to binary and convert -6 to binary
Convert to binary normally if positive.
Invert sign of decimal. Now convert it to binary. Then takes two complement (Invert bits and add 1).
Ex:
1. 6 -> 0110
- -6 -> 6 -> 0110 -> 1001 -> 1010
or as before, -6 -> 2^4 - 6 -> 10 -> 1010
Arithmetic Overflow
Overflow occurs in two’s complement when addition of two positive number gives a negative result or when two negatives gives a positive.
How to detect it: Check the carry 01100 \+ 01101 \_\_\_\_\_\_ 1 1001 1100
Fixed Point Arithmetic
How to convert 0.25 to binary
Base 2 representation of decimals
2^3 2^2 2^1 2^0 . 2^-1 2^-2 2^-3 2^-4
0 0 0 0 . 0 1 0 0
The normalisation of IEE FP
1010.101 x 2^e
1.010101 x 2^(e+3)
Representing Biased forms
Convert 3 to Bias 3
What’s the Bias for 8-bit
Bias: 2^(m-1) - 1
number - bias
3 - 3 = 0
Bias: 2^(7) - 1 = 127
IEEE 754 floating point format
X = (-1)^S x 2^(E-B) x 1.F B = 127
Decimal to IEEE FP
Convert to binary.
Normalise.
Add Bias B then convert the exponent to binary.
S Biased Exponent Fractional Mantissa
1 bit 8 bits 23 bits
IEEE FP to Decimal
(-1)^S x (1+F) x 2^(E-B)
Floating Point Arithmetic
How to add
1.110100 x 2^5
+ 1.01001 x 2^3
change the exponent so they could be the same.