When processing the integer value, you can directly operate each bit of the integer value. This means that you can use the masking technique to get the bits in an integer
Including: 1.8 & (and), | (or), ^ (XOR), ~ (not / negate) (and: all are 1, or: one is 1, XOR: when the two are different, the result is 1 XOR, which can be understood as non carry addition: 1 + 1 = 0, 0 + 0 = 0, 1 + 0 = 1
Nature:
1. The exchange law can exchange the position of operation factors arbitrarily, and the result is invariable
2. Combination law (i.e. (a^b) Ac==a ^ (bAc))
3. For any number x, there are xQx=0, x0=x, seeking difference with oneself or 0, seeking difference with oneself or for oneself
4. Reflexivity AABAB=AA0=A, continue to do exclusive or operation with the same factor, and the final result is yourself)
2. > > and < < operators move binary bits right or left
3. >
For int type, 1 < < 35 and 1 < < 3 are the same (int type only has bit AND: both are 1 and the result is 1, or: one is 1 and the result is 1, XOR: the result is 1 when the two are not the same), while for long type left operand, the right operand should be 64
Usage example:
Judge odd even number
>Get binary bit is 1 or 0 (two solutions)
Exchange the values of two integer variables
>Do not use judgment statement to find the absolute value of integer
package algorithm.Bit operation; //Title: please implement a function, input an integer, and output the number of 1 in the binary representation of the number. //Example: the binary representation of 9 is 1001, with 2 bits being 1 //Ideas: 1. Move the number bit by bit to the left, and operate with 1, and count the number whose result is zero //2. Remove the lowest one at a time import java.util.Scanner; public class test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N =sc.nextInt(); System.out.println(Integer.toString(N,2)); //Difference between Integer and int https://blog.csdn.net/teacher'slee'zzsxt/article/details/79230501 int count=0; for(int i=0;i<32;i++){ if((N&(1<<i))==(1<<i)){ count++; } } System.out.println(count); //Method two: count = 0; while(N!=0){ N=((N-1)&N); count++; } System.out.println(count); } }
package algorithm.Bit operation; //Title: use a sentence to judge whether an integer is the integer power of two //Idea: convert this integer to binary. If this number is the integer power of two, then this number is only one on the left //The other positions are all zero. import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N =sc.nextInt(); System.out.println(Integer.toString(N,2));//Convert N to binary if(((N-1)&N)==0){ System.out.println("This integer is the integer power of two"); } else System.out.println("This integer is not an integer power of two"); } }
package algorithm.Bit operation; //Title: exchange the parity of binary numbers //Ideas: 1. Make and operate integers with 0101010101... And 1010101010... Respectively, and keep them separately //Odd and even digits. Then move the odd number to the left and the even number to the right to get the converted number. //2. The general solution can convert the input number into an array, and then exchange the odd and even digits in the array import java.util.Scanner; public class test3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); System.out.println("The original number is:"+N); int M=transform(N); System.out.println("The converted number is:"+M); } public static int transform(int i){ int even = i&0xaaaaaaaa;//And 10101010 //In order to facilitate the use of the hexadecimal eight a int odd = i&0x55555555;//And 0101010101 return(even>>1)^(odd<<1); } }