Java 定义了位运算符,应用于整数类型 (int),长整型 (long),短整型 (short),字符型 (char),和字节型 (byte) 等类型。位运算时先转换为二进制,再按位运算。 表格中的例子中,变量 a 的值
Java 定义了位运算符,应用于整数类型 (int),长整型 (long),短整型 (short),字符型 (char),和字节型 (byte) 等类型。位运算时先转换为二进制,再按位运算。
表格中的例子中,变量 a 的值为 60(二进制:00111100),变量 b 的值为 13(二进制:00001101):
public class BitOperation {public static void main(String args[]) {
int a = 60;
int b = 13;
System.out.println("a & b = " + (a & b));
System.out.println("a | b = " + (a | b));
System.out.println("a ^ b = " + (a ^ b));
System.out.println("~a = " + (~a));
System.out.println("a << 2 = " + (a << 2));
System.out.println("a >> 2 = " + (a >> 2));
System.out.println("a >>> 2 = " + (a >>> 2));
}
}a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2 = 15
a >>> 2 =