# Could someone explain this code for me please...

Show 80 post(s) from this thread on one page
Page 4 of 6 First 123456 Last
• 06-22-2005
Dae
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;
• 06-22-2005
Dae
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.
• 06-22-2005
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.

Quote:

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.
• 06-22-2005
JoshR
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(); }```
• 06-22-2005
Dae
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.

Quote:

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. ;)

Quote:

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?
• 06-22-2005
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.
• 06-22-2005
Dae
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.
• 06-22-2005
Dae
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,
• 06-22-2005
JoshR
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.
• 06-22-2005
Hunter2
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.
• 06-22-2005
JoshR
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.
• 06-22-2005
Dae
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.
• 06-23-2005
Ganoosh
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.
Quote:

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?
• 06-23-2005
JoshR
I guess now it lets me hmm weird well here it is, just a small compression idea, wouldnt really be that helpfull
• 06-23-2005
Ganoosh
That's pretty cool, it could be expanded on. Hey where are you reading about this blowfish encryption anyway, I want in hehe.
Show 80 post(s) from this thread on one page
Page 4 of 6 First 123456 Last