Computer Composition Principles | Understanding Binary Code

binary conversion

Binary --> Decimal :

  • The digit from right to left Nis multiplied by a power of 2 Nand then added up to become a decimal number.
  • For example, the binary number: 0011 , the corresponding decimal representation is 0 × 2 3 + 0 × 2 2 + 1 × 2 1 + 1 × 2 0 = 3 0×2^3+0×2^2+1×2^1 +1×2^0=30×23+0×22+1×21+1×20=3 , stands for decimal3 33

Decimal --> Binary :

  • short division

    1. Divide the decimal number by 2 and record the remainder.
    2. Continue to divide the quotient by 2 and record the remainder again.
    3. Repeat this process until the quotient is 0.
    4. Arrange the recorded remainder in reverse order to get the corresponding binary number.

For example, to convert the decimal number 23 to binary:

  • 23 ÷ 2 = 11 remainder 1
  • 11 ÷ 2 = 5 remainder 1
  • 5 ÷ 2 = 2 remainder 1
  • 2 ÷ 2 = 1 remainder 0
  • 1 ÷ 2 = 0 remainder 1

Then, put the remainder of the records in reverse order: 10111

Binary addition operation

The rules for binary addition are as follows:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 0, resulting in a carry

For example:

   1 0 1 1     (1011)
+  0 1 0 1     (0101)
------------
   1 1 0 0     (1100)
  • Adding from the rightmost bit, according to the rule, 1 + 1 = 0, and a carry is generated. This carry needs to be added to the addition result of the next bit. Add them up one after another, and finally get the result 1100.
  • If the two binary numbers to be added have different digits, their digits need to be aligned before being added. You can pad the shorter binary numbers with zeros on the left to make them have the same number of digits before adding them

Binary subtraction operation

Subtraction steps

  • Binary subtraction can be done by converting the subtraction to addition, where the addition is done using two's complement form, and the carry is handled.

Here are the steps for binary subtraction:

  1. Convert the subtrahend to its complement form (if the subtrahend is negative).

    • If the subtrahend is positive, no conversion is required.
    • If the subtrahend is negative, it needs to be converted to its complement form. The conversion method is to first take the binary representation of its absolute value, then invert it bitwise (0 becomes 1, 1 becomes 0), and finally add 1.
  2. Binary addition of subtrahends and minuends using two's complement form.

    • Follow the same steps as binary addition, adding bit by bit starting with the lowest bit (rightmost) and handling carry.
  3. Check the highest bit (leftmost bit) of the result to determine its sign.

    • If the highest bit is 1, the result is a negative number.
    • If the highest bit is 0, the result is a positive number.
  4. If the result is negative, convert the result to positive form.

    • Invert the highest bit of the two's complement form of the result (0 becomes 1, 1 becomes 0) to get the complement.
    • Add 1 to the complement to get the positive form.
  5. The final result is the result of subtraction.

Subtraction example

Suppose we want to calculate the result of -5addition 1:

-5 + 1 = ?
  1. will 被减数(-5)convert to two's complement form:

    First, take its inverse:1010

    Then, add 1 to the inverse:1011

    So, the complement of -5 is:1011

  2. 1The binary representation of is:0001

Now, we will perform the binary addition operation:

  1011  (-5)
+ 0001  (+1)
----------

Add starting from the rightmost bit:

  • First digit (lowest digit): 1 + 1 = 10. Writes 0 to the current bit, and 1 as the carry.
  1011  (-5)
+ 0001  (+1)
----------
     0
  • Second bit: 1 (carry) + 1 + 0 = 10. Writes 0 to the current bit, and 1 as the carry.
  1011  (-5)
+ 0001  (+1)
----------
    00
  • Third bit: 1 (carry) + 0 + 0 = 1. Write 1 to the current bit.
  1011  (-5)
+ 0001  (+1)
----------
   100
  • Fourth bit: 1 + 0 = 1. Write 1 to the current bit.
  1011  (-5)
+ 0001  (+1)
----------
  1100
  • The highest bit of the final result is 1, indicating that the result is a negative number, so: 1100 is a negative number.

Next, we need to convert the result to positive form:

  1. Take the one's complement of two's complement: 0011.
  2. Add 1 to the inverse code to get: 0100.

Finally got: -5 + 1 = -4.

Table: Binary addition

(C:\Users\22319\AppData\Roaming\Typora\typora-user-images\image-20230615163731785.png)]

references

Guess you like

Origin blog.csdn.net/YuvalNoah/article/details/131234936