Click here to Skip to main content
15,883,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I would like to concatenate into an unsigned long the following shorts array and be able to decompact it to rebuild the shorts array.


How could I schrink it and avoid overflow ?
Thank you very much in advance.
Best regards.
MiQi

What I have tried:

Here is what I have so far:

C++
short exp[10];
exp[0] = 1; exp[1] = 0; exp[2] = -2; exp[3] = 0; exp[4] = -1; exp[5] = 0; exp[6] = 2; exp[7] = 0; exp[8] = 0; exp[9] = 1;

for (int index = 0; index < sizeof(exp) / sizeof(short); index++)
{
   if (index % 2 == 0)
      exp[index] = 5;   // 0101
   else exp[index] = -5;// 1101
}

LARGE_INTEGER uiCode;
uiCode.QuadPart = 0;
for (index = 0; index < sizeof(exp) / sizeof(short); index++)
{
  uiCode.QuadPart = uiCode.QuadPart | (exp[index] & 0x000f);
  uiCode.QuadPart = uiCode.QuadPart << 4;
}
            
LARGE_INTEGER uiExpectedCode;
uiExpectedCode.QuadPart = uiCode.QuadPart / LONG_MAX;

LARGE_INTEGER uiResiduCode;
uiResiduCode.QuadPart = uiCode.QuadPart % LONG_MAX;

long SchrinkedCode =  ??;
Posted
Updated 15-Mar-16 2:32am

1 solution

It is not quite clear what you want to achieve but there are some possible errors.

Here -5 is not binary 1101:
else exp[index] = -5;// 1101

-5 is hex 0xFFFB. So the lowest 4 bits are 1011.

The loop to shift the nibbles (4 bit values) into the quad part should probably shift first:
for (index = 0; index < sizeof(exp) / sizeof(short); index++)
{
  uiCode.QuadPart <<= 4;
  uiCode.QuadPart |= (exp[index] & 0x000f);
}

Otherwise the lowest four bits will be always zero and the upper bits may get shifted out.

Finally a tip. Arrays can be initialised upon definition:
short exp[10] = { 1, 0, -2, 0, -1, 0, 2, 0, 0, 1 };
 
Share this answer
 
Comments
SuperMiQi 15-Mar-16 8:36am    
Hello Jochen,
I am just looking for a way to generate a unsigned long representation of shorts array. A kind of dim code which will allow me to unpack it and rebuild the shorts array.

Currently the entire application I have to maintain is based on unsigned long otherwise I would be able to use LARGE_INTEGER or other types.

Do you have a magic idea ?

Thank you very much in advance,
Best regards.
MiQi.
Jochen Arndt 15-Mar-16 8:50am    
Your above code (with my modified loop) should work when the short array contains 4-bit values (only the four least signficant bits are used).

But when you are going to pack shorts, you must shift by the number of bits for short (typically 16):
for (index = 0; index < sizeof(exp) / sizeof(short); index++)
{
uiCode.QuadPart <<= sizeof(short) * 8;
uiCode.QuadPart |= exp[index];
}
Then you must also check for overflow because LARGE_INTEGERs are 64 bit (can hold 4 words, 8 bytes, or 16 nibbles).

So you should clarify this. Once the values has been "packed" in some way, reconstruction is just performing the reverse operation.
SuperMiQi 15-Mar-16 8:57am    
Hello Jochen,
Nice but I need to store into an unsigned long ?

Do you have a magic idea ?
This is where I am locked currently.


Thank you very much in advance,
Best regards.
MiQi.
Jochen Arndt 15-Mar-16 9:05am    
It's just the same with unsigned long. But the size of unsigned long is usually 32 bit. So you can store less shorts inside. With short it may be just:
unsigned long val = exp[0] | (exp[1] << 16);
If using unsigned long long is an option: That is 64 bit like LARGE_INTEGER.

But it is still unclear how your input data are organised. Are they short (16 bit) or 4-bit values stored as short?
And how many input values do you have.
If there are too much, the output must be an array too.
SuperMiQi 15-Mar-16 14:20pm    
Hello Jochen,

As the numnber of shorts is bellow 9 items, I will have to change the unsigned long pattern system implemented by one of my previous colleague to a different more flexible solution,
Thank you anyway for your inputs.

Best regards.
MiQi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900