What will be the result of running the following method with an input of 67?
public int MaskOff(int N){
return N^3;
}
a) The method will return 3.
b) The method will return 64.
c) The method will return 67.
d) The method will return 0.
答案是:B
这种题目是考查的是java中的位运算符中的异或运算符^脱字符表示,意思是两上二进制数比较,有一个为true才为true,否则为false
下面写一个程序来验证一下先
public class Eddy {
public static int MaskOff(int N){
return N^3;
}
public static void main (String args []) {
Eddy eddy=new Eddy();
System.out.println(eddy.MaskOff(67));
}
}
第一:把67化成二进制数
67/2=33 1
33/2=16 1
16/2=8 0
8/2=4 0
4/2=2 0
2/2=1 0
1000011=(decimal)67
然后把3化成二进制
3/2=1 1
11
求解:
1000011
0000011
=
1000000
然后把这个化成二进制形式
最后把1000000化成十进制数
2的6次=64
这个题目的答案就是:64