Representing 128-bit numbers in C++

EDIT: when I first wrote this boost::multiprecision::uint128_t wasn’t a thing yet. Keeping this answer for historical reasons.


I’ve made a uint128 class before, you can check it out at: http://www.codef00.com/code/uint128.h.

It is dependent on boost for automatically providing all of the variants of the math operators, so it should support everything a native unsigned int type does.

There are some minor extensions to built in types such as initializing it with a string like this:

uint128_t x("12345678901234567890");

There is a convenience macro which works similary to the ones in C99 which you can use like this:

uint128_t x = U128_C(12345678901234567890);

Leave a Comment