确定两个整数的最大值。 [英] Determine the maximum of two integers.

查看:81
本文介绍了确定两个整数的最大值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下函数确定两个整数的最大值。它是
在我的机器上运行。


如果(a [0] - a [1])是负数,那么第一位是: (未签名)(a [0]

- a [1])?它是0还是1?

#include< limits.h>


int max(int n1,int n2)

{

int a [2];

a [0] = n1,a [1] = n2;

返回[(( unsigned)(a [0] - a [1])> sizeof n1 * CHAR_BIT - 1)];

}

感谢您的时间

解决方案

lovecreatesbea ... @ gmail.com写道:


以下函数确定最大值两个整数。它是
在我的机器上运行。


如果(a [0] - a [1])是负数,那么第一位是: (未签名)(a [0]

- a [1])?它是0还是1?


#include< limits.h>


int max(int n1,int n2)

{

int a [2];

a [0] = n1,a [1] = n2;

返回a [((unsigned)(a [0] - a [1])> sizeof n1 * CHAR_BIT - 1)];

}


谢谢你的时间



这适用于二进制补码和符号级机器。我怀疑

你会遇到任何其他类型。


它会在减法给出的补充机器上失败

值-0,它会在任何其他类型的表示中失败,因为

例如多余的N.


然而它是一个非常复杂的方式比较两个整数。如果我在代码审查期间发现它是
,我会强迫我的开发人员删除它。


顺便说一下,这不是一个C问题。下次在comp.programming中询问。


On Sun,2007年11月18日12:33:32 +0000,Mark McIntyre写道:


lovecreatesbea ... @ gmail.com写道:


>以下函数确定两个整数的最大值。它在我的机器上工作。

如果(a [0] - a [1])是负数,那么第一位是什么:(无符号)(a [0]
- [1])?它是0还是1?

#include< limits.h>

int max(int n1,int n2)

int a [2];
a [0] = n1,a [1] = n2;
返回[((无符号)(a [0] - a [1])> sizeof n1 * CHAR_BIT - 1)];
}

感谢您的时间



这适用于二次补码和符号级机器。我怀疑

你会遇到任何其他类型。


它会在减法给出的补充机器上失败

值-0,



-0是正零,当减法生成时,有
没有额外的问题。


当n1和n2

都为零时,只允许减去负零,并且其中一个或两个都是负数。在这种情况下,

unsigned的转换为零,表示返回[0],当n1 == n2时,这是一个

有效选项。即使其他系统可能不同,负零也不小于或大于

正整数零。


它将失败任何其他类型的表示,对于

例如,过剩-N。



这样的表示是不允许的。


如果unsigned int有填充位,代码也可能失败,对于

至少有两个原因。首先,无法保证

UINT_MAX INT_MAX。其次,右移的行为是

undefined。


??? Harald van D?3k写道:
< blockquote class =post_quotes>
-0是正零,当减法生成时,有
没有额外的问题。



你确定吗?我认为-0的bitpattern是全部的。


>并且它将在任何其他类型的表示中失败,因为
例如多余的N.



不允许这样的表示。



符合C编译器。


如果unsigned int有填充位,代码也可能失败,顺便说一句,至少有两个原因。
。首先,无法保证

UINT_MAX INT_MAX。其次,右移的行为是未定义的。



我相信UINT_MAX必须至少等于INT_MAX?


The following function determines the maximum of two integers. It
works on my machine.

If (a[0] - a[1]) is negative, what''s the first bit of: (unsigned)(a[0]
- a[1])? Is it 0 or 1?
#include <limits.h>

int max(int n1, int n2)
{
int a[2];
a[0] = n1, a[1] = n2;
return a[((unsigned)(a[0] - a[1]) >sizeof n1 * CHAR_BIT - 1)];
}
Thank you for your time

解决方案

lovecreatesbea...@gmail.com wrote:

The following function determines the maximum of two integers. It
works on my machine.

If (a[0] - a[1]) is negative, what''s the first bit of: (unsigned)(a[0]
- a[1])? Is it 0 or 1?
#include <limits.h>

int max(int n1, int n2)
{
int a[2];
a[0] = n1, a[1] = n2;
return a[((unsigned)(a[0] - a[1]) >sizeof n1 * CHAR_BIT - 1)];
}
Thank you for your time

This will work for twos complement and sign-magnitude machines. I doubt
you''ll ever encounter any other sort.

It will fail on ones complement machines where the subtraction gave the
value -0, and it will fail on any other sort of representation, for
example excess-N.

However its a very complicated way to compare two ints. If I found it
during code review, I''d force my developer to remove it.

By the way, this isn''t a C question. Ask in comp.programming next time.


On Sun, 18 Nov 2007 12:33:32 +0000, Mark McIntyre wrote:

lovecreatesbea...@gmail.com wrote:

>The following function determines the maximum of two integers. It works
on my machine.

If (a[0] - a[1]) is negative, what''s the first bit of: (unsigned)(a[0]
- a[1])? Is it 0 or 1?
#include <limits.h>

int max(int n1, int n2)
{
int a[2];
a[0] = n1, a[1] = n2;
return a[((unsigned)(a[0] - a[1]) >sizeof n1 * CHAR_BIT - 1)];
}
Thank you for your time


This will work for twos complement and sign-magnitude machines. I doubt
you''ll ever encounter any other sort.

It will fail on ones complement machines where the subtraction gave the
value -0,

-0 is positive zero, and when the subtraction generates that, there are
no extra problems.

The subtraction is only allowed to give negative zero when both n1 and n2
are zero, and one or both of them is negative. In this case, the cast to
unsigned gives plain zero, meaning a[0] would be returned, which is a
valid option when n1 == n2. Negative zero is not smaller or larger than
positive zero for C integers, even though other systems may differ.

and it will fail on any other sort of representation, for
example excess-N.

Such a representation is not allowed.

The code may also fail if unsigned int has padding bits, by the way, for
at least two reasons. Firstly, there is no guarantee that
UINT_MAX INT_MAX. Secondly, the behaviour of the right-shift is
undefined.


???Harald van D?3k wrote:

-0 is positive zero, and when the subtraction generates that, there are
no extra problems.

You sure? The bitpattern of -0 is all-ones I think.

>and it will fail on any other sort of representation, for
example excess-N.


Such a representation is not allowed.

By a conforming C compiler.

The code may also fail if unsigned int has padding bits, by the way, for
at least two reasons. Firstly, there is no guarantee that
UINT_MAX INT_MAX. Secondly, the behaviour of the right-shift is
undefined.

I belive UINT_MAX must be at least equal to INT_MAX however?


这篇关于确定两个整数的最大值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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