11 /* Public: Reads a subset of bits from a byte array.
13 * data - the bytes in question.
14 * startPos - the starting index of the bit field (beginning from 0).
15 * numBits - the width of the bit field to extract.
16 * bigEndian - if the data passed in is little endian, set this to false and it
17 * will be flipped before grabbing the bit field.
19 * Bit fields are positioned according to big-endian bit layout, but inside the
20 * bit field, values are represented as little-endian. Therefore, to get the bit
21 * field, we swap the overall byte order if bigEndian == false and
22 * use the value we find in the field (assuming the embedded platform is little
25 * For example, the bit layout of the value "42" (i.e. 00101010 set at position
26 * 14 with length 6 is:
28 * 000000000000001010100000000000000000000000000000000000000000000
30 * and the same value and position but with length 8 is:
32 * 000000000000000010101000000000000000000000000000000000000000000
34 * If the architecture where is code is running is little-endian, the input data
35 * will be swapped before grabbing the bit field.
39 * uint64_t value = getBitField(data, 2, 4);
41 * Returns the value of the requested bit field.
43 uint64_t getBitField(uint64_t data, int startPos, int numBits, bool bigEndian);
45 /* Public: Set the bit field in the given data array to the new value.
47 * data - a byte array with size at least startPos + numBits.
48 * value - the value to set in the bit field.
49 * startPos - the starting index of the bit field (beginning from 0).
51 void setBitField(uint64_t* data, uint64_t value, int startPos, int numBits);
53 /* Public: Retreive the nth byte out of 8 bytes in a uint64_t.
55 * source - the source data to retreive the byte from.
56 * byteNum - the index of the byte, starting at 0 and assuming big-endian order.
58 * Returns the requested byte from the source bytes.
60 uint8_t nthByte(uint64_t source, int byteNum);
66 #endif // __BITFIELD_H__