r/cpp_questions•
pslayer89

Store multiple numbers inside a long.

OPEN

I wanted to know if it's possible to store 2 ints, or 4 shorts in a long datatype.

For example we can store 2 shorts inside an int using:

unsigned short a1 = 9;
unsigned short a2 = 61;
unsigned int a = (a1 << 16) | a2;

And to retrieve the numbers:

unsigned short a1 = a >> 16;
unsigned short a2 = a & 0xFFFF;

I was wondering if the same can be done with 4 unsigned shorts or 2 integers using a long integer.

Posted Apr 15, 2016, 1:36 AM UTC

119
Loading comments…