# Thread: Storing several characters in an int

1. ## Storing several characters in an int

Code:
```char x = 'x', y = 'y', z = 'z';
int chars = x << 16; chars |= y << 8; chars |= z;

char x_new = chars >> 16, y_new = chars >> 8, z_new = chars;
cout << x_new << " " << y_new << " " << z_new;```
This code allows me to store several characters in one int. I wonder if it can be useful, in the sense that it will take up less space than an array of chars. Please let me know if it can be used to advantage or if it's totally useless

2. actually, you are learning some of what makes C/C++ useful. To answer your question, no it does not take up less memory. An array of chars means an array of bytes. An int is multiple bytes. You are using the bytes regardless of how you declare it. But.... through clever recasting and such, you should see nice patterns for manipulating memory as if it were bytes. Ahh the glory of C!

3. Yes, it should take up less space then a character array, as a character array includes a pointer (4 Bytes) whereas an integer does not.

4. oh, that's an angle I had not considered. Although in practice you are probably talking about an array of integers vs array of char's OR a list of chars vs a single int. In the array case, the pointer exists in both cases. So I stand by my first statement.

5. First, what are you planning on doing if your values somehow become negative? Bit shifting should only be done on unsigned values. Second, how do you know the size of an int? You could be over-running the int on one machine and under using it on another. This is a very non-portable construct.

>Yes, it should take up less space then a character array
Have you tested this? On my system, an array of three char is smaller than an int containing three char by one byte.

This is a clever way of handling chars, but hardly practical except in extreme cases where storage is at a premium. And even then it has to be very scarce for this to be of any real use since the readability of the program is drastically decreased and you spend so many operations dealing with it. By profiling your method and then the same thing with an array of char (create and then reference each element), the result is that the array is quicker by .03 milliseconds.

And yet another argument against it. Are you sure that it's safe? Did you have to look twice to be sure? What about with this:

char array[3] = { 'x', 'y', 'z' };

I'll bet your results are the same as mine, it's easier to check the correctness of the array since it's so common. With bit twiddling you have to go through everything and make sure that it's all kosher before relaxing your guard.

-Prelude

6. agreed. I always consider buffers of unsigned char to be the most reliable way of doing things from one system to another. An unsigned char is a byte on any system. This allows the best use of memory. You can't really get all paranoid about the use of a pointer. yes, it's the same as a long (in win32 at least), but who cares! a single long is nothing to complain about.