Ç - 转换为二进制补码 [英] C - converting to 2s complement

查看:173
本文介绍了Ç - 转换为二进制补码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定做这种方式


  • 翻转数字0 = 1,1 = 0

  • 加1 LSB

  • 如果随身携带,循环,直到数组[我] == 0

但我卡上的最后一点;我怎么可以说,在有条件的循环?

But I'm stuck on the last point; how can I say that in a conditional loop?

推荐答案

您所谈论的扩展算术。大多数处理器都从每一个加法运算进位和溢出的结果,但C没有为他们提供服务。

You are talking about extended arithmetic. Most processors have carry-out and overflow results from every addition operation, but C does not provide access to them.

您的问题是,数字变得更长,因为他们变得更大。如果你在你的最后一点,你需要来进行,你需要另一位!这意味着你需要重新分配位的数组(如果您使用的是阵列)。

Your problem is that numbers get longer as they get bigger. If you're at the last bit you have, and you need to carry out, you need another bit! That means you need to reallocate the array of bits (if you are using an array).

当然,一个更实际的解决方案是使用原生的整数,而不是单个位,因为你的处理器已经处理二进制补码相当不错。然后,你知道,在进位加一的结果,如果原来的数等于(无符号)-1 。最根本的问题仍然存在;如果你需要进行最后的的无符号你需要分配另一个。

Of course, a more practical solution is to use native integers rather than individual bits, since your processor already handles two's-complement quite nicely. Then, you know that adding one results in a carry-out if the original number is equal to (unsigned) -1. The fundamental problem remains; if you need to carry out of the last unsigned you need to allocate another.

这篇关于Ç - 转换为二进制补码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