Re: I need the speed
- Posted by Robert Craig <robert_craig at COMPUSERVE.COM> Mar 18, 1997
- 1110 views
Lucius L. Hilley III writes: > Please clarify how the remaining bits in bitbuffer will not be lost > and how the bits to be added are being stored into bitbuffer. I hope the following will be a bit clearer and more accurate. Warning: I've checked the syntax, but I haven't actually run or tested the following code. ================================================================= constant SHIFT8 = power(2, 8) atom bitbuffer -- always >= 0, can hold about 30 bits bitbuffer = 0 integer bitbufferlen -- number of bits currently in the buffer bitbufferlen = 0 integer fn fn = open("datafile", "wb") -- remember to open for binary write procedure add_bits(integer value, integer len) -- Add a value to the buffer. Value is len bits long and >= 0. -- len is 0 to 7. -- store len in buffer: len = len * power(2, bitbufferlen) -- shift left by bitbufferlen bits bitbuffer = or_bits(bitbuffer, len) -- OR it into buffer bitbufferlen = bitbufferlen + 3 -- store value in buffer: value = value * power(2, bitbufferlen) -- shift left bitbuffer = or_bits(bitbuffer, value) bitbufferlen = bitbufferlen + len end procedure procedure write_bits() -- Write a byte from the buffer to the file. -- The low order 8 bits are removed. puts(fn, bitbuffer) -- only low-order 8 bits are written bitbuffer = floor(bitbuffer / SHIFT8) bitbufferlen = bitbufferlen - 8 end procedure ======================================= Regards, Rob Craig Rapid Deployment Software