To encode numbers in binary format there are a set of rules that must be fulfilled: Decimal to Binary, e.g. 47: 47 ÷ 2 = 23, remainder = 1 Binary to Decimal, e.g. 101111:23 ÷ 2 = 11, remainder = 1 11 ÷ 2 = 5, remainder = 1 5 ÷ 2 = 2, remainder = 1 2 ÷ 2 = 1, remainder = 0 1 Wrap up the remainders from bottom up, 47 in decimal is equal to 101111 in binary notation. 1 × 2^5 + 0 × 2^4 + 1 × 2^3 + 1 × 2^2 + 1 × 2^1 + 1 = 47 Decimal Fraction to Binary Fraction, e.g. 0.6875: 0.6875 * 2 = 1.375, non-fractional part = 1 //remove one Binary Fraction to Decimal Fraction, e.g. 0.1011:0.375 * 2 = 0.75, non-fractional part = 0 0.75 * 2 = 1.5, non-fractional part = 1 0.5 * 2 = 1.0, non-fractional part = 1 Weap up remainders from top to bottom: 0.1011 1 * 2^(-1) + 0 * 2^(-2) + 1 * 2^(-3) + 1 * 2^(-4) = 0.6875 Negative Using 8 bits
Positive-Positive is ok Positive-Negative in signed magnitude, compare the signs and then perform addition or subtraction as you do in decimal in Signed 2's complement: no matter addition or subtraction or the sign of the operands, add the two numbers and ignore the carry. To check if the result fits in the number of bits required, the XOR of the carry going to the last bit and the carry caused by the last bit should be 0. In an 8 bit register you can only hold integers from +127 to -128 |
Tech in T: depth + breadth > OS >