Amritha
Amritha

Reputation: 805

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

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.

Upvotes: 5

Views: 5615

Answers (3)

ouah
ouah

Reputation: 145829

Just convert one of the two integers to uint64_t:

 uint32_t a, b;
 uint64_t c;
 /* assign to a and b */

 c = (uint64_t) a * b;

Upvotes: 2

zr.
zr.

Reputation: 7788

#include <stdint.h>

uint64_t mul64(uint32_t x, uint32_t y) {
    return (uint64_t)x*(uint64_t)y;
}

Upvotes: 14

Dervall
Dervall

Reputation: 5744

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

Upvotes: 6

Related Questions