-
help with packing
I've been reading on how you can compress the actual bits in a program. Below is my code. I've pieced it together, but all that I'm getting is the full 8 bits. My output reads like:
abcd= 01100001 01100010 01100011 01100100
and I was wanting/hoping it would show just the last 4 bits. Am I way off? What about if I was wanting to try numbers instead of letters? I just don't want to be barking up the wrong tree totally ya know? Don't laugh please. Anyway, below is my code for the output above. Any help or suggestions would be greatly appreciated. thanks!
#include <limits.h>
#include <stdio.h>
void bit_print(int a)
{
int i;
int n = sizeof(int) * CHAR_BIT;
int mask = 1 << (n-1);
for (i=1; i<=n; ++i)
{
putchar(((a&mask)==0) ? '0':'1');
a <<=1;
if (i % CHAR_BIT==0 && i < n)
putchar(' ');
}
}
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;
}
int main()
{
printf("abcd= ");
bit_print(pack('a','b','c','d'));
putchar("\n");
}
-
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.
-
"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."
Ok, this will compress the chars. But how would you retrieve those chars back to normal state?
-
for Question C;
What I'm actually wanting to do is to ask the user for some numbers, doesn't matter how many, then after they enter them, it will give the output of the binary representation of that number. Sort of like the binary rep of 'abcd' in the code that I have. I know how to use printf and scanf for input, but for the packing what all needs to be re-declared as something else for it to work with the initial data being integers? Then my second part of that question is after it gives the output of the packed binary rep of those numbers, to give output of the full unpacked binary rep of those numbers below it. Does this help explain my question? If not, I'll try again, just let me know. Thanks!!