(无符号)-INT_MIN [英] (unsigned)-INT_MIN

查看:73
本文介绍了(无符号)-INT_MIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


int i = INT_MIN;

unsigned int u = -i;


是你保证有INT_MIN的绝对值吗?


为什么它可能没有:-i有类型(int),-INT_MIN可能超过

INT_MAX。


演员表也可能不起作用,因为在(unsigned)-i中;演员是太晚了

迟到,而且 - (无符号)我然后我不能被表示为无符号。


如果长于int,我可以做 - (长)我,但如果我们正在谈论

关于可用的最长整数类型怎么办?


TIA

viza

解决方案

7月21日上午11:53,viza< tom.v ... @ gm-il.com.obviouschange.invalid>

写道:


大家好


int i = INT_MIN;

unsigned int u = -i;


你保证绝对值是INT_MIN吗?


为什么它不会: -i具有type(int),-INT_MIN可能大于

INT_MAX。



那又怎样?相应的整数类型具有相同的值范围;如果

int有N个值,unsigned int有N个值。

保证。

< snip>


7月21日,1:53 * pm,viza< tom.v ... @ gm-il.com.obviouschange.invalid>

写道:


大家好


int i = INT_MIN;

unsigned int u = -i;


你保证有INT_MIN的绝对值吗?



在我的系统上,u变为0,因为INT_MIN是-2147483648而INT_MAX是

2147483647.因此,-INT_MIN回绕到0。


为什么不这样:-i有类型(int),-INT_MIN可能超过

INT_MAX。在大多数系统上,



-INT_MIN大于INT_MAX。


如果长比int更长,我能做 - (长)我,但如果我们正在谈论

关于可用的最长整数类型呢?



我们在看什么?它是关于在unsigned int中获取

signed int的绝对值吗?


7月21日下午12:21,vipps。 .. @ gmail.com写道:


7月21日上午11:53,viza< tom.v ... @ gm-il.com.obviouschange .invalid>

写道:大家好


int i = INT_MIN;

unsigned int u = -i ;


您是否保证绝对值为INT_MIN?


为什么它可能不会:-i具有type(int),-INT_MIN可能大于

INT_MAX。



那又怎样?相应的整数类型具有相同的值范围;如果

int有N个值,unsigned int有N个值。

保证。

< snip>



对不起,我误解了你。你对

INT_MIN< -INT_MAX。

那么,在这种情况下,计算-INT_MIN会调用未定义的行为;

(溢出)

试试这个


if(i == INT_MIN)

u + = INT_MAX +(无符号) - (i + INT_MAX);

u = -i;


Hi all

int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?

Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.

A cast might not work either, because in (unsigned)-i; the cast is too
late, and in -(unsigned)i then i cannot be represented as unsigned.

If long is longer than int, I can do -(long)i, but what if we are talking
about the longest integer type available?

TIA
viza

解决方案

On Jul 21, 11:53 am, viza <tom.v...@gm-il.com.obviouschange.invalid>
wrote:

Hi all

int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?

Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.

So what? corresponding integer types have the same range of values; If
int has N values, unsigned int has N values.
It is guaranteed.
<snip>


On Jul 21, 1:53*pm, viza <tom.v...@gm-il.com.obviouschange.invalid>
wrote:

Hi all

int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?

On my system, u becomes 0 as INT_MIN is -2147483648 and INT_MAX is
2147483647. So, -INT_MIN wraps around to 0.

Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.

-INT_MIN is greater than INT_MAX on most of the systems.

If long is longer than int, I can do -(long)i, but what if we are talking
about the longest integer type available?

What are we looking at? Is it about getting the absolute value of a
signed int in an unsigned int?


On Jul 21, 12:21 pm, vipps...@gmail.com wrote:

On Jul 21, 11:53 am, viza <tom.v...@gm-il.com.obviouschange.invalid>
wrote:Hi all

int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?

Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.


So what? corresponding integer types have the same range of values; If
int has N values, unsigned int has N values.
It is guaranteed.
<snip>


I''m sorry, I misunderstood you. You''re interested in the case that
INT_MIN < -INT_MAX.
Well, in that case, computing -INT_MIN invokes undefined behavior;
(overflow)
Try this

if(i == INT_MIN)
u += INT_MAX + (unsigned)-(i + INT_MAX);
u = -i;


这篇关于(无符号)-INT_MIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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