PutBit


The PutBit method writes specified number of bits to file and returns number of bits actually written. File position is moved forward by one byte every eight bits written.
C++
long PutBit(
  BYTE BitBuffer,                  // buffer that contains data
  long NumberOfBits,               // number of bits to write
  long FilePosition = -1           // position of data in file
);
BASIC
PutBit(
  BitBuffer As Variant,            // variant that contains data
  NumberOfBits As Long,            // number of bits to write
  FilePosition As Long = -1        // position of data in file
) As Long

Parameters

BitBuffer
C++
Variable that contains data.
BASIC
Variant that contains data.
Variant type must be compatible with Byte, Integer or Long data type; it must not be an array.
NumberOfBits
Number of bits to write. 1 to 8 bits can be written in one operation.
If this parameter is 0 bit buffer contents is written to file.
FilePosition
Position in file to write data at.
If this parameter is -1 data is written at current file position.

Return Values

If the method succeeds, the return value is the number of bits actually written.
If the method fails, the return value is failure code and error code is set. Call GetError to get error code.

Remarks

If NumberOfBits is 0 bit buffer is written to file, regardless of the number of bits present in bit buffer; if bit buffer contains less than 8 bits it is left padded with 0 bits to fill entire byte.

Bits are buffered in special bit buffer which is one byte long and contains at most 8 bits to be written one by one. Bits are put into bit buffer from right to left - starting from least significant bit. If entire buffer is filled it is written to file as one complete byte and file positon is moved forward by one byte. Such behaviour guarantees that other non-bit data written by user is always accessed on byte boundary regardless of number of bits present in bit buffer. Read and written bits are never mixed in bit buffer; if GetBit call is followed by PutBit, bit buffer is discarded before bits are written; if PutBit call is followed by GetBit, bit buffer is written to file before bits are read. Bit buffer is flushed whenever other non-bit data is read, file is repositioned, resized, closed or detached.

See Also

GetBit, PutByte, PutShort, PutLong, PutFloat, PutDouble, PutText, PutUnicodeText, PutVariant, PutBinary, Reading and Writing Data