中间乘法的值通常需要两倍的位数作为输入.
// Example
int foo(int a, int b, int carry, int rem) {
int2x c; // Some type that is twice as wide at `int`
c = (int2x)a * b + carry;
return (int) (c % rem);
}
Run Code Online (Sandbox Code Playgroud)
考虑到填充的可能性(似乎限制了sizeof()有用性)和非2的补码整数(限制位误),......
以下是否始终创建所需类型?
如果没有,如何编码至少一个合理的解决方案,即使不完全可移植?
#include <limits.h>
#include <stdint.h>
#if LONG_MAX/2/INT_MAX - 2 == INT_MAX
typedef long int2x;
typedef unsigned long unsigned2x;
#elif LLONG_MAX/2/INT_MAX - 2 == INT_MAX
typedef long long int2x;
typedef unsigned long long unsigned2x;
#elif INTMAX_MAX/2/INT_MAX - 2 == …Run Code Online (Sandbox Code Playgroud) 用C标准写的,即一个int应具有:
但是在实现中,即在16位计算机上,其值为:
为什么负值会有这种差异?