How can I add and subtract 128 bit integers in C or C++ if my compiler does not support them?

后端 未结 7 734
我在风中等你
我在风中等你 2020-11-28 11:07

I\'m writing a compressor for a long stream of 128 bit numbers. I would like to store the numbers as differences -- storing only the difference between the numbers rather th

7条回答
  •  -上瘾入骨i
    2020-11-28 11:53

    There is a lot of literature regarding large integer math. You can use one of the libraries freely available (see links) or you can roll your own. Although I should warn you, for anything more complicated than addition and subtraction (and shifts), you'll need to use non-trivial algorithms.

    To add and subtract, you can create a class/structure that holds two 64-bit integers. You can use simple school math to do the addition and subtraction. Basically, do what you do with a pencil and paper to add or subtract, with careful consideration to carries/borrows.

    Search for large integer. Btw recent versions of VC++, IntelC++ and GCC compilers have 128-bit integer types, although I'm not sure they are as easily accessible as you might like (they are intended to be used with sse2/xmms registers).

    • http://en.wikipedia.org/wiki/Arbitrary_precision_arithmetic
    • http://orion.math.iastate.edu/cbergman/crypto/bignums.html
    • http://www.mathgoodies.com/tutorial/

提交回复
热议问题