Why is int8 in the range of [- 128127]


Today’s article is very basic.

I saw it the other daygoA topic is actually different fromgoIt doesn’t matter much in itself.

func main() {

What is the value of B? If 128 is said directly, it may need to be supplemented. After all, int8 is in the range of – 128127.

The correct answer to this question is – 128. So here comes the question…:

  • Why does int8 range from – 128127?

  • Why – 128?

Before we start, let’s make a brief introduction.

The binary representation of a number in a computer is called the machine number of the number. The machine number is signed. The computer uses the highest digit to store the symbol. The positive number is 0 and the negative number is 1.

For example

  var number int8 = 3

We define a variable of number, whose type is int8. If it is converted to binary, it is 00000011. If it is – 3, then the binary is 10000011. The number of machines here is 10 million 11 and 10 million 11.

Because the first bit of the machine number is the sign bit, the formal value of the machine number is not equal to the real value. For example, the highest bit 1 of 10000011 above represents negative, and the real value is – 3 instead of 131 (10000011 binary converted to decimal equals 131). Therefore, in order to distinguish, the real corresponding value of machine number with sign bit is called the true value of machine number.

The true value of 0000 0001 is + 000 0001 = + 1

Let’s go on to learn about the original code, the inverse code and the complement code.

Original code

The original code is the absolute value of the sign bit plus the true value. Like this one

[+ 1] original = 0000 0001

The original code is the easiest to understand. Because the first bit is a sign bit, the value range of 8-bit binary source code is

[11111 11110111 1111] that is [- 127127]

Inverse code

The inverse code of a positive number is itself, while the inverse code of a negative number is based on its original code, with the sign bit unchanged and the other bits reversed

[+ 1] = [0000 0001] original = [0000 0001] reverse

In this case, if an inverse code represents a negative number, you can’t see its value directly. You usually need to convert it into the original code for calculation.


The complement of a positive number is itself. The complement of a negative number is based on its original code, the sign bit remains unchanged, the other bits are negated, and finally + 1. That is, on the basis of the inverse code + 1.

Why is int8 in the range of [- 128127]

In the same way, the human brain can not see the value directly in the form of complement code, so we need to convert it into the original code and then calculate its value.

It can be seen from the above that the original code, the inverse code and the complement code are completely different. Only the original code is directly recognized by the human brain and used to calculate the expression. Why do we need inverse and complement?

For the computer, addition, subtraction, multiplication and division is already the basic operation. To design it as simple as possible, it is obvious that the recognition of “symbol bit” by the computer will make the basic circuit design of the computer more complicated. So I thought of the symbol bit also involved in the operation. We know that, according to the algorithm, subtracting a number is equal to adding a negative number. 1-1 = 1 + (- 1) = 0, so the computer can only add without subtraction.

Let’s first look at the original code, decimal expression: 1-1 = 0

Why is int8 in the range of [- 128127]

If the original code is used to represent the symbol bit, it is obvious that the result is not correct for subtraction. This is why the computer does not use the original code to represent a number.

Then, in order to solve the subtraction problem of the original code, the inverse code appears

Why is int8 in the range of [- 128127]

It can be found that if the inverse code is used to calculate the subtraction, the true value of the result is correct, but it raises a new problem. Although + 0 and – 0 are the same in understanding, the sign of 0 is meaningless. And there will be two codes [0000 0000] and [100000000] to represent 0.

The complement is finally coming out.

Why is int8 in the range of [- 128127]

In this way, if 0 is represented by [0000 0000], the previous – 0 problem does not exist, and it can be represented by [10000000] – 128:1

Why is int8 in the range of [- 128127]

In the result of complement operation, the complement value of [100000000] is – 128. In fact, the complement of – 0 is used to represent – 128, so – 128 has no representation of original code and inverse code. This is why int8 uses the original code or inverse code to represent the range [- 127127]. Using complement code, not only fixed the sign of 0 and the problem of two codes, but also expressed a minimum number.

Well, let’s go back to the problem itself. Because var B int8 = – 128 / A is not a constant expression, the untyped constant – 128 is implicitly converted to type int 8 (the same as a), so the result of – 128 / A is type int 8 and the value is 128. It is beyond the scope of int8 because the result is not a constant and overflow is allowed. The binary expression of 128 is [100000000], which is exactly the complement of – 128, so the answer is – 128.

Reference article

This work adoptsCC agreementReprint must indicate the author and the link of this article

Wu qinkuli

Recommended Today

Log4j file output

Write a class that dynamically generates properties (in the form of log4j.properties loaded into memory) and specifies that anything can be dynamically modified!====================== The directory cannot be created, but the log file is created automaticallylog4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=C:\bea\user_projects\domain\log ============================== You can write the configuration information of log4j in a file, such as sort. PropertiesThen the servlet is […]