Skip to content

源码:labs/foundations/bitmap-bitset-demo/src/BitmapBasicsDemo.java ​

  • 原始路径:labs/foundations/bitmap-bitset-demo/src/BitmapBasicsDemo.java
  • 类型:java
java
import java.util.BitSet;

public class BitmapBasicsDemo {
    static class SimpleBitmap {
        private final long[] words;
        SimpleBitmap(int capacityBits) { words = new long[(capacityBits + 63) >> 6]; }
        void set(int bit)     { words[bit >> 6] |= 1L << (bit & 63); }
        boolean get(int bit)  { return (words[bit >> 6] & (1L << (bit & 63))) != 0; }
    }

    public static void main(String[] args) {
        SimpleBitmap bm = new SimpleBitmap(1000);
        bm.set(69);
        System.out.println("bit 69 -> word " + (69 >> 6) + ", offset " + (69 & 63));
        System.out.println("get(69)=" + bm.get(69) + ", get(70)=" + bm.get(70));

        long bits10e9 = 1_000_000_000L;
        System.out.println("10 亿数 ≈ " + (bits10e9 / 8 / 1_000_000) + " MB");

        BitSet a = new BitSet(); a.set(1); a.set(3); a.set(100);
        BitSet b = new BitSet(); b.set(3); b.set(50);
        System.out.println("a.get(100)=" + a.get(100) + ", a 中 1 的个数=" + a.cardinality());
        BitSet both = (BitSet) a.clone(); both.and(b);
        System.out.println("a ∩ b = " + both);
    }
}

站点构建时间:2026/8/24 23:43:17