In-depth understanding of java byte type


Author | attack stone --GO!
Source | https://www.cnblogs.com/zl181015/p/9435035.html#4432849

Java also provides a byte data type, and is the base type. java byte as the smallest number is handled, it is defined as the range of -128 to 127, which is signed byte. The following article is mainly to introduce the relevant information concerning the type of byte in java, need friends can refer to.

Introduction

byte, i.e. byte, 8-bit binary composition. In Java, byte type 8-bit binary data is signed.

In the computer, in the range 8-bit unsigned binary number is [-128, 127], so that in Java, byte type is in the range [-128, 127].

Analysis of the range

Why I not have been thinking about -128 to 128 it? Analysis of this question today.

First we have to understand one thing, that is the operation rules:

Highest positive number is 0, the value of a positive number is the value of the binary representation.

1 MSB are negative, a negative value is negated after a plus and minus-sign Found obtained.

We use 8-bit binary to explain this rule:

For example: 00000001. Most significant bit is 0 is positive, then the representation is decimal 1.

Another example: 10000001 most significant bit is 1 is negative, the value is the number? Was added to give invert 01111110 01111111 1 obtained, the value of -127

Understand the rules we started this operation, said byte, byte exactly 8-bit binary number. 16 is a short int 32-bit is 64 bits long.

Not difficult to understand, byte maximum positive number is 01111111 (the highest bit must be 0), which is 127.

Then you might want byte minimum negative number is 11111111, is not it? Think it

Wrong. Let's look at this binary number 11111111 much.

According to the above tips We know that this is a negative number. Its value is first inverted plus 1.

11111111 negated to give: 00000000, 00000001 1 was added. The resulting value of -1.

This is the biggest negative ah. Thus you are not thought of the smallest negative number will be 10 million it?

Let us count negated: 01111111 add 1 to get 10 million last get -128.

127 -128 is however 01111111 10000000 seen a strange thing.

A closer look at the two binary numbers plus 1 is not the former the latter get it? Correct.

You can compile a small program about the experiment:

byte a = 127; 
a+=1; 
System.out.println(a);复制代码

The result is just -128

From this we can see that the binary from 00,000,000 to 01,111,111 from 10,000,000 to 11,111,111 Dao

I.e., from 0 to 127 decimal to -128 to -1.

Next, we use a piece of code to a deeper understanding byte:

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

The above code, the final output will be -56. For the following reasons:

456 binary representation is 111 001 000, because the int is 32-bit binary, so in the computer, in fact 00000000000 ...... 111 001 000, when converted into byte int time, the computer will only retain the last eight, that is 11001000.

Then the most significant bit of 11001000 is 1, is represented by a negative number, the negative number is stored in the form of complement in the computer, so we calculate 11001000 00111000 original code, i.e. 56, it is represented 11001000 -56, so the final test of the value of -56.

If the article helpful, please remember thumbs attention yo ~

Welcome to my public concern number: The feelings of IT, technical articles for daily push them to learn.

Guess you like

Origin juejin.im/post/5ddb339f51882573123ce30f