C 中不带减号的减法
在 C 中如何在不使用 -
运算符的情况下减去两个整数?
How can I subtract two integers in C without the -
operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 C 中如何在不使用 -
运算符的情况下减去两个整数?
How can I subtract two integers in C without the -
operator?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(17)
您可以使用求反并加 1 将 b 转换为负值:
这是二进制补码负数。 当您想要否定值或减去它时使用“-”运算符时,处理器就会执行此操作。
转换浮点数更简单。 只需否定第一位(shoosh 给了你如何做到这一点的示例)。
编辑:
好的,伙计们。 我放弃。 这是我的独立于编译器的版本:
我使用 unsigned int 以便任何编译器都会同等对待它。
如果你想减去负值,那么就这样做:
现在我们完全独立于有符号值约定。 在我的方法结果中,所有整数都将存储为二进制补码 - 因此您必须小心较大的整数(它们必须以 0 位开头)。
You can convert b to negative value using negation and adding 1:
This is two's complement sign negation. Processor is doing it when you use '-' operator when you want to negate value or subtrackt it.
Converting float is simpler. Just negate first bit (shoosh gave you example how to do this).
EDIT:
Ok, guys. I give up. Here is my compiler independent version:
I'm using unsigned int so that any compiler will treat it the same.
If you want to subtract negative values, then do it that way:
Now we are completly independent of signed values conventions. In my approach result all ints will be stored as two's complement - so you have to be careful with bigger ints (they have to start with 0 bit).
Pontus 是对的,2 的补码不是 C 标准所强制的(即使它是事实上的硬件标准)。 菲尔的创意答案+1; 这是在不使用标准库或 -- 运算符的情况下获取 -1 的另一种方法。
C 要求三种可能的表示形式,因此您可以嗅探正在运行的表示形式,并为每个表示形式获取不同的 -1:
值 0x7FFFFFFFE 将取决于您感兴趣的整数类型的宽度(“值位”的数量); 如果未指定,您需要做更多工作才能找到答案!
Pontus is right, 2's complement is not mandated by the C standard (even if it is the de facto hardware standard). +1 for Phil's creative answers; here's another approach to getting -1 without using the standard library or the -- operator.
C mandates three possible representations, so you can sniff which is in operation and get a different -1 for each:
The value 0x7FFFFFFFE would depend on the width (number of ‘value bits’) of the type of integer you were interested in; if unspecified, you have more work to find that out!
Expand ab:
Manufacture -1:
Expand a-b:
Manufacture -1:
将我们限制在数字空间 0 <= c < (a+b):
简化第二项:
代入:
如果b>a,则ba>0,所以:
所以,如果a总是大于b,那么这将起作用。
restricting ourselves to the number space 0 <= c < (a+b):
simplifying the second term:
substituting:
if b>a, then b-a>0, so:
So, if a is always greater than b, then this would work.
鉴于 C 中不强制编码整数以支持二进制补码,因此迭代直至完成。 如果他们想让你跳过火圈,没必要提高效率!
愚蠢的问题......可能是愚蠢的采访!
Given that encoding integers to support two's complement is not mandated in C, iterate until done. If they want you to jump through flaming hoops, no need to be efficient about it!
Silly question... probably silly interview!
要在 C 中减去两个整数,您只需要:
我不相信对于浮点或双精度数字(如整数)有一个简单而优雅的解决方案。 因此,您可以转换数组中的浮点数,并应用与模拟
For subtracting in C two integers you only need:
I don't believe that there is a simple an elegant solution for float or double numbers like for integers. So you can transform your float numbers in arrays and apply an algorithm similar with one simulated here
如果您想对浮点数执行此操作,请从正数开始并更改其符号位,如下所示:
您还可以使用适当的 64 位整数对双精度数执行此操作。 例如,在 Visual Studio 中,这是 __int64。
If you want to do it for floats, start from a positive number and change its sign bit like so:
You can also do this for doubles using the appropriate 64 bit integer. in visual studio this is __int64 for instance.
我想这个
b - a = ~( a + ~b)
I suppose this
b - a = ~( a + ~b)
装配(累加器)样式:
Assembly (accumulator) style:
由于问题要求整数而不是 int ,您可以实现一个小型解释器,而不是使用 教堂数字。
As the question asked for integers not
int
s, you could implement a small interpreter than uses Church numerals.为每个可能的 int-int 情况创建一个查找表!
Create a lookup table for every possible case of int-int!
未测试。 不使用 2 的补码:
假设
int
的长度远小于 255,并且 snprintf/sscanf 往返不会产生任何未指定的行为(对吗?对吗?)。可以使用
a - b == a + (-b) 来计算减法。
替代方案:
Not tested. Without using 2's complement:
Assuming the length of an
int
is much less than 255, and the snprintf/sscanf round-trip won't produce any unspecified behavior (right? right?).The subtraction can be computed using
a - b == a + (-b).
Alternative:
这可以使用整数溢出:
这也适用于浮点数(假设你制作了浮点数版本......)
This would work using integer overflow:
This also works for floats (assuming you make a float version…)
对于任何数据类型的最大范围,补码提供负值减 1 到任何对应值。 例如:
~1 --------> -2
~2---------> -3
等等...我将使用很少的代码片段向您展示这一观察
结果输出:0
注意:这仅对数据类型范围有效。 意味着对于 int 数据类型,此规则仅适用于范围 [-2,147,483,648 到 2,147,483,647] 的值。
谢谢.....愿这对你有帮助
For the maximum range of any data type , one's complement provide the negative value decreased by 1 to any corresponding value. ex:
~1 --------> -2
~2---------> -3
and so on... I will show you this observation using little code snippet
Output: 0
Note : This is valid only for the range of data type. means for int data type this rule will be applicable only for the value of range[-2,147,483,648 to 2,147,483,647].
Thankyou .....May this help you
如果:
0
,或者0
,或者0
将被减数乘以
-1
并将结果添加到减数:否则将被减数乘以
1
并将结果添加到减数。示例(在线尝试):
输出:
Iff:
0
, or0
, or0
multiply the Minuend by
-1
and add the result to the Subtrahend:Else multiply the Minuend by
1
and add the result to the Subtrahend.Example (Try it online):
Output: