C++ 128/256-bit fixed size integer types

The Boost library has data types as part of multiprecision library, for types ranging from 128 to 1024 bits. #include <boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; int128_t mySignedInt128 = -1; uint128_t myUnsignedInt128 = 2; int256_t mySignedInt256 = -3; uint256_t myUnsignedInt256 = 4; int512_t mySignedInt512 = -5; uint512_t myUnsignedInt512 = 6; int1024_t mySignedInt1024 = -7; uint1024_t myUnsignedInt1024 = … Read more

How to use long id in Rails applications?

Credits to http://moeffju.net/blog/using-bigint-columns-in-rails-migrations class CreateDemo < ActiveRecord::Migration def self.up create_table :demo, :id => false do |t| t.integer :id, :limit => 8 end end end See the option :id => false which disables the automatic creation of the id field The t.integer :id, :limit => 8 line will produce a 64 bit integer field

Efficient 128-bit addition using carry flag

Actually gcc will use the carry automatically if you write your code carefully… Current GCC can optimize hiWord += (loWord < loAdd); into add/adc (x86’s add-with-carry). This optimization was introduced in GCC5.3. With separate uint64_t chunks in 64-bit mode: https://godbolt.org/z/S2kGRz. And the same thing in 32-bit mode with uint32_t chunks: https://godbolt.org/z/9FC9vc (editor’s note: Of course … Read more

What is the simplest way of implementing bigint in C?

If you’re looking for a simple library, libtommath (from libtomcrypt) is probably what you want. If you’re looking to write a simple implementation yourself (either as a learning exercise or because you only need a very limited subset of bigint functionality and don’t want to tack on a dependency to a large library, namespace pollution, … Read more