这些运算符用于执行数字的各个位的操作。它们可以与任何整数类型一起使用。它们用于执行二进制索引树的更新和查询操作。
// Java program to illustrate // bitwise operators public class operators { public static void main(String[] args) { int a = 0x0005; int b = 0x0007; // bitwise and // 0101 & 0111=0101 System.out.println("a&b = " + (a & b)); // bitwise and // 0101 | 0111=0111 System.out.println("a|b = " + (a | b)); // bitwise xor // 0101 ^ 0111=0010 System.out.println("a^b = " + (a ^ b)); // bitwise and // ~0101=1010 System.out.println("~a = " + ~a); // can also be combined with // assignment operator to provide shorthand // assignment // a=a&b a &= b; System.out.println("a= " + a); } }
输出:
a&b = 5 a | b = 7 a ^ b = 2 〜a = -6 a = 5