This is the first day of my participation in Gwen Challenge

The range in which 32 – bit signed integers can be represented

You know, minus 231 to 231 minus 1. The highest bit of the signed integer is 1 or 0 for the sign, and the remaining 31 bits for the value

Why is there one more negative than positive

The stored integers in the computer are represented by complement

  • Decimal 0 is binary 00…. 00 0 {32}

  • The highest bit of a positive number is 0, and positive numbers range from 00…. 01{32 zeros} to 011…. 111 {31}

  • The highest digit of a negative number is 1, and negative numbers range from 10…. 00 to 11… 111

There is a binary with the highest bit being 0 (00….) 00) is used to represent the decimal 0, not a positive number, so a negative number has one more than a positive number

Why do I write it in complement

The biggest advantage of the complement system is that it can be added or subtracted without using different calculation methods because of the positive and negative numbers. As long as an addition circuit can handle all kinds of numbered addition, and the subtraction can be represented by a number plus the complement of another number, so as long as there are addition circuit and complement circuit can complete all kinds of numbered addition and subtraction, in the circuit design is quite convenient

For example, for 3- 2,3 = 0011, -1 = 1111, the binary representation is 0011 + 1111 = 10010,10010 is obviously a negative number, not because of the wrong calculation, but because of the overflow of the result, originally only 4 bits, the sum of the result is 5 bits, so that is to directly remove the highest bit. So 0010 is equal to 1

In addition, there is only one representation of 0 in the complement system. If the highest bit is simply 1 or 0 to represent plus or minus, 0 can be represented in two ways: 00…. 00 or ten… 00, this is not only a waste, and to determine whether a number is equal to 0, you have to compare it twice, to see if it is 00…. 00 or ten… 00

The figure above shows the representation of integers in an 8-bit system

What exactly is the complement, and why is the complement of -128 10000000

-128=10000000, how to get, if according to our textbook, the complement of a negative number is equal to the inverse of a positive number plus 1, anyway, in the 8-bit system, 128 should be equal to 010000000, which is 9 bits

How does a computer distinguish between unsigned and signed

  • For binary 00000001, the unsigned number represents 1 and the signed +1

  • For binary 10000001, the unsigned number represents 129 and the signed number represents -127

If you add them up and you get 1000010 could be 130, it could be -126

So how does the computer distinguish between a signed number and an unsigned number? Well, the computer doesn’t distinguish between unsigned and signed. The computer only stores these three binary numbers, which are used by our compiler to distinguish the types of numbers. The one defined as signed is -126, and the one defined as unsigned is 130

reference

Zh.wikipedia.org/zh-cn/%E4%B…