Thread: Could someone explain this code for me please...

  1. #31
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    The compression only works if you have it set up evenly. For example an int is 4 bytes, if you have 4 ints of 8 bits, it will compress it into a single int of 4 bytes. If you have double (8 bytes) and 4 of them each 16 bits wide, then the struct should only take up 8 bytes instead of 8*4 bytes with no bitfields.
    Code:
    struct{
        char a:4;
        char a:4;
    }
    If a char is only one byte, which I believe it is, then the struct should only be one byte insize, rather than 2, because it splits evenly.
    And no you can't allocate less than a byte, it's the smallest unit of memory. For example if you only needed 2 bits and wanted to allocate those 2 bits, you'd have to allocate a whole byte just for those 2 bits.

  2. #32
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yeah I get all that, but:

    For example an int is 4 bytes, if you have 4 ints of 8 bits, it will compress it into a single int of 4 bytes.
    If you have 4 ints of 6 bits then you can only store 1 byte in each of them, which ends up being 4 bytes total of course.. which is 1 int, so you arent getting any extra space. Ohhh!! wait, its compression because you can store 4 number values (1 for each of those ints) but it doesnt actually compress, it just makes it so youre able to store 4 different numbers in the size of 1 int in the end, whereas if you only had 1 full int you would only be able to store 1 number because you cant split it up. I get that now, cool, but that means youre limited (like you said up there) to the size of your number because you only have 8 bits to use.. so no big numbers.

    Alright theories and solutions out there, time for me to get down and dirty and run tests to help me grasp the concept.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #33
    *this
    Join Date
    Mar 2005
    Posts
    498
    Well it can compress because you essentially string together the bits of 4 integers (each less than or equal to 11111111, 255), because an integer that is less than that binary value, already takes up full 4 bytes so by stringing them together you use all the bits and not just 8 bits, then you could use the same structure and unstring it, ill put together an example and show you. I am definetly using this in my encryption algorithm, then im going to add an algorithm for switching certain blocks of bits from one character to another etc...
    Last edited by JoshR; 06-22-2005 at 05:39 PM.

  4. #34
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    Well it can compress because you essentially string together the bits of 4 integers (each less than or equal to 11111111, 255), because an integer that is less than that binary value, already takes up full 4 bytes so by stringing them together you use all the bits and not just 8 bits, then you could use the same structure and unstring it, ill put together an example and show you. I am definetly using this in my encryption algorithm, then im going to add an algorithm for switching certain blocks of bits from one character to another etc...
    Oh yeah definetly, I get the point since my last post, and after re-reading the old posts I see the points there too. This would be awesome to use, if only I had some sort of challenge program to write so I could actually get some experience with it. But first, I must fiddle around, and read that bitwise tutorial on this site.

    Any examples you got would be wicked, keep the info coming
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #35
    *this
    Join Date
    Mar 2005
    Posts
    498
    Last edited by JoshR; 06-22-2005 at 06:00 PM.

  6. #36
    *this
    Join Date
    Mar 2005
    Posts
    498
    I am working on an encryption algorithm right now, see the problem with the thing above is that the value has to be less than or equal to 255 so there could be other posibilities, like doing some bit operations to manipulate the numbers before stringing them

  7. #37
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yeah after testing Ganoosh is definetly right, it will allocate 8 bits for that number, and the next int after that will have 8 bits too and they wont mix together like xErath said (like if int zero:8 only uses 5 bits, and int one:8 uses 9 bits, they wont take bits from eachother).

    Its working quite nicely, it works with the char's too. However I find something odd. In a char struct I declare that each char is using 4 bits, and the struct itself adds up to 1 byte, which is fine. So each char can only use 1111 bits max, however it allows me to assign each of the chars to 255 (which is 11111111 bits (1 byte)). Which means its not truncating the extra assigned bits like it did with the int example you gave (where you put in 400 in a int zero:8 and it truncated the extra bits and resulted in 144).. why doesnt it do that for the char when assigning it 255 or any other number thats more than 4 bits.

    Code:
    struct compressed {
         char a:4;
         char b:4;
    };
    
    int main() {
         compressed test;
    
         test.a = 254;
         test.b = 254;
    
         cout << "sizeof(test):       " << sizeof(test) << "/n" << endl; // returns 1 byte
         cout << "a:   " << test2.a << endl; // returns a 1 byte icon
         cout << "b:   " << test2.b << endl; // returns a 1 byte icon
    }
    Last edited by Dae; 06-22-2005 at 06:45 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #38
    *this
    Join Date
    Mar 2005
    Posts
    498
    because you could only assign up to 4 bits, so you are directly assigning the 4 bit data an 8 bit integer(because its less than 255). so it only uses the first 4

    Since 4 bits (1111) = 15;
    you can only use a value <= 15;
    Last edited by JoshR; 06-22-2005 at 07:45 PM.

  9. #39
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    because you could only assign up to 4 bits, so you are directly assigning the 4 bit data an 8 bit integer. so it only uses the first 4

    Since 4 bits (1111) = 15;
    you can only use a value <= 15;
    No thats exactly my point. Thats how it SHOULD be, it should only be <= 15, but tests are allowing it to go up to <= 255 (8 bits). Um where do you get 8 bit integer? (the struct itself is an 8 bit integer?) read my code again and can you re explain please?

    see the problem with the thing above is that the value has to be less than or equal to 255 so there could be other posibilities
    Of course the great thing is you have flexibility though,

    Code:
    struct compressed {
         unsigned int zero:16;
         unsigned int two:8;
         unsigned int four:4;
         unsigned int three:4;
    };
    // Edit: oh I see, the variables inside the struct are declaring their amount of bits they want to use from that struct, which adds up to an even 1, 2, or 4 bytes or whatever. However when you assign a number that uses more bits than the variable can hold its suppose to truncate it like it did for the integer example (400 > 255 which is 9 to 8 bits, so it truncated 1 bit), but when you assign any number >= 16 to a 4 bit char it doesnt truncate it, it doesnt care, it still acts like that char has 8 bits. As shown in my last post example. And not only that but it allows both of the char's to be set to 8 bits, adding up to 16 bits. But when you sizeof that struct, it says 1 byte, which is 8 bits. In conclusion wouldnt that result in decompression? Meaning we were able to assign a small struct and decompress it on assigning values to the char's within them? (because you cant get any lower than 8 bits (1 byte)) If so.. thats a pretty good method to use if you could find a way to take advantage.
    Last edited by Dae; 06-22-2005 at 07:27 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #40
    *this
    Join Date
    Mar 2005
    Posts
    498
    ya that is a weird example, because if you check the integer value of the character after its not 254 its -2 so something is happening, if you make a union and have there be an integer in there and assign 254 to the integer then have it use the char struct, it will show you what you want, but other than that, that wont work. sorry for such a bad explanation im in a rush.

  11. #41
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    That's interesting with the chars, maybe it's working sort of a like a union? I'm gonna run a test and post the code if I figure it out.

  12. #42
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    ya that is a weird example, because if you check the integer value of the character after its not 254 its -2 so something is happening, if you make a union and have there be an integer in there and assign 254 to the integer then have it use the char struct, it will show you what you want, but other than that, that wont work. sorry for such a bad explanation im in a rush.
    Thats an explanation? you mean solution to something I'm not even trying to do. I was just testing and noticed the char's arent acting the same way as the int's when it comes to the bits being truncated, I'm not actually trying to do anything. And I'm not sure youve tested my code because it is working, its just its treating char a:4; as if it was char a; when you assign values to it, but when you retrieve the sizeof, it returns a value as if its char a:4; (which it is). Odd eh.

    I was also thinking it might be acting like a union, but that cant be because in the example b is assigned right after a, and yet they both print out what was assigned to them. I think whats happening is it isnt allowing char a:4; to actually create a char with 4 bits, but it will still compile and the sizeof is still going to think its using a:4 because thats whats in the coding. Sort of like.. they just forgot to put an error for assigning a bit value to char's in the standard.

    If you're testing, would you mind seeing if you can get it so the struct variables can take in char strings (using an array if necessary) or somehow. That would be the ultimate! just test.. 1 char or 2 chars even, says I cant convert to char const* and I'm sort of a newbie when it comes to char strings.
    Last edited by Dae; 06-22-2005 at 08:21 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #43
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Well, I did a little test, however it is truncating. But when I assign a value that will fit 4 bits I get some strange numbers. Run this test.
    Code:
    #include <iostream>
    using namespace std;
    
    struct char4 {
            char a:4;
            char b:4;
    };
    
    int main() {
        char4 test;
        cout<<"sizeof char4: "<<sizeof(char4)<<endl; //1 byte
        test.a = 128; // a is truncated to 0 because 128 will not fit in 4 bits
        cout<<"a: "<<test.a<<" "<<(unsigned int)test.a<<endl;
        test.b = 10; // 10 fits in 4 bits, so b should be 10
        cout<<"b: "<<test.b<<" "<<(unsigned int)test.b<<endl; // outputs a very large number that
        // would not even fit in a byte. 
        cout<<"sizeof test: "<<sizeof(test)<<endl; // 1 byte
        cin.get();
    }
    As you can see the 128 is being truncated. Also remember to use unsigned ints, that way the sign bit is treated as part of the number. I'm also getting some strange number for a number like 254.

  14. #44
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    char strings are outdated, unless you're using C. Try C++ strings: http://www.cprogramming.com/tutorial/string.html

  15. #45
    *this
    Join Date
    Mar 2005
    Posts
    498
    What my explanation was that when you assign 254 to char, it might look like the same character and output the same, but the number representation changes from 254 to -2, if you output it with static_cast<int>(test.a);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM