Java Bitwise operators

Source: Internet
Author: User

Complement:

Complement is also called the second complement, that is, the number as a bit inverse operation (that is, a complement or anti-code ), and then add 1, a negative number can be represented by the complement of its opposite numbers. The advantage of the complement is that subtraction between two numbers can also be expressed as an addition between one number and the other, so that the addition and subtraction of the various signed numbers can be accomplished by simply using the add circuit and the two complement circuit.

E.g: there are 0011 (3) and 1111 (-1), 0011+1111=10010 (2)

In n-bit binary numbers, the maximum integer is 2n-1-1, the minimum negative number is -2n-1, and the nth bit is the sign bit.

To calculate the complement:

If 5 is represented by a 8-bit binary number as 0000 0101, then its one complement is the inverse code is 1111 1010, the second complement is a complement plus 1, so the complement of 5 is 1111 1011 (-5)

Note: In the example shown in e.g, the 1111 in 0011+1111 is only 1 of the complement form, not the binary representation of 15.

Left shift Operator: Universal format value <<num num is the number of digits left shifted

Algorithm: Discard the highest bit, 0 minimum bit of input

For the shift operator, the right argument requires a modulo 32 operation, provided that value is of type int.

On the premise that the number does not overflow, the left shift is equivalent to multiplying by 2, and the left shift n is equal to the N-time multiplied by 2.

When the left-hand operand is short, or byte, the left shift is automatically converted to type int

Right shift operator: Value>>num

Algorithm: Low-level, high-fill sign bit

Shift right one is equivalent to dividing by 2, right shift n is equivalent to dividing by 2 n times

Unsigned Right Shift Value>>>num

Ignore symbol bit expansion, 0 max Bit

Unsigned right shift only to 32-bit, 64-bit value has meaning

1  Public Static voidMain (String args[]) {2 3         intI=1;4 System.out.println (integer.tobinarystring (i));5System.out.println (i<<3);6System.out.println (Integer.tobinarystring (i<<3));7System.out.println (i<<35);8System.out.println (Integer.tobinarystring (i<<35));9         Ten          One         intT;  A         intnum = 0xFFFFFFE;  -System.out.println ("num=" +num+ "binary represented as" +integer.tobinarystring (num)); -             for(t=0; t<4;t++) {   thenum = num << 1;  -System.out.println ("num=" +num+ "binary represented as" +integer.tobinarystring (num));  -            }   -System.out.println ("32" + "binary is represented as" +integer.tobinarystring (-32));  +  -}

Java Bitwise operators

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.