Snippet of Code #3 – Dumping Bits

Sometimes it’s useful to save a little memory by storing data as bits rather than bytes.  After all, if you’re storing a boolean then using a whole byte is rather wasteful.  In any event, sometime you might want to inspect individual bits when working on your program.  If you’ve ever needed to do this then this code will help.

void dumpbits(char byte)
{
	char finalhash[13]={'\0'};
	char hexval[16] = {'0', '1', '2', '3', '4', '5', '6',
                '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
	unsigned char mask_table[] = { 0x01, 0x02, 0x04,
                0x08, 0x10, 0x20, 0x40, 0x80 };

	finalhash[0] = hexval[((byte >> 4) & 0xF)];
	finalhash[1] = hexval[byte & 0x0F];
	finalhash[2] = ' ';
	for (int iterator=0;iterator<8;iterator++)
	{
		if (( byte & mask_table[iterator] ) != 0x00)
			finalhash[3+iterator]='1';
		else
			finalhash[3+iterator]='0';
	}
	printf("%s ",finalhash);
}
CategoriesUncategorised

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.