java Bitwise Operators Bitwise Operators and

A bitwise operator.

1. Bitwise AND operator (&)

  If the two binary numbers , the same number of digits are 1, the result bit is 1, and 0 otherwise .

  例 5&4

        Binary 5 is 0,000,000,000,000,101

        Binary 4 is 0,000,000,000,000,100

        The result is 0,000,000,000,000,100 to decimal is 4.

2. bitwise OR operator (|)

  If the two binary numbers, the same number of bits is a 1, then the result is 1 bit, and 0 otherwise.

  Example 5 | 4

        Binary 5 is 0,000,000,000,000,101

        Binary 4 is 0,000,000,000,000,100

        The result is 0,000,000,000,000,101 to decimal is 5.

 

3. The XOR operator (^)

  If the two binary numbers, the same number of bits is only a 1, then the result is 1 bit, and 0 otherwise.

  Example 5 ^ 4

        Binary 5 is 0,000,000,000,000,101

        Binary 4 is 0,000,000,000,000,100

        The result is 0,000,000,000,000,001 to decimal 1.

 

 4. Non-operator (~)

    Bitwise "not" operator, also known as inversion operator. If the input 0, the output 1, if the input 1, output 0.

    Examples 1-5

        Binary 5 is 0,000,000,000,000,101

        1-5 is the 1,111,111,111,111,010 -6 to decimal.

 

Second, the displacement of the operator.


1. signed displacement operator (<< >>)

    Shift operators only be used to treat type integer bits left shift operator (<<) can be specified according to the right operator to operator moves to the left the left operand (at low complement 0), "Signed "Bit right shift operator (>>) is specified according to the operator the right operand to the right to the left of the operator. "Signed" using right-shift operators "sign extension"; if the sign bit is positive, 0 is inserted at a high level; if a negative sign bit. 1 is inserted in the high. java added a "unsigned" right shift operator (>>>), he uses the "zero-extended"; regardless of the sign, are inserted in the high 0

    << Example 5 2 equal to 20
        binary 5 is 0,000,000,000,000,101
        left two 0,000,000,000,010,100

    Example 5 >> 2 equal to a
        binary 5 is 0,000,000,000,000,101
        right two 0,000,000,000,000,001

    Example >> 2 equal to -2 -5
        binary -5 1,111,111,111,111,011 is
        shifted two bits 1,111,111,111,111,110 

2. unsigned displacement operator (>>>)

   An increase in Java "unsigned" right shift operator (>>>), which uses "zero extend": regardless of the sign, are inserted in the high 0.

   Example 5 is equal to 2 >>> 1
        binary is 5 0,000,000,000,000,101
        right two 0,000,000,000,000,001

   Example 2 >>> equal 1073741822 -5
         binary -5 1,111,111,111,111,011 is
        shifted two bits 0,011,111,111,111,110 

3. The displacement of the operator can be used (or << >> = >>> = or =) equal sign in combination with

  When used in combination, the median value of the operation will move to the left of symbol specified by the value of the right side, the results obtained and then assigned to the variable on the left. But during "unsigned" right shift combined with assignment, you may encounter a problem, if such a shift operation for byte or short value, may not get the correct results. They will first be converted to an int and then carry out a right shift, and then cut off, assigned to the original type, in this case the results obtained may be -1.

  EXAMPLE short s = -1; s >>> = 10
       convert binary int is 1,111,111,111,111,111 1,111,111,111,111,111
       right 10 0,000,000,000,111,111 1,111,111,111,111,111 (right of the decimal result 4194303)

       Truncated 16-bit assignment short 1111 1111 1111 1111 (decimal result actual -1)

 

Third, the supplement.

1. Negative binary

 

        World Computer's only 0 and 1, then the negative number represents how it?

 

        Binary is the sign from a high level perspective, the most significant bit if one is negative, 0 if it is positive.

 

        If the negative is simply the highest bit becomes 1 if the value is not what we want will appear in the operation, it introduced: the original code, anti-code and complement. Positive original code, the inverted, complementary codes are the same, is negative in addition to the inverted sign bit (MSB) of the original code negated, the anti-complement code is +1

 

        Negative binary conversion, computer calculation is the complement with

 

       1, first removed the original code of the binary number,

 

       2, and then obtains the inverted

 

       3, finally obtaining complement

        Example -5

 

        -5 original code is 1,000,000,000,000,101

 

        Inverted is determined 1,111,111,111,111,010

 

        Complement is obtained 1,111,111,111,111,011

2. & and &&, | and ||, ~ and!

     & && and logic and can be expressed, && is the difference between a short circuit, as long as the foregoing conditions are not satisfied, the condition is no longer behind the judgment, and the &-circuit. Further & integer bit can be used as operators.

  | || and can be expressed, "or" || difference is that as long as the first condition is met, the latter condition is no longer judge, but | to judge all conditions. In addition | integer bits can be used as operator.

    ~ Is a unary operator and not equal sign (=) in combination (& = | = ^ = legal), and! Represents logical negation, and equals (==) represents the relationship operator used in combination is not equal (! =).

     

 

Guess you like

Origin www.cnblogs.com/zzy20170523/p/11007064.html