Hmm. This is something I am actually fairly interested in, so if you could explain your question a bit more, I'd appreciate it. As it is, I'm just looking at this function...
Code:
int pack(char a, char b, char c, char d)
{
int p = a;
p = (p<<CHAR_BIT)|b;
p = (p<<CHAR_BIT)|c;
p = (p<<CHAR_BIT)|d;
return p;
}
This function takes 4 chars and puts them together into an int. If you want to cut off the first 4 bits of each char, well, here's how you'd do it...
Code:
int pack(char a, char b, char c, char d)
{
int p = a & 0x0F; // &0x0F will change 01100001 into 00000001
p = (p<<CHAR_BIT)|(b & 0x0F);
p = (p<<CHAR_BIT)|(c & 0x0F);
p = (p<<CHAR_BIT)|(d & 0x0F);
return p;
}
That code will result in the value 00000001 00000010 00000011 00000100. Of course, if you want to not include all that extra space 0s, then you have to not shift p left a full char each time you add a new value, you only want to shift it half a char, since you only want to keep half of the char's value...
Code:
int pack(char a, char b, char c, char d)
{
int p = a & 0x0F; // &0x0F will change 01100001 into 00000001
p = (p<<(CHAR_BIT / 2))|(b & 0x0F);
p = (p<<(CHAR_BIT / 2))|(c & 0x0F);
p = (p<<(CHAR_BIT / 2))|(d & 0x0F);
return p;
}
Will result in 00000000 00000000 00010010 00110100. So now we've compressed the 4 chars to only take half an int each. Of course, since we're storing them in a 4-byte int, there's still gonna be that blank space. You can make up that by either using something smaller (like a short), or packing 8 chars into the int instead of 4.