<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java實現位圖 注意點: 1. 可以用`int[]`和`long[]`實現。 2. Java中`int`是4字節32位,`long`是8字節64位,位大小為`bitSize`。 3. 當前`index`,則在數組下標 = `index / bitSize` 的 `index % bitSize`位。 ## 代碼 ~~~ package com.mango.data.structure.bitmap; /** * 位圖 * @Author: mango * @Date: 2022/5/12 10:36 下午 */ public class Bitmap { // 用int數組實現,一個int 4個字節32位 private int[] data; private int size; private final int bitSize = 32; public Bitmap(int size){ this.size = size; int dataSize = size % bitSize == 0 ? size / bitSize : (size / bitSize) + 1; this.data = new int[dataSize]; } private void check(int index){ if(index >= this.size){ throw new RuntimeException("index " + index + " out of range size " + this.size); } } /** * 設置index的位為1 * @param index */ public void bitOne(int index){ this.check(index); this.data[index/bitSize] = this.data[index/bitSize] | (1 << (index % bitSize)); } /** * 設置index的位為0 * @param index */ public void bitZero(int index){ this.check(index); this.data[index/bitSize] = this.data[index/bitSize] & (~(1 << (index % bitSize))); } /** * 獲取inde位置的值,是0或者1 * @param index */ public int getBit(int index){ this.check(index); return (this.data[index/bitSize] >> (index % bitSize)) & 1; } } ~~~ ## 測試 ~~~ public class Test { public static void main(String[] args) { Bitmap bitmap = new Bitmap(101); System.out.println(bitmap.getBit(2)); bitmap.bitOne(2); System.out.println(bitmap.getBit(2)); bitmap.bitZero(2); System.out.println(bitmap.getBit(2)); bitmap.bitOne(100); System.out.println(bitmap.getBit(100)); } } ~~~ 輸出: ``` 0 1 0 1 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看