Original code, anti-code, complement, the expression of negative numbers in the computer

Source: Internet
Author: User

  Original code:Converts an integer to binary, which is its original code.

The original code for the single-byte 5 is: 0000 0101;-5 The original code is 1000 0101.

 Anti- code: The inverse of a positive number is its original code, negative number of the inverse code is the original code,In addition to the sign bit, each bit is reversed.
such as the single-byte 5 of the Anti-code is: 0000 0101;-5 The Anti-code is 1111 1010.

 complement: a positive complement is its original code;negative anti-code +1 is the complement。
If the complement of the single byte 5 is: 0000 0101;-5 The original code is 1111 1011.

In a computer, a positive number is expressed directly in the original code, such as single Byte 5, which is represented in the computer as: 0000 0101.
A negative number is a complement, such as a single-byte-5, represented in a computer as 1111 1011.

here is a question, why in the computer, negative numbers in the complement of the expression? Why not just use the original code to express? such as single byte -5:1000 0101.
  
I want to consider it from the software for two reasons.

1. Indication Range
For a single-byte integer, the unsigned type, which represents a range of [0,255], represents a total of 256 data. A signed type whose representation range is [-128,127].
Look at unsigned first, 0 means 0000 0000,255 is 1111 1111, just meet the requirements, can represent 256 data.
Look at the symbol, if the original code, 0 is expressed as 0000 000. Because we have symbols, we should also have a minus 0 (although it is still 0): 1000 0000.

Well, let's see if that's enough to meet our requirements, say 256 data?
Positive, no problem, 127 is 0111 1111,1 is 0000 0001, of course, the other should be no problem.
Negative, 1 is 1000 0001, then the minus, the largest number is 111 1111, which is 127, so the smallest data in negative numbers is-127.
This seems to be not right, how to express-128? Seemingly direct use of the original code can not be expressed, and we have two 0.
If we designate one of the 0 as-128, can't we? This is also an idea, but there are two problems: one is that it is too large with the 127 span, and the other is that it is inconvenient to operate with hardware.
Soin a computer, a negative number is indicated by a complement。
such as single-byte-1, the original code is 1000 0001, the anti-code is 1111 1110, the complement is 1111 1111, the computer's single-byte 1 is represented as 1111 1111.

Single-byte-127, the original code is 1111 1111, anti-code 1000 0000, the complement is 1000 0001, the computer's single-byte-127 is expressed as 1000 0001.
  
Single-byte-128, the original code does not seem to show, in addition to the symbol, the maximum number can only be 127, its representation in the computer is 1000 0000.

Original code, anti-code, complement, the expression of negative numbers in the computer

Related Article

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.