GetBit


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

Parameters

BitBuffer
C++
Pointer to variable that receives data.
BASIC
Variant that receives data.
Variant type must be compatible with Byte, Integer or Long data type; it must not be an array.
Variant can be uninitialized; in such case it is converted to Byte.
NumberOfBits
Number of bits to read. 1 to 8 bits can be read in one operation.
If this parameter is 0 no data is read and method only returns number of bits available in bit buffer.
FilePosition
Position in file to read data at.
If this parameter is -1 data is read at current file position.

Return Values

If the method succeeds, the return value is the number of bits actually read.
The return value is 0 if end of file was encountered.
If the method fails, the return value is failure code and error code is set. Call GetError to get error code.

Remarks

Method returns the number of bits actually read, which may be less than expected if end of file is encountered prematurely. If NumberOfBits is 0 no data is read and method returns number of bits available in bit buffer.

Bits are buffered in special bit buffer which is one byte long and contains at most 8 bits to be accessed one by one. Bits are fetched from right to left - starting from least significant bit. If user requests more bits than are available in buffer, it is refilled with one complete byte read from file and file positon is moved forward by one byte. Such behaviour guarantees that other non-bit data read 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

PutBit, GetByte, GetShort, GetLong, GetFloat, GetDouble, GetText, GetUnicodeText, GetVariant, GetBinary, Reading and Writing Data