Hi,

I had a thought the other day, about packing two or more data type's into another larger data type. Then perform some addition/subraction operations on the larger data type, so you basically get two operations for the price of one.

Here is an example of what i mean:

short int a,b,c,d;

unsigned short int ab,cd;

long int e,f,result;

/* pack a & b into e */

/* pack c & d into f */

result = e + f;

/* unpack e and f, store in ab and cd */

Would ab now hold a+b, and cd hold c+d?

Say two chars were packed into a short...

12345678 | 12345678

----------------------------

00000001 | 00000001 dec 513 OR 1 and 1 as 2 chars

00000001 | 00000001 dec 513 OR 1 and 1 as 2 chars

*PERFORM*ADDITION*

00000010 | 00000010 dec 1026 OR 2 and 2 as two chars

I take it that the above would save an addition operation, instead of adding 2 chars to one another, then adding another 2 chars to one another.

Assuming that all this is possible, I would guess that there would be some sort of overhead involved in packing and unpacking, but how much?

I am quite new to C, so if I appear to have been on the crack pipe, please say.

I am just interested in the proof of concept of doing this, or ideally, any real world examples of it.