Representing numbers Flashcards
Converting base n to decimal
For each digit, work out d*b^p (digit, base, place)
The sum is the number in decimal
201.12 = 23^2 + 03^1 + 13^0 + 13^-1 + 2*3^2 = 18 + 1 + 1/3 + 2/9 = 19.55555
10010 = 12^4 + 02^3 + 02^2 + 12^1 + 0*2^0 = 8 + 2 = 10
DEAD = D16^3 + E16^2 + A16^1 + D16^0 = 134096 + 14256 + 1016 + 131 = 57005
Why do we use hexadecimal?
Reading and writing binary values is difficult for humans, more compact (can write numbers using fewer symbols), easy to convert to binary
Converting binary to hexadecimal
Separate it into nibbles and you can translate it easily
01011000 = 0101 1000 = 5 8 = 58
Converting decimal to binary
Repeatedly divide by 2 and write down the remainders right to left until you get to 1/2 = 0 + 1
69/2 = 34 + 1
34/2 = 17 + 0
17/2 = 8 + 1
8/2 = 4 + 0
4/2 = 2 + 0
2/2 = 1 + 0
1/2 = 0 + 1
Read this “backwards” to get 1000101
Converting decimal fractions into binary
Multiply the number by 2 and if the result is equal to/greater than 1, subtract 1 and write down a “1”, otherwise write down “0”. Repeat until you reach 1.
0.40625 * 2 = 0.8125, 0
0.8125 * 2 = 1.625, 1
0.625 * 2 = 1.25, 1
0.25 * 2 = 0.5, 0
0.5 * 2 = 1.0, 1
0.40625 = 0.01101
Converting base n to base m
<Compiete>
</Compiete>
Multiplying binary numbers
<Compiete>
</Compiete>
Signed Magnitude Representation for representing negative numbers
Add a bit at the beginning representing the sign (0 = positive, 1 = negative)
0000 0110 = 6
1000 0110 = -6
Has two zero values, makes binary arithmetic difficult
Ones complement for representing negative numbers
To make a number negative, flip all its bits
(e.g. 0100 1001 = 73, 1011 0110 = -73)
The first bit still represents the sign, still has two ways of writing zero
Twos complement for representing negative numbers
Make a number negative by flipping its bits and adding 1
(e.g. 0100 1001 = 73, 1011 0110 + 1 = 1011 0111 = -73)
The first bit still represents the sign
Makes binary arithmetic simple, only one representation for zero
Bias notation for representing negative numbers
Stores a number N as an unsigned value N+B, where B is the bias (half the unsigned range)
e.g. -65 is represented as -65 + 127 = 62 (0011 1110)
-8 is represented as 119 (0111 0111)
2 is represented as 129 (1000 0001)
Structure of a single precision 32-bit floating point number
+/- M * 2^e
1-bit sign, 8-bit exponent e, 23-bit mantissa M
The exponent is in the range -126 to 127, and is stored with a bias, meaning the number which gets converted into binary is actually between 1 and 254
Floating numbers with special meanings
Normal numbers have exponents between 1 and 254 (e = -126 to 127)
exponent 0 and mantissa 0 means 0
exponent 0 and mantissa not 0 means subnormal number
exponent 255 with mantissa 0 means +/- infinity
exponent field 255 with mantissa not 0 means NaN
How is the mantissa stored?
Scaled so that it starts with 1.xxxxxx
The leading 1 is then removed, meaning we only need to store 23 bits.
Converting a number (-12.375) into floating point
12.375 = 1100.011
Keep moving the decimal place until the number becomes 1.xxxxx
1100.011 = 1.100011 * 2^3
Sign bit is 1 (negative)
Get rid of the .1
Mantissa part is 1000110000000…
Exponent part is 3+127 = 130 = 1000 0010
11000001010001100000000000000000