一.符号认知:
&——按位与
|——按位或
^——按位异或
二.基本方法:
x&(x-1) 消去最后一位1
x&(x-1)==0 判断是否为2的幂次数(2的幂次数只有一个1,去掉就变成0)
while(x!=0) {x = x&(x-1);b++;} 判断有多少个1
例题代码1:
import java.util.Scanner;
//常用方法
public class WeiYunSuan {
public static void F0(int x) {
//判断奇偶
if((x&1)==1)
System.out.println(x+"是奇数");
else System.out.println(x+"是偶数");
}
public static int F(int x) {
//消去最后一位1
int k = x&(x-1);
return k;
}
public static int F1(int x) {
//判断有多少个1
int b = 0;
while(x!=0) {
x = x&(x-1);
b++;
}
return b;
}
public static void F2(int x) {
//判断是否是2的幂次数(含有一个1)
int z=x;
int b = 0;