Detail the byte type in java

  • 2020-06-15 08:26:13
  • OfStack

introduce

byte, or bytes, consists of eight bits of base 2. In Java, data of type byte is 8-bit signed base 2.

In computers, the value range of 8-bit signed 2-base Numbers is [-128, 127], so in Java, the value range of byte type is also [-128, 127].

Value range analysis

1 is always wondering why not -128 to 128? Today, I analyzed 1 of this problem.

The first thing we need to understand is the rules of arithmetic:

#######################################################################

The highest bits of positive #### Numbers are 0, and positive Numbers are the values in base 2. # # # #

The highest bits of negative Numbers are all 1's, negative Numbers are going to be 1's plus 1's plus a minus sign to get the value. # # # #

#######################################################################

Let's illustrate this rule in base 2 of 8 bits:

For example: 00000001. The highest digit is 0, which is positive, so it's 1 in decimal.

Another example: 10000001. The highest digit is 1 is negative, what is the value? You get 01111110 plus 1 is 01111111, so it's -127

To understand this, let's start with byte. byte happens to be an 8-bit base 2 number. short is 16 bits int is 32 bits long is 64 bits.

It is not difficult to understand that the largest positive number for byte is 01111111 (the highest digit must be 0), which is 127.

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

Big mistake. Let's see what 11111111 means in base 2.

So we know that this is a negative number based on the tip above. It's going to be the inverse plus 1.

Take the reverse of 11111111 and get 00000000, add 1 and get 00000001. And we end up with a value of minus 1.

That's the biggest negative number. So do you think the smallest negative number is going to be 10 million?

So let's do 1 and let's do the reverse: 01111111 plus 1 to get 10 million and then we get -128.

127 is 01111111 whereas -128 is 10000000. See a strange thing.

So let's see if we can add 1 to the base 2 to get the base 2? right

One small program can be programmed for experiment 1:


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

That's exactly -128

From this we can see that the base 2 goes from 00000000 to 01111111 to 10000000 to 11111111

So we're going from 0 to 127 to minus 128 to minus 1 in base 10

Let's take a closer look at byte in one piece of code:


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

The above code will output -56. The reasons are as follows:

The base 2 of 200 is 111001000, and since int is in 32-bit base 2, it's actually 00000000000... 111001000. When int switches to byte, the computer will only keep the last eight bits, which is 11001000.

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

conclusion

The above is the whole content of this article, I hope the content of this article can bring 1 definite help to your study or work, if you have any questions, you can leave a message to communicate.


Related articles: