12 ENDIANNESS_LITTLE_ENDIAN,
16 uint8_t getNibble(const uint8_t nibble_index, const uint8_t data[],
17 const uint8_t length, Endianness endianness);
19 uint8_t getByte(const uint8_t byte_index, const uint8_t data[],
20 const uint8_t length, Endianness endianness);
22 void getBits(const uint16_t start_index, const uint16_t field_size,
23 const uint8_t data[], const uint8_t length, Endianness endianness,
26 /* Public: Reads a subset of bits from a byte array.
28 * data - the bytes in question.
29 * startPos - the starting index of the bit field (beginning from 0).
30 * numBits - the width of the bit field to extract.
31 * bigEndian - if the data passed in is little endian, set this to false and it
32 * will be flipped before grabbing the bit field.
34 * Bit fields are positioned according to big-endian bit layout, but inside the
35 * bit field, values are represented as little-endian. Therefore, to get the bit
36 * field, we swap the overall byte order if bigEndian == false and
37 * use the value we find in the field (assuming the embedded platform is little
40 * For example, the bit layout of the value "42" (i.e. 00101010 set at position
41 * 14 with length 6 is:
43 * 000000000000001010100000000000000000000000000000000000000000000
45 * and the same value and position but with length 8 is:
47 * 000000000000000010101000000000000000000000000000000000000000000
49 * If the architecture where is code is running is little-endian, the input data
50 * will be swapped before grabbing the bit field.
54 * uint64_t value = getBitField(data, 2, 4);
56 * Returns the value of the requested bit field.
58 uint64_t getBitField(uint64_t data, const uint16_t startPos, const uint16_t numBits, bool bigEndian);
60 /* Public: Set the bit field in the given data array to the new value.
62 * data - a byte array with size at least startPos + numBits.
63 * value - the value to set in the bit field.
64 * startPos - the starting index of the bit field (beginning from 0).
66 void setBitField(uint64_t* data, uint64_t value, int startPos, int numBits);
68 /* Public: Retreive the nth byte out of 8 bytes in a uint64_t.
70 * source - the source data to retreive the byte from.
71 * byteNum - the index of the byte, starting at 0 and assuming big-endian order.
73 * Returns the requested byte from the source bytes.
75 uint8_t nthByte(uint64_t source, int byteNum);
81 #endif // __BITFIELD_H__