package test2;
public class BitOper {
public static void main(String[] args) {
//判断一个int类型第几位是0,还是1
int i = 18; //10010
int oper = 1; //00100 与操作
System.out.println(isZero(i,oper));
//给1个int数第几位赋值为1
i = 18; //10010
oper = 2; //00100 或操作
System.out.println(setOneByOper(i,oper));
//给1个int数第几位赋值为0
i = 18; //10010
oper = 1; //11101与操作
System.out.println(setZeroByOper(i,oper));
}
public static boolean isZero(int i,int oper){
int result = i & (1<<oper);
return result==0;
//if(result==0){
// return true;
//}else{
// return false;
//}
}
public static int setOneByOper(int i,int oper){
return i | (1 << oper);
}
public static int setZeroByOper(int i,int oper){
return i & ~(1 << oper);
}
}
位操作的练习
最新推荐文章于 2024-02-19 22:32:00 发布