Thoughts on the source code, reverse code, complement code, and left-right shift

Source: Internet
Author: User

Tag: Complement

1. Why do I need to use the original code, reverse code, and complement code?
1) How is negative binary representation? A: In the original code, a value of 1 indicates a negative number.
2) How can computers perform subtraction? 1-1 =? A: For computers, the logic operation should be simple. 1-1 can be expressed as 1 + (-1), so there is no subtraction. Subtraction is implemented using addition.

1-1 = 1 + (-1) = [1, 00000001]ORIGINAL + [2, 10000001]Original = [2, 10000010]Original =-2

If the original code is used for calculation, the result is incorrect for subtraction. That is why the computer does not use the original code to represent a number.

In order to solve the problem of original code subtraction, there is a reverse code:

Decimal expression: 1-1 = 0

1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] + [1111 1110] = [1111 1111] = [1000 0000] =-0


3) How does the binary value of 0 indicate that + 0 is different from-0? Answer: + 0 and-0, but the complement code is 0. The computer uses the complement code. Therefore, the computer does not distinguish between + 0 and-0.
Reason: + 0 original codes, reverse codes, and supplementary codes are all 00000000
-The original code of 0 is 10000000, the reverse code is 11111111, and the supplementary code is 00000000.

The complement code solves the two Encoding Problems of the 0 symbol:

1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original

= [0000 0001] fill + [1111 1111] fill = [0000 0000] fill = [0000 0000] original = 0


4) 8 digits. What is the original code of-128? A:-128 does not have the original code. The original code is in the range of [-127, + 127]. The computer uses the Supplementary Code. Because-0 is removed, [1000 0000] is supplemented with-128. it can be used to represent the minimum value-128. The value range of the complement code is [-128,127].
2. Why is the left shift logical operation and the right shift arithmetic operation?
The Left shift is a logical operation, and the right shift is an arithmetic operation. The logical shift is performed by the mark bit (plus or minus sign), so the positive and minus signs may be changed. While the arithmetic shift is to fix the flag bit. Moving other digits will not change the plus or minus signs, but the first moved digit is 0 or 1. If the flag bit is 1, if both are supplemented with 1 and 0, all are supplemented with 0.
A: If the negative number is shifted to 0, the first is that the negative number is changed to a positive number. If the number is supplemented by 1, because the computer uses a complement representation
3. shift left is equal to * 2, and shift right is equal to/2. Is it suitable for negative numbers? A: It is suitable. The original misunderstanding is due to the original code and not the supplementary code, the computer uses the complement code for computation. The input number of the output is also a complement.
In this example, Int Is 32-bit and 4-byte, so there are 8 digits in hexadecimal notation.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.