Saturday, January 8, 2011

Bit Operation #6

Extract a single bit from a character.

int extractBit(char byte, unsigned int pos)
{
assert(pos < 8);

return ((byte >> pos) & 1);
}


Extract a range of bits from a character.

char extractBitRange(char byte, unsigned int startPos, unsigned int offset)
{
assert((startPos + offset) < 8);
return (byte >> startPos) & ~(0xfe << offset);
}

No comments: