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.

1  
// little-endian
2  
persistable sclass BitBuffer is IntSize {
3  
  new ByteBuffer byteBuffer; // TODO (optimization): use a growing circular buffer
4  
  int currentByte, currentBit;
5  
  
6  
  void add(int b) {
7  
    add(odd(b));
8  
  }
9  
  
10  
  void add(bool b) {
11  
    if (b) currentByte |= 1 << currentBit;
12  
    if (currentBit == 7) {
13  
      byteBuffer.add((byte) currentByte);
14  
      currentByte = 0;
15  
      currentBit = 0;
16  
    } else
17  
      ++currentBit;
18  
  }
19  
  
20  
  bool get(int iBit) {
21  
    int iByte = iBit >> 3;
22  
    int theByte = iByte == byteBuffer.size()
23  
      ? currentByte 
24  
      : byteBuffer.get(iByte);
25  
    ret (theByte & (1 << (iBit & 7))) != 0;
26  
  }
27  
  
28  
  bool hasFullByte() {
29  
    ret !byteBuffer.isEmpty();
30  
  }
31  
  
32  
  byte popFullByte() {
33  
    ret byteBuffer.popFirst();
34  
  }
35  
  
36  
  public int size() {
37  
    ret byteBuffer.size()*8 + currentBit;
38  
  }
39  
  
40  
  // TODO: optimize void writeBits(int data, int nBits)
41  
}

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: 179 / 363
Version history: 10 change(s)
Referenced in: [show references]