Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

41
LINES

< > BotCompany Repo | #1033635 // BitBuffer

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (10076L) is out of date.

// little-endian
persistable sclass BitBuffer is IntSize {
  new ByteBuffer byteBuffer; // TODO (optimization): use a growing circular buffer
  int currentByte, currentBit;
  
  void add(int b) {
    add(odd(b));
  }
  
  void add(bool b) {
    if (b) currentByte |= 1 << currentBit;
    if (currentBit == 7) {
      byteBuffer.add((byte) currentByte);
      currentByte = 0;
      currentBit = 0;
    } else
      ++currentBit;
  }
  
  bool get(int iBit) {
    int iByte = iBit >> 3;
    int theByte = iByte == byteBuffer.size()
      ? currentByte 
      : byteBuffer.get(iByte);
    ret (theByte & (1 << (iBit & 7))) != 0;
  }
  
  bool hasFullByte() {
    ret !byteBuffer.isEmpty();
  }
  
  byte popFullByte() {
    ret byteBuffer.popFirst();
  }
  
  public int size() {
    ret byteBuffer.size()*8 + currentBit;
  }
  
  // TODO: optimize void writeBits(int data, int nBits)
}

Author comment

Began life as a copy of #1033633

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033635
Snippet name: BitBuffer
Eternal ID of this version: #1033635/11
Text MD5: 3191ebed21c01e1607855c5e3616506e
Author: stefan
Category: javax / io
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-30 18:31:39
Source code size: 924 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 178 / 361
Version history: 10 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)