The reason why byte and 0xff are involved in int conversion in Java

Source: Internet
Author: User

In Java, why does byte convert int with 0xff and perform operations?

See the following code before profiling the problem:
Public static string bytes2hexstring (byte [] B ){
String ret = "";
For (INT I = 0; I <B. length; I ++ ){
String hex = integer. tohexstring (B [I] & 0xff );
If (Hex. Length () = 1 ){
Hex = '0' + hex;
}
RET + = Hex. touppercase ();
}
Return ret;
}
The above is to convert byte [] to a hex string. Note that here B [I] & 0xff is used to calculate a byte and 0xff, and then integer is used. tohexstring gets the hexadecimal string. We can see that
B [I] & 0xff still produces an int. Why should I perform the operation with 0xff? Directly integer. tohexstring (B [I]);, can I convert byte to int? The answer is no.

The reason is:
1. The Byte size is 8 bits, and the int size is 32 bits.
2. the binary code of Java uses the complement form.

Here we will first review basic computer Theory

A byte is saved in one byte and has eight bits, that is, eight 0 s and one S.
The first bit of the 8-digit is the symbol bit,
That is to say, 0000 0001 represents number 1.
1000 0000 represents-1
So the maximum number of positive numbers is 0111, Which is 1111.
The maximum number of negative numbers is 1111, which is-1111.

The above is the binary original code, but in Java it adopts the form of complement code. What is the complement code below?

1. Anti-code:
If a number is positive, its anticode is the same as the original code;
If a number is negative, the symbol bit is 1, and the rest of the code is reversed;

2. Complement: with overflow, we can convert subtraction into addition.
For the decimal number, get 5 subtraction from 9:
9-4 = 5 because 4 + 6 = 10, we can use 6 as the complement of 4.
Rewrite to addition:
9 + 6 = 15 (remove the high 1, that is, minus 10) to get 5.

For hexadecimal numbers, subtraction from C to 5 is available:
C-7 = 5 because 7 + 9 = 16 will 9 as the complement of 7
Rewrite to addition:
C + 9 = 15 (remove high 1, that is, minus 16) to get 5.

In a computer, if we use one byte to represent a number, and one byte has eight bits, more than eight bits will enter 1. In the memory, this is (100000000), and carry 1 is discarded.

(1) If a number is positive, the original code, reverse code, and complement code are the same.
(2) A number is negative, and the just-Signed bit is 1. The rest of you will reverse the original code, and then add 1 to the entire number.

-The original Code of 1 is 10000001.
-The Anti-Code of 1 is 11111110.
+ 1
-The complement of 1 is 11111111.
 
The original code of 0 is 00000000
The bitcode of 0 is 11111111 (the bitcode of positive and negative Zeros is the same)
+ 1
The completion code of 0 is 100000000 (1 of the headers are removed, and the completion codes of positive and negative zeros are the same)

The parameter of integer. tohexstring is int. If & 0xff is not performed, when a byte is converted to int, the byte is supplemented because the Int Is 32 bits and the byte is only 8 bits,
For example, if the decimal number of the complement code 11111111 is-1 and the value is converted to an int, it will become more than 11111111111111111111111111111111! That is, 0xffffffff, but this number is incorrect. This complement will cause errors.
And 0xff, the 24-bit high will be cleared, and the result is correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.