相关疑难解决方法(0)

如何确定宽度是"int"和"unsigned"两倍的整数类型?

中间乘法的值通常需要两倍的位数作为输入.

 // 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 portability

5
推荐指数
1
解决办法
436
查看次数

C类型值的范围

用C标准写的,即一个int应具有:

  • 最低-32767最高32767

但是在实现中,即在16位计算机上,其值为:

  • 最低-32768最高32767

为什么负值会有这种差异?

c

3
推荐指数
1
解决办法
236
查看次数

标签 统计

c ×2

int ×1

portability ×1