C代码用于使用受限运算符从原码转换为补码。

huangapple go评论45阅读模式
英文:

C Code to convert from sign magnitude to two's complement using restricted operators

问题

我正在用C语言编写代码,将原码转换为二进制补码。最高位是符号位,输入将是short类型。到目前为止,我有一个代码,但测试未通过(我没有测试案例):

int magtoComp(int x) {
int mask = x >> 15;
int mag = x & ~(1 << 15);
return (mag ^ mask) + ~mask + 1;

英文:

I am writing a code in C to convert from sign magnitude to two's compliment. The most significant bit is the sign bit and the input will be a short type.

thus far I have a code that is failing the test (I do not have test cases):

int magtoComp(int x) {
int mask = x &gt;&gt; 15;
int mag = x &amp; ~(1 &lt;&lt; 15);
return (mag ^ mask) + ~mask + 1;

答案1

得分: 3

计算二进制补码表示中的 -x,可以执行 ~x + 1。如果 sign 是符号位,可以写成 (x ^ -sign) + sign- 不被允许,但可以计算 -sign~sign + 1

因此,这个解决方案:

// 将一个16位符号-幅度表示转换为二进制补码
int shortSignMag2TwosComp(int x) {
    unsigned y = x;                     // 使用无符号算术
    unsigned sign = y >> 15;            // 提取符号位
    unsigned mag = y & ~(1u << 15);     // 屏蔽掉符号位
    return (mag ^ (~sign + 1)) + sign;
}

如果 int 有超过16位,你可以使用 int 替代 unsigned 来符合规则:

// 将一个16位符号-幅度表示转换为二进制补码
// 假设 int 类型具有超过15位值位
int shortSignMag2TwosComp(int x) {
    int sign = x >> 15;            // 提取符号位
    int mag = x & ~(1 << 15);      // 屏蔽掉符号位
    return (mag ^ (~sign + 1)) + sign;
}
英文:

To compute -x in two's complement representation, one performs ~x + 1. if sign is the sign bit, this can be written (x ^ -sign) + sign. - is not allowed but you can compute -sign as ~sign + 1.

Hence this solution:

// convert a 16-bit sign+magnitude representation to 2&#39;s complement
int shortSignMag2TwosComp(int x) {
    unsigned y = x;                     // use unsigned arithmetics
    unsigned sign = y &gt;&gt; 15;            // extract the sign bit
    unsigned mag = y &amp; ~(1u &lt;&lt; 15);      // mask off the sign bit
    return (mag ^ (~sign + 1)) + sign;
}

If int has more than 16 bits, you can use int in place of unsigned to conform to the rules:

// convert a 16-bit sign+magnitude representation to 2&#39;s complement
// assuming type int has more than 15 value bits
int shortSignMag2TwosComp(int x) {
    int sign = x &gt;&gt; 15;            // extract the sign bit
    int mag = x &amp; ~(1 &lt;&lt; 15);      // mask off the sign bit
    return (mag ^ (~sign + 1)) + sign;
}

答案2

得分: 0

假设你的计算机是2的补码形式,这就很简单:

    uint32_t sign = data & (1u << 31);
    if(sign)
    {
      data &= ~(1u << 31);
      data *= -1;
    }

或者,稍微优化一些的版本:

    int32_t signmag_to_2compl (uint32_t signmag)
    {
      int32_t sign = (signmag & (1u << 31)) ? -1 : 1;
      signmag &= ~(1u << 31);    
      return sign * signmag;
    }

`uint32_t`,因为在2的补码机器上运行时,将有符号数存储为`int`没有任何意义。我们还希望避免在有符号数上进行位运算。
英文:

Assuming your computer is 2's complement, it's as easy as:

uint32_t sign = data &amp; (1u &lt;&lt; 31);
if(sign)
{
  data &amp;= ~(1u &lt;&lt; 31);
  data *= -1;
}

Or if you will, a slightly more optimized version:

int32_t signmag_to_2compl (uint32_t signmag)
{
  int32_t sign = (signmag &amp; (1u &lt;&lt; 31)) ? -1 : 1;
  signmag &amp;= ~(1u &lt;&lt; 31);    
  return sign * signmag;
}

uint32_t since it's completely senseless to store a signed magnitude number in an int while running on a 2's complement machine. We also want to avoid doing bitwise arithmetic on signed numbers.

huangapple
  • 本文由 发表于 2023年2月14日 00:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定