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

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

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

问题

  1. 我正在用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):

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

答案1

得分: 3

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

因此,这个解决方案:

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

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

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

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:

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

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

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

答案2

得分: 0

  1. 假设你的计算机是2的补码形式,这就很简单:
  2. uint32_t sign = data & (1u << 31);
  3. if(sign)
  4. {
  5. data &= ~(1u << 31);
  6. data *= -1;
  7. }
  8. 或者,稍微优化一些的版本:
  9. int32_t signmag_to_2compl (uint32_t signmag)
  10. {
  11. int32_t sign = (signmag & (1u << 31)) ? -1 : 1;
  12. signmag &= ~(1u << 31);
  13. return sign * signmag;
  14. }
  15. `uint32_t`,因为在2的补码机器上运行时,将有符号数存储为`int`没有任何意义。我们还希望避免在有符号数上进行位运算。
英文:

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

  1. uint32_t sign = data &amp; (1u &lt;&lt; 31);
  2. if(sign)
  3. {
  4. data &amp;= ~(1u &lt;&lt; 31);
  5. data *= -1;
  6. }

Or if you will, a slightly more optimized version:

  1. int32_t signmag_to_2compl (uint32_t signmag)
  2. {
  3. int32_t sign = (signmag &amp; (1u &lt;&lt; 31)) ? -1 : 1;
  4. signmag &amp;= ~(1u &lt;&lt; 31);
  5. return sign * signmag;
  6. }

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:

确定