public class test{
// 與運算子用符號“&”表示
// 只有兩個位都是1,結果才是1,可以知道結果就是1,即1
public static void main(String[] args) {
int a = 1;
int b = 129;
System.out.println(a + " toBinary :" + Integer.toBinaryString(a));
System.out.println(b + " toBinary :" + Integer.toBinaryString(b));
System.out.println(a + " & " + b + " = " + (a & b));
}
}
output :
1 toBinary :1
129 toBinary :10000001
1 & 129 = 1
public class test{
// 或運算子用符號“|”表示
// 兩個位只要有一個為1,那麼結果就是1,否則就為0,可以知道結果就是10000001,即129
public static void main(String[] args) {
int a = 1;
int b = 129;
System.out.println(a + " toBinary :" + Integer.toBinaryString(a));
System.out.println(b + " toBinary :" + Integer.toBinaryString(b));
System.out.println(a + " | " + b + " = " + (a | b));
}
}
output :
1 toBinary :1
129 toBinary :10000001
1 | 129 = 129
public class test{
// 或運算子用符號“~”表示
public static void main(String[] args) {
int b = 129;
System.out.println(b + " toBinary :" + Integer.toBinaryString(b));
System.out.println(~b);
}
}
output :
129 toBinary :10000001
-130
public class test{
// 或運算子用符號“^”表示
// 兩個運算元的位中,相同則結果為0,不同則結果為1,可以知道結果就是10000000,即128
public static void main(String[] args) {
int a = 1;
int b = 129;
System.out.println(a + " toBinary :" + Integer.toBinaryString(a));
System.out.println(b + " toBinary :" + Integer.toBinaryString(b));
System.out.println(a + " ^ " + b + " = " + (a ^ b));
}
}
output :
1 toBinary :1
129 toBinary :10000001
1 ^ 129 = 128