Binary


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
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.

Binary to Decimal, e.g. 101111:
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
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

Binary Fraction to Decimal Fraction, e.g. 0.1011:
1 * 2^(-1) + 0 * 2^(-2) + 1 * 2^(-3) + 1 * 2^(-4) = 0.6875

Negative Using 8 bits
 Positive
 0000,1001  //to show number 9
 Signed magnitude 1000,1001  //just set the bit sign in the left most bit
 Signed 1's complement
 1111,0110   //invert all bits, This scheme has two representations for 0 (0 and -0)
 Signed 2's complement 1111,0111   //start from right, leave all zeros until  you face 1, leave that one, invert all the rest to the left
  


Add
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.



Overflow:


In an 8 bit register you can only hold integers from +127 to -128






Comments