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