Capturing the top 4 bytes of an 8-byte word

I multiply 0x1d400 * 0xE070381D.

When I do this on my calculator, the result 0x00019A4D26950400

When I tried to implement this in cpp here, what I have.

long long d;

d = 3765450781 * 1d400;

The result obtained by this code is that d = 0x26950400. These are just the bottom 4 bytes, what happened to everything else?

I am trying to allocate the top 4 bytes 0x00019A4Dand store them in another variable. How can I do that?

If I could get the multiplication to display all 8 bytes, what I was thinking of doing was to allocate the top 4 bytes:

d = d & 0xFF00; //0xFF00 == (binary) 1111111100000000

d = d>>8;

Will this work?

+3
source share
3 answers

LL (, 3765450781LL), int, d.

+6

LL long long, MByD.

, unsigned long long. , , - . ( , .)

4 , 4 , .

, , >> - , .

d = d >> 32;

d >>= 32;
+5

As others pointed out, you should suffix your 64-bit numeric literals with LL.

To print the variable long longin hexadecimal format, use the format specifier "%016llX":

long long d;
d = 3765450781LL * 0x1d400LL;
printf("%016llX\n", d);

displays 00019A4D26950400.

To get the upper and lower 32 bits (4 bytes) of a variable d, you can do:

unsigned int upper;
unsigned int lower;

upper = d >> 32;
lower = d & 0x00000000FFFFFFFF;

printf("upper: %08X lower: %08X\n", upper, lower);
+1
source

All Articles