How to represent negative numbers in binary, byte & oxff in the end why (byte) 0x97 = -105: java 2's complement

In the binary code in order to distinguish positive and negative numbers, a method using the most significant bit is the sign bit to distinguish the sign bit is a positive number of 0, 1 sign bit is negative. The remaining portion is the absolute value of this number, the original code can be used, the inverted, three forms of complement to represent an absolute value portion.
Original code simple, it's best to understand. Is the absolute value of the original binary code forms: for example, 8-bit binary +7 original code is 00000111, -7 original 8-bit binary code is 10000111.
But for the binary operation, the operation of the original code is not convenient enough, when the two numbers together, first it is determined whether the two numbers of the same symbols, symbols of different words, but also the absolute value of which is determined larger number. Therefore, in the computer, they are usually employed in the form of complement.
Positive integer complement code form is the same as the original, 8-bit two's complement is, for example, +7 00000111; and negative integers complement can be obtained by the following manner: The absolute value of this negative integer negate plus 1, together with the symbol bit 1 together represent it. -7 e.g. 8-bit twos complement: the absolute value of -7 7 plus 1 have negated 1111001, together with the sign bit 1 is 11,111,001.
You can also practice at +13 and 8-bit two's complement -13: + 13d = 00001101, -13d = 11110011.

 

  (byte) 0x97=-105

    In fact when numeric type conversion, only if there is a problem when faced with negative fundamental reason is that Java is not a negative number is encoded with an intuitive way, instead of using the " 2's complement " approach, this benefit is an addition and subtraction operations can be used simultaneously adding circuit is completed, but in the development Shique will encounter many strange questions, such as (byte) is the result of 128 -128, that is a large positive number, after the cut has turned out negative. 3.2 cited some of the transition rules, the application of these rules can be very accommodating to solve common problems of transition.

What this line of code output is?

public static void main(String[] args) {
  System.out.println(0xffffffff);
}

The same output following two lines of code do?

public static void main(String[] args) {
  byte b=-1;
  System.out.println((int)(char)b);
  System.out.println((int)(char)(b & 0xff));
}

Try running in Eclipse above two code snippets, if you are surprised by the output, read on ...

As you can see: the
operation result of the first code fragment: -1
operation result of the second code fragments are: 65535 and 255

The above two code snippets from "Java doubts" sixth small problem "multiple transformation", the original title reads as follows:

public class Multicast{
  public static void main (String[] args){
    System.out.println((int)(char)(byte)-1);
  }
}

The above code continuously conducted three types of conversion, the final result will return -1 do? The answer of course is no, the result of which the output is 65535. Here I compiled the relevant basic knowledge for everyone, I believe we should know after reading one of the reasons.

