Could someone explain this code for me please...

This is a discussion on Could someone explain this code for me please... within the C++ Programming forums, part of the General Programming Boards category; Do you think strings will work in this situation? Anyway nevermind, forget all hypothesizing from above. I did a simple ...

  1. #46
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Do you think strings will work in this situation?

    Anyway nevermind, forget all hypothesizing from above. I did a simple for loops test and found out that even though you can set the char to 255 or 100 or even 1000, its just looping through the same symbols, how many symbols can you loop through? 16! which is the highest bit amount it can take (1111) so it was being truncated.. just in an unnoticable way, if I put in 17 (10001) it would truncate it (result: 0001) and therefor start from the beginning. How I didnt notice this? dont ask my idiot ass.

    So.. there wouldnt really be any real compression using char's because you cant select A or b or C or Z or y unless you went higher than 4 bits, which is 8 bits.. and thats just a normal char. Point defeated. Unless theres some bit shifting method that would make use of it.. hmm like typing by just bit shifting :P thatd be fun.

    Edit: Yeah strings dont seem to be any good here. I know how to use them btw, they are so simple, got to love it. However you cant exactly type: string one:4;
    Last edited by Dae; 06-22-2005 at 10:13 PM.
    Warning: Have doubt in anything I post.

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

  2. #47
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    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);
    Oh I see, well.. yah what do you expect, 254 is a value assigned to a char (like A, B, C, etc.) so its not going to convert to an int and keep the same value.
    Warning: Have doubt in anything I post.

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

  3. #48
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Yes, but if you use and unsigned int, the number will be 4294967294. But when you convert the char to an unsigned int like (unsigned int)test.a, it's setting all bits in the int except the rightmost one to 1 for some reason.

    Edit: Yeah strings dont seem to be any good here. I know how to use them btw, they are so simple, got to love it. However you cant exactly type: string one:4;
    That's true but I was just saying rather than use the struct with chars, just use strings.

    Also yeah, a char is already a byte, so you don't need to do much to it really, but if you do make it smaller, you will be able to hold less chars because it won't hold a value smaller.
    Last edited by Ganoosh; 06-22-2005 at 10:19 PM.

  4. #49
    *this
    Join Date
    Mar 2005
    Posts
    498
    well it does work here is evidence:
    Code:
    #include <iostream>
    using namespace std;
    
    struct char4 {
            unsigned char a:4;
            unsigned char b:4;
    };
    
    union TEST {
            char A;
            char4 bits;
    };
    
    template <typename ChildType>
    string ConvtoBin (ChildType eData) {
       string bits;
       for (int a = 7; a >= 0; a--) 
         bits += ((eData >> a) & 1)+48;
       return bits;
    };
    
    int main() {
        TEST ch;
        ch.A = 100;
        cout << "Size of TEST:  " << sizeof(TEST) << endl;
        cout << "Size of ch:    " << sizeof(ch) << endl;
        cout << "Character A:   " << ch.A << endl;
        cout << "Bit a:         " << ch.bits.a << endl;
        cout << "Bit b:         " << ch.bits.b << endl;
        cout << "-----------------------------" << endl;
        cout << "   INTEGER REPRESENTATIONS   " << endl;
        cout << "-----------------------------" << endl;
        cout << "Character A:   " << static_cast<int>(ch.A) << endl;
        cout << "Bit a:         " << static_cast<int>(ch.bits.a) << endl;
        cout << "Bit b:         " << static_cast<int>(ch.bits.b) << endl;
        cout << "-----------------------------" << endl;
        cout << "    BINARY REPRESENTATIONS   " << endl;
        cout << "-----------------------------" << endl;
        cout << "Character A:   " << ConvtoBin(int(ch.A)) << endl;
        cout << "Bit a:         " << ConvtoBin(ch.bits.a) << endl;
        cout << "Bit b:         " << ConvtoBin(ch.bits.b) << endl;
        
        cin.get();
    }

  5. #50
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Ganoosh
    Yes, but if you use and unsigned int, the number will be 4294967294. But when you convert the char to an unsigned int like (unsigned int)test.a, it's setting all bits in the int except the rightmost one to 1 for some reason.
    Oh yeah thats right, didnt think about that.

    That's true but I was just saying rather than use the struct with chars, just use strings.
    Of course I would do that if I was doing a normal struct, but I'm trying to make a bit field of the char.. cant exactly do that with a string man.

    Also yeah, a char is already a byte, so you don't need to do much to it really, but if you do make it smaller, you will be able to hold less chars because it won't hold a value smaller.
    Yeah.. I dont get what youre saying here, "will be able to hold less chars" .. you can only hold 1 char in the first place. "because it won't hold a value smaller" I think youre trying to say with all this that yah you cant hold more than 1 char but accidently contridicted yourself?
    Warning: Have doubt in anything I post.

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

  6. #51
    *this
    Join Date
    Mar 2005
    Posts
    498
    I think he meant the range for example, 4 bits of max 1111 = 15; so only character values 1-15 will show up anything different will be cut short. It still holds 1 character but just less bits limits it to the number of characters it can hold.

  7. #52
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Haha, nice program~

    In my last post I just saw the light that yeah the binary would be the same when converting >.< I see what you meant by using a union, so it gives using a char thats given a smaller bit field a purpose.. but what sucks is 1111 (:4) only goes to 16, and the actualy letters start way up at 65.. if only there was a way to get letters from those char's in that struct, that would be the ultima.
    Last edited by Dae; 06-22-2005 at 10:49 PM.
    Warning: Have doubt in anything I post.

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

  8. #53
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    I think he meant the range for example, 4 bits of max 1111 = 15; so only character values 1-15 will show up anything different will be cut short. It still holds 1 character but just less bits limits it to the number of characters it can hold.
    Oh, if thats what he meant then he just echo'd my post before that. That was confusing, got it now, thanks,
    Warning: Have doubt in anything I post.

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

  9. #54
    *this
    Join Date
    Mar 2005
    Posts
    498
    you could always XOR it up there, for example 15^100 = 107 which is: k, so if you wanted to change the range you always could.

  10. #55
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Didn't have time to read the entire thread, but: What you guys are talking about is "bitfields". AFAIK, it only works for POD types (as Dae has been saying, it's logical - you can't take a complex structure and randomly tell the compiler to truncate it to a certain size), and makes some nasty problems with the amount of information the variables can hold. Note that it only applies to structs. Also, I saw the idea tossed around that the size of the struct increments 4 bytes at a time, 'because you wanted a new int and an int is 4 bytes'. Rather, I believe this is due to a compiler optimization that aligns all structures to 4 bytes ('word' size, 32-bit cpu's work most efficiently speed-wise with 4 bytes). As mentioned by xErath some time ago, you can usually find a compiler option to force byte-by-byte packing (or 2-byte packing or whatever you want), although you can never have a fraction of a byte (logically).

    Also note, the use of bitfields will generally slow you down quite a bit, since rather than directly accessing the bits, the compiler has to generate code that uses bitwise operations (probably a mask and then right-shift) to get the information you want, and more code to set the information you want (probably an &= with the complement of a mask, then |= the result of left-shifting the result of &'ing the value with another mask). For the reasons of packing problems and slowdown problems, generally bitfields will NOT be a worthwhile optimization.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #56
    *this
    Join Date
    Mar 2005
    Posts
    498
    I see your point, but optimization wasnt my reasoning, I saw it in the blowfish encryption algorithm and I felt like I wanted to use it in my own encryption algorithm. Thanks for the clarification.

  12. #57
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yeah this thread was a lot of fun, but as Hunter2 said this isnt really optimizing, which is where I was hoping it would head to.

    Oh and yeah no idea who in this through thought structs only increment by 4 bytes each time. (I hypothesized that maybe a struct was an int for a post because of wierd results though, hehe)

    I'm with JoshR, probably wont be too bad in an encryption algorithm. By the way you got another attachment to that program you made? gmail said it was gone.
    Last edited by Dae; 06-23-2005 at 09:18 AM.
    Warning: Have doubt in anything I post.

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

  13. #58
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Yeah, I don't see a reason for using bitfields, I read they weren't commonly used, but it was cool debugging and trying to figure out what they were.
    Hunter2, I posted a program earlier that did what you were saying, but it had less operations, but also it was very strict it would change a bit if it were able to be used with other things.
    By the way you got another attachment to that program you made? gmail said it was gone.
    Yeah, I was never able to get that program. Perhaps post it another way and link to it again?

  14. #59
    *this
    Join Date
    Mar 2005
    Posts
    498
    I guess now it lets me hmm weird well here it is, just a small compression idea, wouldnt really be that helpfull
    Attached Files Attached Files

  15. #60
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    That's pretty cool, it could be expanded on. Hey where are you reading about this blowfish encryption anyway, I want in hehe.

Page 4 of 6 FirstFirst 123456 LastLast
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, 03: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, 12: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-15-2002, 11:04 PM

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