【问题标题】:Modify line of code in c to NOT contain unsigned variable or casting. (?)修改 c 中的代码行以不包含无符号变量或强制转换。 (?)
【发布时间】:2015-06-03 21:23:30
【问题描述】:

如何在不使用 unsigned 标记的情况下修改 ANSI C 中的以下行?

unsigned int x, y, z; // unsigned variables should not be used
/*... some operations where x, y and z gets values between 0x0 and 0xFFFFFFFF ... */
x = (unsigned int)-(int)(y * z); // line to modify

【问题讨论】:

  • 我不明白。声明三个unsigned int 类型变量的那一行标有注释“不应使用无符号变量”。

标签: c unsigned unsigned-integer ansi-c


【解决方案1】:

您可以直接将否定应用于无符号值。结果将以 2^N 为模减少,这就是您现有代码的作用。

x = -(y * z);

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    相关资源
    最近更新 更多