Appearance
源码:labs/foundations/bitwise-operators-demo/src/BitwiseOperatorsBasicsDemo.java
- 原始路径:
labs/foundations/bitwise-operators-demo/src/BitwiseOperatorsBasicsDemo.java - 类型:
java
java
/**
* 位运算速查 Demo(通用,不绑 HashMap)。
*
* 理论文档:docs/00-foundations/01-bitwise-operators.md
* HashMap 下标算法专题:labs/java/runtime-concurrency/map-concurrency-compare
*
* 运行:
* cd labs/foundations/bitwise-operators-demo
* javac -d out src/BitwiseOperatorsBasicsDemo.java
* java -cp out BitwiseOperatorsBasicsDemo
*/
public class BitwiseOperatorsBasicsDemo {
static final int READ = 1 << 0;
static final int WRITE = 1 << 1;
static final int EXEC = 1 << 2;
public static void main(String[] args) {
System.out.println("====== 位运算速查:符号与口诀 ======\n");
printCheatSheet();
System.out.println("\n====== 1. & 按位与 ——「掩码筛子,只留关心的位」======\n");
demoAndMask();
System.out.println("\n====== 2. | 按位或 ——「有 1 就 1,用来打开标志」======\n");
demoOrFlags();
System.out.println("\n====== 3. ^ 按位异或 ——「相同为 0,不同为 1」======\n");
demoXorMix();
System.out.println("\n====== 4. >>> 无符号右移 ——「右边扔掉,左边补 0」======\n");
demoUnsignedShift();
System.out.println("\n====== 5. >> 有符号右移 ——「负数左边补 1」======\n");
demoSignedShift();
System.out.println("\n====== 6. << 左移 ——「相当于乘 2 的幂」======\n");
demoLeftShift();
}
static void printCheatSheet() {
System.out.println("┌──────┬────────────────────┬──────────────────────────────────────┐");
System.out.println("│ 符号 │ 名称 │ 常见用途 │");
System.out.println("├──────┼────────────────────┼──────────────────────────────────────┤");
System.out.println("│ & │ 按位与 │ 掩码:取出或清零某些位 │");
System.out.println("│ | │ 按位或 │ 置位:打开 READ / WRITE 等标志 │");
System.out.println("│ ^ │ 按位异或 │ 翻转位、拌散数据 │");
System.out.println("│ ~ │ 按位非 │ 配合掩码做取反清零 │");
System.out.println("│ << │ 左移 │ 乘 2 的幂;构造 1<<n 标志位 │");
System.out.println("│ >> │ 有符号右移 │ 带符号整数除 2 │");
System.out.println("│ >>> │ 无符号右移 │ 高位无符号地参与低位计算 │");
System.out.println("└──────┴────────────────────┴──────────────────────────────────────┘");
System.out.println("详细图解见:docs/00-foundations/01-bitwise-operators.md");
}
static void demoAndMask() {
int value = 0b0000_0000_0000_0000_0000_0000_0000_1011; // 11
int mask = 0x0F; // 低 4 位
printBits("value", value);
printBits("mask ", mask);
printBits("value & mask", value & mask);
System.out.println("比喻:mask 是筛子,只保留低 4 位。");
}
static void demoOrFlags() {
int flags = 0;
flags |= READ;
flags |= WRITE;
System.out.printf("flags = READ | WRITE → %d%n", flags);
System.out.printf("含 READ? %s%n", (flags & READ) != 0);
System.out.printf("含 EXEC? %s%n", (flags & EXEC) != 0);
}
static void demoXorMix() {
int h = 0b0001_0010_0011_0100_0000_0000_1010_1011;
int shifted = h >>> 16;
int mixed = h ^ shifted;
printBits("h ", h);
printBits("h >>> 16 ", shifted);
printBits("h ^ (h>>>16)", mixed);
System.out.println("比喻:把高位信息拌进低位(集合 hash 拌位同理,见 map-concurrency-compare)。");
}
static void demoUnsignedShift() {
int h = 0x1234_ABCD;
printBits("h ", h);
printBits("h >>> 16", h >>> 16);
System.out.println(">>> 无符号:右边丢掉,左边补 0。");
}
static void demoSignedShift() {
int negative = -1;
printBits(" -1 ", negative);
printBits(" -1 >> 4 ", negative >> 4);
printBits(" -1 >>> 4", negative >>> 4);
System.out.println("带符号除法用 >>;需要无符号语义时用 >>>。");
}
static void demoLeftShift() {
System.out.println("1 << 3 = " + (1 << 3));
System.out.println("5 << 2 = " + (5 << 2) + " (5 × 4)");
}
static void printBits(String label, int value) {
String bits = String.format("%32s", Integer.toBinaryString(value)).replace(' ', '0');
System.out.printf(" %-14s = %11d 二进制: %s%n", label, value, bits);
}
}