1. ## adding together two shorts

I have 2 32-bit unsigned long (DWORD) variables. the values that I am interested are contained in the low order shorts (WORD) of both of the variables. I would like to somehow and these 2 shorts together to form a long, since I know I am dealing with unsigned variables here I don't have to worry about sign bits or anything. I tried this:

Code:
`return LOWORD(dwVer1) & LOWORD(dwVer2);`
without any luck. when I try that I get the low order short/WORD in dwVer2 printed, but that is not what I am trying to achieve here. say for example the low order short/WORD in dwVer1 is 1, and the low order short/WORD in dwVer2 is 2, I am looking for the value 12 to be returned.

would anyone here please be able to help me out here? I obviously don't have much experience with bit manipulation in C, infact I think I will go back and review it again a few times. it's one of those boring topics that I probably went through too quickly, and now I'm regretting it.

any help is appreciated. thanks.

2. You'd shift one of the shorts 32 bits to the left and OR it with the other one. I'll give an example with chars into a short for readability, with spaces between each byte.
Code:
```char1 = 00110111
char2 = 11010101
short = (00110111 << 8) | 11010101
= 00110111 00000000 | 11010101
= 00110111 11010101```
In short (no pun intended):
Code:
```short x = rand();
short y = rand();
long both = x << 32 | y;```

without any luck. when I try that I get the low order short/WORD in dwVer2 printed, but that is not what I am trying to achieve here. say for example the low order short/WORD in dwVer1 is 1, and the low order short/WORD in dwVer2 is 2, I am looking for the value 12 to be returned.
That's not the result you'd get, just so you know. [/edit]

3. Well to join them together, it would be
return LOWORD(dwVer1) | ( LOWORD(dwVer2) << 16 );

To separate them, use >> 16, and mask the result with & 0xFFFF

I tried this:

Code:
```dwRet = osvi.dwMajorVersion << 32 | osvi.dwMinorVersion;
wsprintf(buf, "&#37;d", dwRet);
MessageBox(NULL, buf, NULL, MB_OK);```
but it still prints 1. :/

5. See Salem's post. I misread yours, thinking you just had two shorts you wanted stored in a long. Plus I said 32 instead of 16. Oops.

To use only the lower 16 bits of a variable and zero out the upper 16, use
Code:
`variable & SHRT_MAX`
Then you'd shift one short 16 bits to the left again.
Code:
```unsigned long x = rand();
unsigned long y = rand();
unsigned long both = ((x & USHRT_MAX) << 16) || (y & USHRT_MAX);```
USHRT_MAX is usually 65535 (which you can use instead) and it's in <limits.h>.

6. this prints 5:

Code:
```wsprintf(buf, "&#37;d", LOWORD(osvi.dwMajorVersion));
MessageBox(NULL, buf, NULL, MB_OK);```
this prints 1:

Code:
```wsprintf(buf, "%d", LOWORD(osvi.dwMinorVersion));
MessageBox(NULL, buf, NULL, MB_OK);```
so I'm looking for 51.

this prints 65541:

Code:
```dwRet = LOWORD(osvi.dwMajorVersion) | ( LOWORD(osvi.dwMinorVersion) << 16 );
wsprintf(buf, "%d", dwRet);
MessageBox(NULL, buf, NULL, MB_OK);```
this prints 1:

Code:
```dwRet = ((osvi.dwMajorVersion & 65535) << 16) || (osvi.dwMinorVersion & 65535);
wsprintf(buf, "%d", dwRet);
MessageBox(NULL, buf, NULL, MB_OK);```
:/

7. Right . . . well, what you're looking for can't be easily done with bit shifting. You'll need to do something like this.

Look at the edit at the end before you wade through this post.

Count the number of digits in one number: "1" has one digit, "12", has two digits, and "12345" has five. Then multiply the other number by 10^digits and add them together. It's a lot easier than it sounds. You can count the digits much like this:
Code:
```long num = rand(), digits = 0;
while(num) {
num /= 10;
digits ++;
}```
I'm sure you can code it from there.

Or use strings. You could use sprintf() easily to concatenate the numbers:
Code:
```char both[BUFSIZ];
sprintf(both, "&#37;d%d", x, y);```
Then you could convert both back into a number, with sscanf() or strtol() or something else. I like strtol(), but that's just me.
Code:
`long bothnum = strtol(both, NULL, 0);`
It's in <stdlib.h>, I think. You'd have to check.

Or, if the numbers are always one digit, just use
Code:
`long result = x*10 + y;`
 Bah, it's simple! Just go
Code:
`wsprintf(buf, "%d%d", LOWORD(osvi.dwMajorVersion), LOWORD(osvi.dwMinorVersion));`
Will that work? *printf() is just like printf(). You can have multiple format specifiers. [/edit]