The author | rashly stone – GO!

Source | https://www.cnblogs.com/zl181015/p/9435035.html#4432849

Java also provides a byte data type, and this is the basic type. Java byte is treated as the smallest number, so its range is defined as -128 to 127, or signed byte. The following article mainly introduces the Java byte type related information, need friends can refer to below.

introduce

Byte, or byte, consists of eight bits of binary. In Java, data of type byte is an 8-bit signed binary number.

In computers, 8-bit signed binary numbers are in the range of [-128, 127], so in Java, the byte type is also in the range of [-128, 127].

Value range analysis

I keep thinking why not -128 to 128? Today we analyzed the problem.

The first thing we need to understand is the algorithm:

The highest bit of a positive number is 0, and the value of a positive number is the binary value.

Negative values are always 1, and negative values are the ones you take the reverse and you add one and you put a minus sign and you get the value.

Let’s use 8-bit binary to illustrate this rule:

For example, 00000001. If the highest digit is 0, it is a decimal 1.

Another example: 10000001. If the highest digit is 1 and the value is negative, what is the value? If you do the reverse, you get 01111110 plus 1 you get 01111111, so it’s minus 127

To understand this operation, let’s say byte. Byte is exactly an 8 bit binary number. Short is 16 bits int is 32 bits long is 64 bits.

Understandably, the maximum positive number of bytes is 01111111 (the highest bit must be 0), which is 127.

So you might think that the smallest negative number of bytes is 11111111, right? So want to

Big mistake. Let’s see what the binary number 11111111 represents.

We know from the above that this is a negative number. It’s going to be the reverse and then add 1.

If 11111111 is reversed, you get 00000000. If you add 1, you get 00000001. And then you end up with a value of minus 1.

This is the largest negative number ever. From this, did you think that the smallest negative number would be 10 million?

So let’s do it the other way around: 01111111 plus 1 is 10 million and we get -128.

127 is 01111111 whereas -128 is 10000000, which is a strange thing to see.

Take a closer look at the two binary numbers. Is the one plus one the other? right

Can make a small program experiment:

byte a = 127; 
a+=1; 
System.out.println(a);Copy the code

That’s exactly -128

From this we can see that the binary is 00000000 to 01111111 to 10000000 to 11111111

That’s 0 to 127 to -128 to -1 in decimal.

Next, let’s take a closer look at byte with a piece of code:

public class A { public static void main(String[] args) { int b = 456; byte test = (byte) b; System.out.println(test); }}Copy the code

The above code will print -56 at the end. Here’s why:

The binary representation of 456 is 111001000, and since int is a 32-bit binary, in computers, it’s actually 00000000000… 111001000, when an int is converted to a byte, then the computer keeps only the last eight bits, which is 11001000.

Then the highest bit of 11001000 is 1, so it represents a negative number, and negative numbers are saved in the form of complement in the computer. Therefore, we calculate that the original code of 11001000 is 00111000, that is, 56, so 11001000 represents -56, so the final value of test is -56.

If the article is helpful to you, please remember to like and follow yo ~

Welcome to pay attention to my public number: love IT, daily push technical articles for you to learn and reference.