Deep understanding of the representation of integers inside a computer

Source: Internet
Author: User

1. Representation of unsigned integers we know that unsigned integers are stored in binary form inside the computer, for example, we declare and initialize a variable in the C language:
int i = 66;
     Suppose that the machine we are targeting is a 32-bit machine, and the byte order is little endian. Since the int type is 32 bits, the number 66 is stored as its 32-bit binary representation (01000010 00000000 00000000 00000000), which occupies 4 storage units.     In this way, a 32-bit binary number can represent an unsigned integer that has a 2^32 range of [0, 2^32-1]. However, we need computer computing to often contain negative integers, so the computer can "recognize" negative integers first. This requires us to develop a rule that, for a given 32-bit binary number, this rule should be able to accurately indicate whether it represents a negative integer or a positive integer, and further describes what the value is. In this demand, people first invented the original code, which can represent both positive and negative integer encoding rules. The rule of the original code is very simple, take the 32-bit binary number, the most significant bit (most significant BIT,MSB) is defined as the sign bit, 1 words this number is a negative, 0 words this number is a positive, the remaining 31 bits to indicate the value of this number. In other words, the original code splits 32 bits into 1-bit sign bits and 31-bit numeric bit sequences.     However, there are two problems with the original code, one is that the value 0 is expressed in two ways: one is the MSB is 1 the remaining bits are 0 (-0), one is the MSB is 0, the remaining bits are 1 (0), and one more headache is that the opposite number of two numbers (except 0) add not equal to 0 ... So people invented the anti-code. The inverse code is also the MSB as the sign bit, then the remaining 31 bits, the lower 31 bits of a number is reversed, you can get its corresponding inverse number. In the code system of Anti-code, two of the inverse number of the addition of the number is equal to 0, but the value of 0 is still two forms, a 32-bit 0 +0, and a 32-bit all 1-0.     We don't just want the value 0 to have a binary representation, but we also want the signed and unsigned numbers to use the same set of adders and multipliers. Based on these demands, the complement was born.  2. Complement representation      in the complement representation, the range of 32-bit binary numbers can be represented as integers in two halves, half to negative integers, and half to nonnegative integers. Then it is natural to divide it into half-way by 0 or one of the MSB. In complement notation, the MSB is 1 to indicate that the number is a negative integer and 0 is a non-negative integer.   (1) signed integers and unsigned integers      Let's first clarify the concept of unsigned integers and signed integers. First of all, what we need to know is, the computer does not know the sign and unsigned, for the computer, signed and unsigned integers are a sequence of bits. Whether a sequence of 32-bit bits represents a signed or unsigned number is entirely context-determined. For example, there is such a sequence of bits in memory: 11000000 00000000 00000000 00000000. If we now know that it represents an integer, can we know whether it is positive or negative? The answer is no. Unless we know that this bit sequence is the value of an int variable, then we can know that it is a signed integer, so this sequence represents-64, and if this is a unsigned variable, its value is 193. So the so-called a signed integer and an unsigned integer, which is a context。 In the context of the signed integer, we say that 11000000 00000000 00000000 00000000 represents-64, because its sign bit is 1, and in the context of an unsigned integer, all the numbers are considered positive, there is no sign bit at all,    So the same bit sequence represents 192. (2) Why the choice of complement expression of the superiority of the complement expression, we first want to understand a concept: congruence operation. Before that, we first introduce the concept of the modulo operation (MoD), the difference between the modulo operation and the take-rest operation (generally denoted by "%") is that the calculation quotient of the remainder operator is rounded to 0, and the modulo operator calculates the negative infinity. For example, 3 of 5 of the value-3, the calculation process is the quotient-0.6 to 0 rounding to get 0, and then-3-(0 * (-3)) to get the result of-3. While the value of -3 MoD 5 is 2, the calculation process is to get the quotient-0.6 to take the whole get-1, then-3-(-1 * 5) gets 2. The addition and multiplication units of modern computers are actually "congruence operations". In a 32-bit system, we define 2^32 as the modulo of this congruence system. We use the same residue to represent a congruence operation, and "≡" is the congruence symbol, equivalent to the equal sign in the equation. Adder Calculation 1 + 1 is actually the same addition, roughly divided into the following three steps:
    1. A≡1 (mod 2^32)
    2. B≡1 (mod 2^32)
    3. C≡a + b≡2 (mod 2^32)
So let's take a look at the 32-bit system where the result range (x mod 2^32) is [0, 2^32-1]. In particular, we note: 2^32-1≡-1 (mod 2^32) ≡2^32-1 (mod 2^32), that is, 1 and 2^32-1 are the same, the same, 2 and 2^32-2,-3 and 2^32-3 are the same.    Then we can define 11111111 11111111 11111111 11111111 represents-1 and 2^32-1, the former is in the context of the signed integer, which is in the context of an unsigned integer. Let's look at the relationship between X and X. In this congruence system. Assuming X is a 32-bit sequence, it is easy to know that X + ~x will get 11111111 11111111 11111111 11111111. With the same redundancy expression is: X + ~x≡2 ^32-1 (mod 2^32). and X + ~x + 1≡2^32 (mod 2^32) = 0 (mod 2^32). So in this congruence system we can get-x≡~x + 1 (mod 2^32). This is the complement of a negative integer that we often say is the complement of its opposite number, which is represented by a bitwise reversal plus one.

Deep understanding of the representation of integers inside a 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.