Write in front: the blogger is a real combat development after training into the cause of the “hill pig”, nickname from the cartoon “Lion King” in “Peng Peng”, always optimistic, positive attitude towards things around. My technical path from Java full stack engineer all the way to big data development, data mining field, now there are small achievements, I would like to share with you what I have learned in the past, I hope to help you on the way of learning. At the same time, the blogger also wants to build a perfect technical library through this attempt. Any anomalies, errors and matters needing attention related to the technical points of the article will be listed at the end, and everyone is welcome to provide materials in various ways.
- Please criticize any mistakes in the article and revise them in time.
- If you have any questions you would like to discuss or learn, please contact me at [email protected].
- The style of the published article varies from column to column, and all are self-contained. Please correct the deficiencies.
Why is the storage range of a byte from -128 to 127?
This article keywords: byte, byte, binary, inverse, complement \
The article directories
- Why is the storage range of a byte from -128 to 127?
-
- A byte,
- Second, inverse and complement
-
- 1. Radix-minus-one complement
- 2. Complement
- Byte data range
A byte,
In a computer, a binary bit is the smallest place of storage, and since it is binary, only zeros and ones can be stored. Obviously, it would be a cumbersome process to manipulate each bit directly, so we use other data types directly in programming, such as byte, int, float. These data types can make our data storage much easier, we only need to care about how wide and what kind of data they can store. So a byte, which is what we call a byte, takes up 8 bits of space.
- 1 byte = 8 bits
These eight bits are eight binary bits, and one of them is a sign bit, which happens to be a 0 and a 1 for plus and minus. So how wide a range of numbers can these eight bits represent? For the positive conversion of the base I believe that it is not difficult to everyone, you can also refer to the related article at the bottom, we first look at how negative numbers in binary representation and conversion.
Second, inverse and complement
Let’s put the formula here:
- Complement of a positive number = source = inverse
- The complement of a negative number = the inverse + 1
So what is the source code in the first place? Very simple, in the case that we do not consider the sign, according to the base conversion method of a decimal number into a binary number, and then add the corresponding sign bit to complete. The sign bit appears in the first (left) bit, with 0 representing a positive number and 1 representing a negative number.
- +3 -> 11 -> Complete by symbol and byte length: 0000 0011
- -5 -> 101 -> Complete by symbol and byte length: 1000 0101
So why are the concepts of inverse and complement proposed? There are two reasons:
1. Radix-minus-one complement
Ensure normal operation between positive and negative numbers in binary.
First let’s look at what happens when we use source code for positive and negative numbers. 5+(-3)=2, then the operation in binary is directly added as in decimal. The carry is carried, but the sign bit cannot directly participate in the operation. In other words, it is difficult for the computer to directly determine the positive and negative of the final expression. Source code: 0000 0011 + 1000 0101 = 1000 1000, the result is: -8 (don’t worry about what the final symbol bit should be, because this method is not used in computers, just for example). Obviously, using raw code is fine for positive numbers, but not for negative numbers, so we need to redefine a rule to handle negative numbers. Since it is no problem to calculate in positive numbers, it can be stipulated that the inverse of positive numbers is equal to the original code, and the inverse of negative numbers is minus the sign bit, and the rest is negative.
- +3 -> Original code: 0000 0011 -> Inverse code: 0000 0011
- -5 -> Source code: 1000 0101 -> Inverse code: 1111 1010
Reverse code calculation: 1111 1101 -> source code: 1000 0010 -> decimal: -2. Well, that doesn’t seem to be a problem, but there can be problems when a positive and a negative result is positive (+5 and -3, you can check for yourself) or when it happens to be zero.
2. Complement
Conflict between +0 and -0.
We know from the concept of negation that a positive number must have a corresponding negation, and for integers we just change the sign bit. But!!! This 0 is special, and there is a well-known concept: the negative of 0 is still 0, which directly leads to the conversion of the base is not one-to-one correspondence.
- +2 -> Original code: 0000 0010 -> Inverse code: 0000 0010
- -2 -> Source code: 1000 0010 -> Inverse code: 1111 1101
Reverse code calculation: 1111 1111 -> source code: 1000 0000 -> decimal: -0. The result looks correct, but in positive numbers: 0000, 0000 also represents 0. So for 1,000, which cannot be erased, let it represent a special number: -128. For example, in Java, short occupies two bytes, and the highest bit is the sign bit. Then this number will appear: 1000 0000 0000 0000 0000, which is also -0 from the source code. What do we do if the sign bit is 1 and all the other bits are 0? Thus, in order to solve this kind of problem, there is the concept of complement: the complement of a negative number = inverse + 1. We use the critical position calculation as an example, for example :(-2)+(-126)=-128.
- -2 -> Source code: 1000 0010 -> Inverse code: 1111 1101 -> Complement code: 1111 1110
- -126 -> Source code: 1111 1110 -> Inverse code: 1000 0001 -> Complement code: 1000 0010
- Result of complement operation: 1000 0000 (this kind of numbers have no original code and inverse code)
At this point, it’s time to knock on the blackboard and underline a key point: in memory, all calculations are performed using complement (integer types)!
Byte data range
With the above concepts in mind, the scope of byte should be clear.
- Maximum positive number: 0111 1111 -> 2^ 7-1 = 127
- Maximum negative number: 1000 0000 -> -2^7 = -128
The same is true for the range of other positive types:
- Short in Java: 2 bytes -> -2^15 ~ 2^ 15-1
- Int: 4 bytes -> -2^31 ~ 2^ 31-1
- Long in Java: 8 bytes -> -2^63 ~ 2^ 63-1
Data types defined by different languages take up different amounts of bytes in memory, and there is no need to remember these ranges directly. It is generally fine to remember bytes.
- List of related articles:
Students who are not clear about the conversion of base can directly answer: How to convert between bases