473,881 Members | 1,653 Online

# (unsigned)-INT_MIN

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
Jul 21 '08 #1
30 4939
On Jul 21, 11:53 am, viza <tom.v...@gm-il.com.obviousc hange.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>
Jul 21 '08 #2
On Jul 21, 1:53*pm, viza <tom.v...@gm-il.com.obviousc hange.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?
Jul 21 '08 #3
On Jul 21, 12:21 pm, vipps...@gmail. com wrote:
On Jul 21, 11:53 am, viza <tom.v...@gm-il.com.obviousc hange.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;
Jul 21 '08 #4
On Jul 21, 8:53 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
wrote:
int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?
No. On a normal (2's complement) system, the
code causes undefined behaviour because the
operation "-i" causes an integer overflow.

Not sure what 'vipps' was on about..
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.
Signed values can be converted to unsigned ;
(unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
which is INT_MAX on the normal machine.
Jul 21 '08 #5
On Jul 21, 12:38 pm, Old Wolf <oldw...@inspir e.net.nzwrote:
On Jul 21, 8:53 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
wrote:
int i= INT_MIN;
unsigned int u= -i;
Is u guaranteed to have the absolute value of INT_MIN?

No. On a normal (2's complement) system, the
code causes undefined behaviour because the
operation "-i" causes an integer overflow.

Not sure what 'vipps' was on about..
Which one of my two (without counting this one) messages are you
refering to?
I posted a follow-up quoting my first message saying I misunderstood,
and then I gave an answer which I believe to be correct. (and
relevant)
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.

Signed values can be converted to unsigned ;
(unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
which is INT_MAX on the normal machine.
Wrong.
Assuming i is -N with N >= 0, (unsigned)i is UINT_MAX - N + 1.
So (unsigned)INT_M IN would be UINT_MAX + INT_MIN + 1.
Jul 21 '08 #6
On Jul 21, 12:36 pm, vipps...@gmail. com wrote:
<snip>
Try this

if(i == INT_MIN)
u += INT_MAX + (unsigned)-(i + INT_MAX);
u = -i;
^
insert 'else' there... :-( I should be looking twice at my messages
before posting them.
Jul 21 '08 #7
Hi

On Mon, 21 Jul 2008 08:53:51 +0000, viza wrote:
int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?
If long is longer than int, I can do -(long)i, but what if we are
talking about the longest integer type available?
Thanks for the answers so far, but none of them help. Perhaps I can
explain it a bit better:

What I would like is a constant expression of unsigned type for the
absolute value of an constant negative expression of the corresponding
signed type.

Ie:

1) I know i is negative
2) I want (unsigned)abs(i )

The answer simply: ( (unsigned)-i ) for any i except INT_MIN. What is
the answer for INT_MIN? or for any i?

Thanks,

viza
Jul 21 '08 #8
Hi

On Mon, 21 Jul 2008 10:02:32 +0000, viza wrote:
What I would like is a constant expression of unsigned type for the
absolute value of an constant negative expression of the corresponding
signed type.
Thanks for the answers so far, but none of them help
I could use a conditional operation based on vippstars second post, but
is there a neater way?

viza
Jul 21 '08 #9
On Jul 21, 9:51 pm, vipps...@gmail. com wrote:
On Jul 21, 12:38 pm, Old Wolf <oldw...@inspir e.net.nzwrote:
Not sure what 'vipps' was on about..

Which one of my two (without counting this one) messages are you
refering to?
(and I posted before you posted your correction).
Signed values can be converted to unsigned ;
(unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
which is INT_MAX on the normal machine.

Wrong.
Assuming i is -N with N >= 0, (unsigned)i is UINT_MAX - N + 1.
So (unsigned)INT_M IN would be UINT_MAX + INT_MIN + 1.
Knew I should have checked that more before
posting .. the baby was crying though :)
Jul 21 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.