Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I multiply 2 32-bit integers to produce a 64-bit integer?

Tags:

c

int

32bit-64bit

How do I take an input of 2 32 bit unsigned integers, multiply them and get the output as a 64 bit integer in C? Any help is appreciated! Thanks.

like image 460
Amritha Avatar asked Feb 27 '12 12:02

Amritha


2 Answers

#include <stdint.h>

uint64_t mul64(uint32_t x, uint32_t y) {
    return (uint64_t)x*(uint64_t)y;
}
like image 66
zr. Avatar answered Oct 19 '22 23:10

zr.


Convert the two integers to 64 bit first, then do a normal multiplication and return the value.

like image 43
Dervall Avatar answered Oct 20 '22 00:10

Dervall