In Java, why hexadecimal digits after the decimal number 0xFF corresponding negated -256 it?

int number = 0xFF;

Literal means without saving the variables in the program, can be directly expressed as a specific number or value strings.

0xFF is an integer literals, the default type is integer literals int.

We know that in Java, int is a 4-byte (32-bit) of the basic data types.

So 0xFF actually complete wording is 0x000000FF.


 

4 may represent a binary hexadecimal, then  0x000000FF  converted to binary wording is:

0000 0000 0000 0000 0000 0000 1111 1111

Bitwise:

  ~number

= ~0xFF

= ~0x000000FF

= 0xFFFFFF00【H】 = 1111 1111 1111 1111 1111 1111 0000 0000【B】;

The first is the sign bit, 1 indicates a negative number, so there should be a conversion , the conversion from a negative form to complement the original code, in order to obtain real value it wants to express.


 

Converted from complement to the original code, obtained by subtracting 1 to the inverted and inverted to obtain the original code.

The first step, the least significant bit minus 1:

. 1 1,111,111,111,111,111,111 complement 1111 0000 [0000] --1 =  . 1 1,111,111,111,111,111,111 code 1110111111111 [trans]

The second step, in addition to the sign bit, invert:

1 0,000,000,000,000,000,000 0001 0000 0000] [original code

After obtaining the original code, its value calculation part to give: 100,000,000 [B] = 2 . 8  [Q] = [Q] 256

Plus sign bit , the outcome is: -256

Therefore, the hexadecimal number 0xFF corresponding decimal number is negated after -256.


 

In summary, here are three related to knowledge.

First, in Java, the default type is integer literals int, it is composed by a 32bit;

Second, bitwise representation of a binary number ~ bitwise, i.e. 0 to 1, a 1 to a 0;

Second, in a computer system, there is the sign bit numeric code data is represented in the form of complement and storage. Complement itself it is positive; a negative complement code is converted to the original code, before the least significant digit by 1, and then everybody except the sign bit inverted, the original code is finally obtained.

Guess you like

Origin www.cnblogs.com/buildnewhomeland/p/12129834.html