Java Complement practice

Source: Internet
Author: User

Numbers in Java appear in the form of a complement

The conversion of byte in Java to a number is also in the form of a complement

=================================================

The complement of the negative number = the symbol bit of the original code is unchanged the remaining bits are reversed and added a

=================================================

Special numbers.
The complement of two numbers equals itself: one is 0, the other is the maximum negative number in the binary form that can be represented by a signed bit in the bit (that is, 1000 ...). )。

The 0 Complement calculation method (take 8 bits for example) is as follows: First calculate its inverse code:

1111 1111
And then add one to the inverse code:

0000 0000, Overflow bit binary value = 1 (binary)
The overflow is ignored, and the result is 0 (0 is the only number that will overflow during the complement calculation. )。 So the complement of 0 is 0. and 0 x (-1) = 0, so its complement still satisfies the principle of "the complement of number A is-a".

If the calculation is 1000 0000 (this is the maximum negative number 128 of the binary form that can represent a signed bit-differentiated in 8 bits): First calculate its inverse code:

0111 1111
Plus one is its complement.

1000 0000
The complement of 1000 0000 (-128) is still 1000 0000 (-128). But ( -128) x (-1) = 128, so its complement is an exception to the above rule.

Summary: Since 0 can be equal to 0 of the two complement-0, and also because the 8-bit two complement can display a range of values of-128 ~ 127, but 128 of the two complement 128 cannot be used in the number of bits that already have a number of 8 of the digits of the available two complement representation. "The maximum negative number in binary form that can be represented by a symbolic bit in the calculation of other digits (that is, 1000 ... 000), there will be a similar situation. 】

So: 0 and 128 are really two special numbers in the "complement of number a" principle.

=================================================

//-128 original code: 1000 0000 0000 0000 0000 0000 0000
//-128 anti-code: 1111 1111 1111 1111 1111 1111 0111 1111
//-128 complement: 1111 1111 1111 1111 1111 1111" 0000
system.out.println (integer.tohexstring ( -128));
//ffffff80

//(-128 & 0xFF) Complement:
  1111 1111 1111 1111 1111 1111" 0000
& 0000 0000 0000 0000 0000 0000 1111 1111
= 0000 0000 0000 0000 0000 0000" 0000
system.out.println (integer.tohexstring ( -128 & 0xFF));
//00000080

//byte byte_128 = (byte) -128 (---128 complement reserved last 8--0000)
//byte_128 complement: 0000
//byte_128:1111 1111 (symbol bit invariant remaining bit reversed)
//byte_128 original code: 1000 0000 (complement to original Code = complement of complement)
system.out.println ( byte_128 );
//-128

//byte byte128 = (byte) (----128 complement reserved last 8--0000)
//byte128 complement: 0000
//byte128:1111 1111 (symbol bit invariant remaining bit reversed)
//byte128 original code: 1000 0000 (complement to original Code = complement of complement)
system.out.println ( byte128 );
//-128

byte byte233 = (byte) 233 (--233 complement reserved last 8--1110 1001)
byte233 Complement: 1110 1001
byte233 Anti-code: 1001 0110 (the symbol bit is not changed the remaining bit reversed)
byte233 Original code: 1001 0111 (complement to the original code = complement of complement)
System.out.println (byte233);
-23

Java complement practice

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.