views:

398

answers:

8

This is for MSVC

#define Get64B(hi, lo) ((((__int64)(hi)) << 32) | (unsigned int)(lo))

Specifically, what is the role of the 'operator <<' ?

Thanks for your help

+17  A: 

<< is the left shift operator. This macro is intended to make a 64-bit value from two 32 bit values, using the first argument as the top 32 bits and the second argument as the bottom 32 bits of the new value.

AakashM
+3  A: 

It takes two 32 bit integers and returns a 64 bit integer, with the first parameter as the 32 high bits and the second as the 32 low bits.

<< is the left shift operator. It takes the high 32 bits, shifts them over, and then ORs the that result with the low bits.

FreeMemory
+2  A: 

operator << is a binary left shift operator. It shifts the int64 variable hi left by 32 bits.

stanigator
A: 

It bit shifts the value of hi to the left by 32 bits.

Soo Wei Tan
A: 

That's the left shift operator, and its standard meaning (for number types) is shifting bits to the left

int a = 1;
int b = a << 3; // b is now 1000 binary, 8 decimal

The code creates a 64 bit number out of two 32 bit numbers.

Johannes Schaub - litb
+2  A: 

AakashM is correct. It may be easier to understand written as a method

__int64 Get64B(__int32 hi, __int32 lo) {
  __int64 combined = hi;
  combined = combined << 32;  // Shift the value 32 bits left.  Combined
                              // now holds all of hi on the left 32 bits
  combined = combined | lo;   // Low 32 bits now equal to lo
  return combined;
}
JaredPar
A: 

That returns a 64 bit int using two 32 bit int, one is used as de hi order bytes and the second one as the low order bytes.

hi << 32 converts the integer to the high order bytes of the 64 bit int. Example:

Get64B (11111111111111110000000000000000, 000000000000000011111111111111111)

returns 11111111111111110000000000000000000000000000000011111111111111111

Because 11111111111111110000000000000000 << 32 returns

1111111111111111000000000000000000000000000000000000000000000000

AlbertEin
A: 

Return a 64-bits integer of two 8,16,32 or 64-bits integers. This safer as: hi << 32 | lo

bill
Shifting hi by 32 bits without first casting it to a 64bit int will cause it to overflow.
Joe Gauterin
This is exactly what i'm explaining. You must first cast hi to 64bits. This is done by (__int64)hi. It's not safe to write: h << 32 | lo. The macro can be simplified to: "(__int64)hi << 32 | lo", when hi and lo are "unsigned char", "unsigned short" or "unsigned".
bill