A, Java coding how negative?

    Using Java "2's complement" (Two's Complement) encoding negative, it is a method for encoding the value, to complete two steps: first, each have taken the opposite bit value becomes 0 becomes 1,1 to 0. For example, + 8 binary code is 00001000, 11110111 is the negated. The second step, will form the previous value plus 1.11110111 becomes 11111000. Therefore, the 2's complement of 00001000 is 11111000. That is, -8 is represented by 11111000 in the computer (8-bit machine) in. Details about the "2's complement", see Ruan Yifeng Bowen "About 2's complement", Bowen address portion attached herein by reference.

Second, what is the sign extension (Sign Extension)?

    Sign extension (Sign Extension) for extending the length of the binary bits in the numerical conversion type, in order to ensure the original symbol values ​​and the converted values ​​(positive or negative) and the same size, the narrower the type generally used (e.g., byte) the wider the type (e.g., int) conversion. Extended length refers to the bit, a number of padded bits to the left of the sign bit of the original value (0 for positive, 1 for negative).

    For example, if the decimal number 10 represented by 6 'bit, binary code is "001010", if it is sign-extended to 16bits length, the result is "0,000,000,000,001,010", i.e. to the left to make up 10 0 ( because 10 is positive, the symbol is 0), sign extended value before and after the magnitude and sign remains unchanged; 10bits if represented by a decimal number -15, using the "2's complement" code, binary code is "1111110001" , if it is sign-extended to 16bits, the result is "1,111,111,111,110,001", i.e., fill the left 6 1 (-15 is negative because, symbol 1), before and after the sign extended value of the magnitude and sign are not held change.

Three, Java type conversion rules

1. Java in integer literals   

 In Java int type literal writing in the following ways:

    - metric units, direct writing decimal numbers

    - octal way, starting with 0 format, for example, 012 decimal 10

    - hexadecimal numbers, starting with 0x format, for example, 255 decimal 0xff

 Note that, both the data type in Java int 012 and return 0xff, i.e., the length is 32 bits.

2. Java numeric type conversion rules

    This rule is "Java doubts" summary: If the original value types are signed, then sign extension; if a char type, regardless of what it is to be converted to type, are zero-extended. There is another need to remember one rule, if the target type is smaller than the length of the source type, the target type of taken directly length. For example, byte converted to an int type, int type, taken directly to the right of eight.

Fourth, to resolve "multiple transformation" problem

  Three consecutive casts of expression is as follows:

(int)(char)(byte)-1

1. int (32 bit) -> byte (8 bits)

  -1 literal int type, according to "2's complement" coding rule, the coded result of 0xFFFFFFFF, i.e., all 32-bit byte is set to 1. conversion type, direct interception of the last 8 bits, the byte 0xFF result, the corresponding decimal value is -1.

Byte 2. (. 8-bit)  -> char (16 bits)

  Since the byte is a signed type, so when converted into a char type (16-bit) sign extension is required, i.e. up continuously on the left 0xff 8 1 (1 is the sign bit of the 0xff), the result is 0xffff. Since the char is an unsigned type, it is represented by decimal number 65535 0xffff.

Char 3. (16 bits)  -> int (32 bits)

  Since the char is unsigned, when converted to type int is zero-extended, i.e., fill 16 consecutive 0 0xffff left 0x0000FFFF a result, the corresponding decimal number is 65535.

Fifth, a few examples of transition

  During type conversion, we must understand the meaning of the expression, can not rely on his senses. The best way is to express your intentions clear.

  When a char-value c is a width wider transition type, and sign extension is not desired to be encoded as follows:

int i = c & 0xffff;

  Has been mentioned above, is an int literal 0xffff, the prior & operator, the compiler will automatically transition to type int c, 0 is added before 16 c of the binary-coded, and then be & 0xffff operation, the intention is to force the front expression 16 is set to 0, after 16 remain unchanged. Although this operation is not necessary, but clearly expressed intention not to sign extension.

If desired sign extension may be coded as follows:

int i = (short)c; //Cast causes sign extension

  C first converted into a type short, and char which are of equal width, and the type is signed, then the conversion into an int type short, automatic sign-extended, i.e., if the short is negative, then the left to make up 16 a 1, or else make up 16 0.

  If the value b when a transition to a byte char, and undesirable sign extension, you must use a bit mask to restrict it:

char c = (char)(b & 0xff);

  (B & 0xff) is the result of 32-bit int type former 24 is forced to 0, after 8 remains unchanged, and when converted to char type, taken directly after 16. So that regardless of b is positive or negative, when converted to char, are equivalent to 0 is the complement of the eight on the left, i.e., zero-extended rather than sign extended.

  If desired sign extension is encoded as follows:

char c = (char)b; //Sign extension is performed

At this point in order to clarify the intent expressed need sign extension, the comment is necessary.

VI Summary

    In fact when numeric type conversion, only if there is a problem when faced with negative fundamental reason is that Java is not a negative number is encoded with an intuitive way, instead of using the "two's complement" way, so that the benefits additions and subtraction operations can be used simultaneously adding circuit is completed, but in the development Shique will encounter many strange questions, such as (byte) is the result of 128 -128, that is a large positive number, after the cut has turned out negative. 3.2 cited some of the transition rules, the application of these rules can be very accommodating to solve common problems of transition.

 

 

 

A, Java coding how negative?

Java采用”2的补码“(Two's Complement)编码负数,它是一种数值的编码方法,要分二步完成:第一步,每一个二进制位都取相反值,0变成1,1变成0。比如,+8的二进制编码是00001000,取反后就是11110111。第二步,将上一步得到的值加1。11110111就变成11111000。所以,00001000的2的补码就是11111000。也就是说,-8在计算机(8位机)中就是用11111000表示。关于“2的补码”的详细信息,请参考阮一峰的博文《关于2的补码》,博文地址附在本文的参考部分。

Second, what is the sign extension (Sign Extension)?

符号扩展(Sign Extension)用于在数值类型转换时扩展二进制位的长度,以保证转换后的数值和原数值的符号(正或负)和大小相同,一般用于较窄的类型(如byte)向较宽的类型(如int)转换。扩展二进制位长度指的是,在原数值的二进制位左边补齐若干个符号位(0表示正,1表示负)。

举例来说,如果用6个bit表示十进制数10,二进制码为"00 1010",如果将它进行符号扩展为16bits长度,结果是"0000 0000 0000 1010",即在左边补上10个0(因为10是正数,符号为0),符号扩展前后数值的大小和符号都保持不变;如果用10bits表示十进制数-15,使用“2的补码”编码后,二进制码为"11 1111 0001",如果将它进行符号扩展为16bits,结果是"1111 1111 1111 0001",即在左边补上6个1(因为-15是负数,符号为1),符号扩展前后数值的大小和符号都保持不变。

Three, Java type conversion rules

  1. Java, integer literals

    In Java int type literal writing in the following ways:

    • Decimal way, direct writing decimal numbers

    • Octal way, starting with 0 format, for example, 012 decimal 10

    • Hexadecimal numbers, starting with 0x format, for example, 255 decimal 0xff

    Note that, both the data type in Java int 012 and return 0xff, i.e., the length is 32 bits.

  2. Java numeric type conversion rules

    This rule is "Java doubts" summary: If the original value types are signed, then sign extension; if a char type, regardless of what it is to be converted to type, are zero-extended. There is another need to remember one rule, if the target type is smaller than the length of the source type, the target type of taken directly length. For example, byte converted to an int type, int type, taken directly to the right of eight.

Fourth, to resolve "multiple transformation" problem

Three consecutive casts of expression is as follows:

(int)(char)(byte)-1
  1. int (32 bit) -> byte (8 bits)

    -1 literal int type, according to "2's complement" coding rule, the coded result of 0xFFFFFFFF, i.e., all 32-bit byte is set to 1. conversion type, direct interception of the last 8 bits, the byte 0xFF result, the corresponding decimal value is -1.

  2. byte (8 bit) -> char (16 bits)

    Since the byte is a signed type, so when converted into a char type (16-bit) sign extension is required, i.e. up continuously on the left 0xff 8 1 (1 is the sign bit of the 0xff), the result is 0xffff. Since the char is an unsigned type, it is represented by decimal number 65535 0xffff.

  3. char (16 bit) -> int (32 bits)

    Since the char is unsigned, when converted to type int is zero-extended, i.e., fill 16 consecutive 0 0xffff left 0x0000FFFF a result, the corresponding decimal number is 65535.

Fifth, a few examples of transition

During type conversion, we must understand the meaning of the expression, can not rely on his senses. The best way is to express your intentions clear.

When a char-value c is a width wider transition type, and sign extension is not desired to be encoded as follows:

int i = c & 0xffff;

Has been mentioned above, is an int literal 0xffff, the prior & operator, the compiler will automatically transition to type int c, 0 is added before 16 c of the binary-coded, and then be & 0xffff operation, the intention is to force the front expression 16 is set to 0, after 16 remain unchanged. Although this operation is not necessary, but clearly expressed intention not to sign extension. 
If desired sign extension may be coded as follows:

int i = (short)c; //Cast causes sign extension

C first converted into a type short, and char which are of equal width, and the type is signed, then the conversion into an int type short, automatic sign-extended, i.e., if the short is negative, then the left to make up 16 a 1, or else make up 16 to 0. 
If when a byte value b transition to a char, and do not want to have sign extension, you must use a bit mask to limit it:

char c = (char)(b & 0xff);

(b & 0xff) is the result of 32-bit int type former 24 is forced to 0, after 8 remains unchanged, and when converted to char type, taken directly after 16. So that regardless of b is positive or negative, when converted to char, are equivalent to 0 is the complement of the eight on the left, i.e., zero-extended rather than sign extended. 
If desired sign extension is encoded as follows:

char c = (char)b; //Sign extension is performed

At this point in order to clarify the intent expressed need sign extension, the comment is necessary.

VI Summary

In fact when numeric type conversion, only if there is a problem when faced with negative fundamental reason is that Java is not a negative number is encoded with an intuitive way, instead of using the "two's complement" way, so that the benefits additions and subtraction operations can be used simultaneously adding circuit is completed, but in the development Shique will encounter many strange questions, such as (byte) is the result of 128 -128, that is a large positive number, after the cut has turned out negative. 3.2 cited some of the transition rules, the application of these rules can be very accommodating to solve common problems of transition.

Guess you like

Origin blog.csdn.net/qq_38998213/article/details/93216039