help with packing

This is a discussion on help with packing within the C Programming forums, part of the General Programming Boards category; I've been reading on how you can compress the actual bits in a program. Below is my code. I've pieced ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Unhappy 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");
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    "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?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    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!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure packing
    By rajkumarmadhani in forum C Programming
    Replies: 3
    Last Post: 11-26-2007, 03:44 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  3. packing a message
    By majoub in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-09-2005, 01:48 PM
  4. Packing into an EXE file
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2002, 04:34 AM
  5. packing two characters into an unsigned integer.
    By Nutshell in forum C Programming
    Replies: 17
    Last Post: 01-30-2002, 02:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21