c ++ 11中任意大小的最小31位整数

1 c++ binary decimal c++11

我需要获得某个整数的最低31位,这比标准的32位int更大.

获取具有大整数的最低31位的32位int,或者从大整数获得填充的bytearray就足够了.

在c#中我会使用BigInt和.toByteArray - 在c ++ 11中有类似的东西(我是c ++中的noob)吗?

Cam*_*ron 5

屏蔽最低31位并返回结果:

template<typename T>
T mask31(T x) {
    static_assert(std::is_integral<T>::value, "mask31 is only supported on integers!");
    return x & (T)0x7fffffff;
}
Run Code Online (Sandbox Code Playgroud)

如果你知道你正在使用的类型,你当然可以取消模板goop并直接屏蔽内联:-)