Two's complement sum, original code, complement, bitwise "and", bitwise "or"

       First of all, numbers are represented in binary form in computers; numbers are divided into signed numbers and unsigned numbers; original code, inverse code, and complement code are all representations of signed fixed-point numbers; unsigned numbers are all processed as positive numbers ; the most significant bit of a signed fixed-point number is the sign bit, 0 is positive and 1 is negative.

1. Binary complement summation

rule:

The calculation is performed column by column from low to high. The addition of 0 and 0 is 0, the addition of 0 and 1 is 1, and the addition of 1 and 1 is 0 but a carry 1 is generated, which is added to the next column.

That is, 0+0=0; 0+1=1; 1+1=10.

If a carry is generated after the most significant bits are added, 1 is added to the final result.

Precautions:

  1. In the complement operation, the sign bit and the numerical value are involved in the operation.

  2. After the sign bits of the complement code are added, if a carry occurs, it will be sent back to the lowest bit for addition (circular carry).

  3. With the inverse code operation, the result of the operation is also the inverse code. When converting to a true value, if the sign bit is 0, the digit remains unchanged; if the sign bit is 1, the result should be negated to obtain its true value.

Two, two's complement, original code

rule:

The original code, inverse code, and complement code of positive numbers are the same.

Complement of negative number: the absolute value of the number is taken, then negated, and then +1 (add 1 to the complement).

For example, the absolute value of -1 is 1, that is, 0000 0001. After inversion, it is 1111 1110, and adding 1 becomes 1111 1111, which is the complement of -1.

Original code for negative numbers: Change the sign bit of the corresponding positive number to 1.

Precautions:

  1. With two's complement, the sign bit and other bits can be treated uniformly. At the same time, subtraction can also be handled as addition.

  2. When adding two complement-represented numbers, if there is a carry in the most significant bit (sign bit), the carry is discarded.

complement.jpg

3. Phase-by-phase "and" and phase-by-phase "or"

AND: if one is 0, it is 0, otherwise it is 1;

That is, 1 and 1=1, 1 and 0, 0 and 1, and 0 and 0 all=0.

or: 1 if one of them is 1, otherwise 0;

That is, 1 or 1, 1 or 0, 0 or 1 = 0, 0 or 0 = 0.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325163358&siteId=291194637