/* * Java Bitwise operations: With (&), non (~), or (|), XOR (^), left Shift (<<), right Shift (>>) *+------------------------------------------------- ----------------------------------+ *| & | When the bits of both operands are 1 o'clock, the result is 1, otherwise 0 such as 1 1 0 0 & 1 0 1 0 = 1 0 0 0 | *+-----------------------------------------------------------------------------------+ *| | | When the bit on both sides of the operand has one side of 1 o'clock, the result is 1, otherwise 0 such as 1 1 0 0 | 1 0 1 0 = 1 1 1 0 | *+-----------------------------------------------------------------------------------+ *| ~ | Change the operand's bit 0 to 0. If 1 0 0 = 0 0 1 1 | *+-----------------------------------------------------------------------------------+ *| ^ | When the phase of both operands is the same, the result is 0 and no is 1. E.g. 1 1 0 0 ^ 1 0 1 0 = 0 1 1 0 | *+-----------------------------------------------------------------------------------+ *| << | Shift the operand to the left by the M bit, such as int 1 << 2, 0000 0000 0000 0000 0000 0000 0000 0001 | *| | << 2 = 0000 0000 0000 0000 0000 0000 0000 0100 = 4 | *+-----------------------------------------------------------------------------------+ *| >> | Shift the operand to the right by the M bit, such as int 4 >> 2, 0000 0000 0000 0000 0000 0000 0000 0100 | *| | >> 2 = 0000 0000 0000 0000 0000 0000 0000 0001 = 1 | *+-----------------------------------------------------------------------------------+ *| >>> | Unsigned right-shift operation does not consider the symbol bit, the rightmost data bit is missing, the leftmost data bit is filled with 0 | *| | 1111 1111 1111 1111 1111 1111 1111 0110 ( -10) >>> 1 = | *| | 0111 1111 1111 1111 1111 1111 1111 1011 (2147483643) | *+-----------------------------------------------------------------------------------+ *======================== ================================================================================== *<< Operation Rules: * *. In binary form, all numbers are shifted to the left by the corresponding number of digits, the position is shifted out (discarded), and the low vacancy is 0. When the left-hand operand is of type int, the 31st bit of each move is shifted out and discarded; when the left-hand operand is a long type, The 63rd bit of each move 1 bits is to be removed and discarded; when the left-hand operand is a byte and a short type, these types are automatically expanded to be of type int; * *<< mathematical meaning: The number does not overflow, for positive and negative numbers, left one is equal to multiply by 2 of the 1, the left n is equal to the number of times the N of 2 * *<< NOTE: *. If the number of bits moved exceeds the maximum number of digits of the type, the compiler will model the moved bits. If you move the int type 33-bit, you actually move only the 33%32=1 bit *2.N bit binary, the highest bit is the sign bit, so the value range represented -2^ (n-1)--2^ (n-1)-1, so the module is 2^ (n-1) *========================= ================================================================================== *>> Operation Rules: * *. According to the binary form of all the numbers to the right to move the corresponding number of digits, low-off (discard), high position of the vacant sign bit, that is, a positive complement 0, minus 1. When the operand to the right is the byte and short type, these types are automatically expanded to int * For example: If you want to shift the value to negative Each right shift is 1 on the left, if you want to shift the value is positive, each right shift is 0 on the left, this is called the symbol bit extension (reserved sign bit), in the right shift * *>> mathematical significance. Shift right one equivalent to 2, right shift n bits equals 2 n times * *>> NOTE: * * sign bit unchanged, left to fill the symbol bit *======================================================================================== =================== *>>>. The symbol bit extension is ignored, and the maximum number of bits is 0. The unsigned right shift operator >>> is only meaningful for 32-bit and 64-bit values *====================== ===================================================================================== * Negative number in the form of a positive complement = complement = The original code of the inverse code + 1 * Anti-code = ORIGINAL Code bitwise NEGATION * Original code = number of binary representation * * in cryptographic operations and graphics operations, the displacement operation will be used frequently. Use a displacement operation only if you need particularly fast performance, and the performance test proves that the math * operation is causing performance problems. *=========================================================================================================== * 1 << 2 = 0000 0000 0000 0000 0000 0000 0000 0001 << 2 = 0000 0000 0000 0000 0000 0000 0000 0100 [4] * 1 <<-2 = 0000 0000 0000 0000 0000 0000 0000 0001 << 2 = 0000 0000 0000 0000 0000 0000 0000 0100 [1073741824 ] = = [1 << + ( -2)] * 1 << = 0000 0000 0000 0000 0000 0000 0000 0001 << 31 = 1000 0000 0000 000 0 0000 0000 0000 0000 [ -2147483648] = = [ -2^31] *-1 << 2 = 1111 1111 1111 1111 1111 1111 1111 1111 << 2 = 1111 1111 1111 1111 1111 1111 1111 1100 [-4] *-2^31 << 2 = $0000 0000 0000 0000 0000 0000 0000 << 2 = 0000 0000 0000 0000 0000 0000 0000 0000 [0] *-2^31 >> 2 = $0000 0000 0000 0000 0000 0000 0000 << 2 = 1110 0000 0000 0000 0000 0000 0000 0000 [ -536870912] *-2^31 >>> 2 = 1000 0000 0000 0000 0000 0000 0000 000 0 << 2 = 0010 0000 00000000 0000 0000 0000 0000 [536870912] *============================================================================== =============================*/
Java Basics Java bit operation