Java &0XFF usage Analysis and the original code, anti-code, and complement related knowledge

Source: Internet
Author: User

Charstrong go to intWhy use 0xFF? Note: in Javaexpressed in complementary form BinaryIf you do not want a symbol extension, you can take the action. For example char c;int i = C & 0xFFFF, where Char has 8 bits, the int type has 32 bits, and with 32/8=4 F (i.e. 0xFFFF) to do and operate, you can mask the symbol extension.
///negative integer, the extra FF is entered in front, without removing the front redundant FF, and output in double byte
System. out. println (Integer.tohexstring(-2). toUpperCase ());//fffffffe
Essentially 0xFF will be converted to 0x000000ff and then bit-op
System. out. println (Integer.tohexstring(-2&0xFF). toUpperCase ());//fe
System. out. println (Integer.tohexstring(-2&0x000000ff). toUpperCase ());FE

//binary representation
System. out. println (Integer.tobinarystring(-2). toUpperCase ());//11111111111111111111111111111110, corresponding to 2 complement forms after symbolic expansion of the form of expression
System. out. println (Integer.tobinarystring(-2&0xFF). toUpperCase ());//11111110,It's a 2-complement form.
Symbol extensions are used to extend the length of a bits when a numeric type is converted, to guarantee that the converted numeric value and the original value's symbol (positive or negative) and size are the same, typically for a narrower type, such as Byte, to convert to a wider type (such as int). Extended bits length refers to a number of sign bits (0 indicates positive, 1 for negative) on the left side of the bits of the original value.Java Numeric type conversion rule One <Java FAQ > Summary1. If the initial numeric type is signed, the symbol extension is executed, and if it is a char type, the 0 extension is performed regardless of what type it is being converted to. 2. If the length of the target type is less than the length of the source type, the length of the target type is directly intercepted. For example, the int type is converted to a byte type, and the right 8 bits of the int are intercepted directly.  Analyze the problem of "multiple transformations" for
    1. (int)(char)(byte)-1
STEP1:Int (32-bit), byte (8-bit) 1 is the literal of int, and its complement expression in Java is [111...1], that is, 32 bits are all 1. When converting to a byte type, the last 8-bit complement is intercepted directly [1111 1111], so the decimal number corresponding to byte is-1. Step2:byte (8-bit), char (16-bit) Because Byte is a signed type, a symbolic extension is required when converting to char (16-bit), resulting incomplement[1111 1111 1111 1111]. Since char is an unsigned type, its corresponding decimal number is 2^16-1=65535.  3. Char (16-bit), int (32-bit) Since char is an unsigned type, 0 is expanded when converted to int, and the result is a complement [0000 0000 0000 0000 1111 1111 1111 1111], and its corresponding decimal number is 65535. Basic concept positive (fixed-point decimal, fixed-point integer):     Original code, complement, anti-code the same; negative (fixed-point decimal, fixed-point integer): Anti-code: Keep the original code symbol bit unchanged, the value of the reverse Complement: The first method to the inverse code of the lowest bit +1 is OK, if the value of the highest bit has carry is discarded (do not carry the symbol bit) The second method is based on the original code, the sign bit is unchanged, the other starts from the lowest bit, until the first 1 is encountered, nothing changes, the bit is reversed by bitwise, I.E. (Recommended)                     The Original code: ([Sign bit][***][1][n*0]), and its corresponding complement only to the [* *] part of the reverse can be followed. For example, when the total number of encodings is 8: +127 of the original code, anti-code, complement is: 0 1111111 -127 of the original code, anti-code, the complement is: 1 1111111, 1 0000000, 1 0000001 Expand learn the complement of known negative numbers, how to get their decimal number? 1. First counter, including sign bit 2. Convert to decimal number 3. Add a minus sign and subtract 1 for example: For 11111010, the inverse 00000101, the corresponding decimal number is 5, the third step conversion can get the result:-6 Why N-bit binary data representation range is [ -2^ (n-1), 2^ ( n-1)-1]? In the case of 8-bit binary, -128= (-1) + (-127) = ([1000 0001]+[1111 1111]) = ([1111 1111]+[1000 0001]) complement = ([1000 0000]), so in the result of the complement operation,([1000 0000]) complement means-128, which is the extra data.



From for notes (Wiz)



Java &0XFF usage Analysis and the original code, anti-code, and complement related knowledge

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.