减去未签名的实体 [英] Subtracting unsigned entities
问题描述
如果有人知道x和y是无符号整数(大小未知 -
它们可能是短的,int,long,long long),那么最便携的是什么? $ b方式确定它们的区别?
如果x小于y,则xy为负数。
一个天真的方法是
if(xy){
...
}
但是编译器可以编码(x> ; y)as(xy 0)。
-
Fred Kleinschmidt
波音航空C& S
If one knows that x and y are unsigned integers (of unknown size -
they may be short, int, long, long long), what is the most portable
way to determine their difference?
If x is smaller than y, then x-y is negative.
A naive approach is
if ( x y ) {
...
}
but a compiler may code (x>y) as (x-y 0).
--
Fred Kleinschmidt
Boeing Aero C&S
推荐答案
fr * **************** @ boeing.com 说:
如果知道x和y是无符号整数(大小未知 -
它们可能是短的,长的,长的,长的长),什么是最便携的
方式来确定它们之间的差异?>
如果x小于y,那么xy是负数。
一个天真的方法是
如果( xy){
...
}
If one knows that x and y are unsigned integers (of unknown size -
they may be short, int, long, long long), what is the most portable
way to determine their difference?
If x is smaller than y, then x-y is negative.
A naive approach is
if ( x y ) {
...
}
为什么这么天真?
Why is that naive?
>
但编译器可能将(x> y)编码为(xy 0)。
>
but a compiler may code (x>y) as (x-y 0).
不,如果它不能产生正确的结果就不能这样做。
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
No, it can''t do that if it won''t produce the correct result.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
fr ***************** @ boeing.com 写道:
fr*****************@boeing.com writes:
如果有人知道x和y是无符号整数(大小未知 -
它们可能是短的,int,long,long long),什么是最便携的
方法来确定它们的区别?
如果x小于y,则xy为负数。
If one knows that x and y are unsigned integers (of unknown size -
they may be short, int, long, long long), what is the most portable
way to determine their difference?
If x is smaller than y, then x-y is negative.
数学上,是的。但是在C中,
类型unsigned int的两个值的差异总是非负的。
Mathematically, yes. But in C, the difference of two values of
type unsigned int is always nonnegative.
一个天真的方法是
if(xy){
...
}
但是编译器可以编码(x> y) as(xy 0)。
A naive approach is
if ( x y ) {
...
}
but a compiler may code (x>y) as (x-y 0).
不,编译器有义务产生正确的结果。
-
"方式我明白了,一个不同意我的聪明人是
可能是我在任何给定的
日与之互动的最重要人物。
--Billy Chambless
No, the compiler is obliged to produce the correct result.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I''ll interact with on any given
day."
--Billy Chambless
5月12日,11:18 * pm,fred.l.kleinschm ... @ boeing.com写道:
On May 12, 11:18*pm, fred.l.kleinschm...@boeing.com wrote:
如果有人知道x和y是无符号整数(大小未知 -
它们可能是短的,int,long,long long),那么是最便携的
方式来确定它们的区别?
If one knows that x and y are unsigned integers (of unknown size -
they may be short, int, long, long long), what is the most portable
way to determine their difference?
让我们举个例子:
unsigned x,y;
x = 65530u;
y = 655u;
这两者之间的差异是64875.
如果你做(xy),你会得到64875u。但是如果你这样做(yx),你会得到一个实现定义的值,这个值取决于
UINT_MAX的值(因为当你超过零时你就是'将从UINT_MAX开始向后计数
。
Let''s take an example:
unsigned x, y;
x = 65530u;
y = 655u;
The difference between these two is 64875.
If you do (x-y), you''ll get 64875u. However if you do (y-x), you''ll
get an implementation-defined value which depends on the value of
UINT_MAX (because when you go past zero you''ll start counting backward
from UINT_MAX).
如果x小于y,则xy为负数。
天真的方法是
* * if(xy){
* * * ...
* *}
但编译器可能会将代码(x> y)编码为*(xy 0)
If x is smaller than y, then x-y is negative.
A naive approach is
* *if ( x y ) {
* * * ...
* *}
but a compiler may code (x>y) as *(x-y 0)
如果方法是唯一想到的方法。
The if method is the only one that comes to mind.
这篇关于减去未签名的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!