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

45
LINES

< > BotCompany Repo | #1035687 // VariableSizeUIntForBitHead

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

Libraryless. Click here for Pure Java version (13227L/76K).

// unsigned values only
sclass VariableSizeUIntForBitHead is BitIO {
  // How many bits are written at once
  settable int chunkSize = 3;
  
  settable int value;
  
  gettable int bitCount;
  
  public void readWrite(BitHead head) {
    if (head.writeMode()) {
      bitCount = max(1, numberOfBits(value));
      int value = this.value;
      
      head.writePartialByte(value, chunkSize);
      value >>>= chunkSize;
      
      while (value != 0) {
        head.writeBit(1);
        head.writePartialByte(value, chunkSize);
        value >>>= chunkSize;
      }
      head.writeBit(0);
    }
    
    if (head.readMode()) {
      int value = head.readPartialByte(chunkSize);
      int shift = 0;
      bitCount = chunkSize;
      while (!head.isEOF() && head.readBit()) {
        value |= head.readPartialByte(chunkSize) << bitCount;
        bitCount += chunkSize;
      }
      value(value);
    }
  }
  
  void readWrite(BitHead head, IF0<Int> getter, IVF1<Int> setter) {
    if (head.writeMode())
      value(getter!);
    readWrite(head);
    if (head.readMode())
      setter.get(value());
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035687
Snippet name: VariableSizeUIntForBitHead
Eternal ID of this version: #1035687/5
Text MD5: d574d5aeb646c81b211516e7bfd0a307
Transpilation MD5: d63c2124ecf97c73d24f0941cb6100e5
Author: stefan
Category: javax / io
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-07-06 21:51:26
Source code size: 1149 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 79 / 146
Version history: 4 change(s)
Referenced in: [show references]