1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#define BITS_PER_BYTE 8
unsigned char CharMask[] = { 0x01u, 0x02u, 0x04u, 0x08u,
0x10u, 0x20u, 0x40u, 0x80u };
unsigned long LongMask[] = { 0x00000001ul, 0x00000002ul, 0x00000004ul, 0x00000008ul,
0x00000010ul, 0x00000020ul, 0x00000040ul, 0x00000080ul,
0x00000100ul, 0x00000200ul, 0x00000400ul, 0x00000800ul,
0x00001000ul, 0x00002000ul, 0x00004000ul, 0x00008000ul,
0x00010000ul, 0x00020000ul, 0x00040000ul, 0x00080000ul,
0x00100000ul, 0x00200000ul, 0x00400000ul, 0x00800000ul,
0x01000000ul, 0x02000000ul, 0x04000000ul, 0x08000000ul,
0x10000000ul, 0x20000000ul, 0x40000000ul, 0x80000000ul };
unsigned long GetBits(unsigned char *bitString, unsigned long lOffset, unsigned long lLength)
{
// Note that the lOffset begins at zero.
unsigned long lByteOffset;
unsigned long lBitOffset;
unsigned long lValue = 0ul;
long i;
// Determine the byte at which to start getting bits.
lByteOffset = lOffset / BITS_PER_BYTE;
// Determine the bit position of the byte at which to start getting bits.
lBitOffset = lOffset - lByteOffset * BITS_PER_BYTE;
// Process bit-by-bit.
for (i = lLength-1; i >= 0; i--)
{
// Get the bit from the source bit string, and then set it in
// the destination return value.
if (bitString[lByteOffset] & CharMask[lBitOffset])
lValue |= LongMask[i];
// Increment the current bit position within the bit string.
lBitOffset++;
if (lBitOffset > 7)
{
lBitOffset = 0;
lByteOffset++;
}
}
// Return the extracted value.
return (lValue);
} |
Partager