First, the basic knowledge
The encoding method of the numerical value in the computer, the original code, the inverse code, the complement.
The complement of positive number is the same as the original code, the complement of negative number is: The original code sign of negative number is unchanged, the other bits are reversed, plus 1.
In a computer, values are stored in the form of a complement. The benefits of complement:
The ① is designed with the complement of complementary memory, because it can transform subtraction into addition, simplifying operation rules, and unifying addition and subtraction.
② can also resolve the two representations of 0 without regard to the sign bit: for example, 0 of the original code has +0 and 0
+0=[0000 0000 0000 0000 0000 0000 0000 0000] Original
-0=[1000 0000 0000 0000 0000 0000 0000 0000] Original
In the case of a complement, [0000 0000 0000 0000 0000 0000 0000 0000] is used to denote 0, while [1000 0000 0000 0000 0000 0000 0000 0000] is used to denote -2^32
That's why we see the numeric range of type int in Java as [ -2^32, 2^32-1]. A negative number that can be represented is one more than an indication of positive numbers. This extra negative number is expressed in the original code----128
Second, the maximum and minimum value representation of int in Java
In Java, integers of type int are represented by 32 bits. The highest bit is the sign bit. The following are examples of 32bit values.
Therefore, the largest int integer in Java is: (1<<31)-1 and the value is: 2^32-1. It is stored in memory in the form of complement: [0111 1111 1111 1111 1111 1111 1111 1111] Complement
The smallest int integer in Java is available in 1 shifts: (1<<31) with a value of: -2^32. It is in the form of complementary form in memory: [1000 0000 0000 0000 0000 0000 0000 0000] Complement
In addition, the Java.util.BitSet class can perform some bit-related operations.
Three, a negative number of the modulo operation (for residual%)
When x < 0 o'clock, the modulo operation of negative numbers is as follows:
X mod y = x-(<x/y>)
X% y = x minus (y multiplied by the quotient of X and Y) .<x/y> represents the lower bound
Four, JAVA bit operation application
This demonstrates a simple application of XOR or operation
① any int integer with 0 xor or get the integer itself
② any int integer that is different from itself or gets 0
Example: 3^4^5^4^5 = 3
because: 3^4^5^4^5 = 4^4^5^5^3=3 (4^4=0,5^5=0,0^3=3)
According to the above two properties, the problem can be solved:
Given an array, each element, except an element, appears two times to find the element that appears once. Time complexity O (n), Spatial complexity O (1). (link)
Similarly, it is possible to solve this problem similarly:
To an array of length n-1, the number ranges from 1 to n (No duplicates), where there is a missing number to find the number. The time complexity is O (n) and the space complexity is O (1). (link)
The idea is that 0 is different from all the elements in the array, and then,...... N xor. In this way, the missing number appears only once in the XOR operation. The end result of the XOR is the missing number.
such as: 3^4^5^4^5 = 4^4^5^5^3 =3 (4^4=0,5^5=0,0^3=3)
The code is as follows:
1 Public classSolution {2 Public Static intSinglenumber (int[] nums) {3 intAns = 0;4 inti = 0;5 for(; i < nums.length; i++)6Ans = ans ^ nums[i] ^ (i+1);7 return(ans^ (i+1));8 }9 Ten Public Static voidMain (string[] args) { One int[] Nums = {3,4,1,5,7,6}; A intR =Singlenumber (nums); -System.out.println (R);//2 - } the}
Five, references
About the original code, complement, anti-code, seek redundancy reference: http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
About Java bit Operation application reference: http://my.oschina.net/xianggao/blog/412967
JAVA bit Operation Learning