Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > (int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something?

Reply
Thread Tools

(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something?

 
 
Keith Thompson
Guest
Posts: n/a
 
      03-20-2011
puppi <(E-Mail Removed)> writes:
> On Mar 19, 11:14*pm, Keith Thompson <(E-Mail Removed)> wrote:

[...]
>> What you say is true for many (most) implementations, but it's not
>> at all guaranteed by the language.
>>
>> Integer types can have padding bits and/or trap representations.
>> signed and unsigned int, for example, might not necessarily be
>> able to represent the same number of values; (signed) int might
>> have two different representations for zero.

>
> You're correct, of course. I was assuming that signed integers were
> represented in two complement's form.


You were also (implicitly) assuming that the most negative
representable value isn't treated as a trap representation, and
that unsigned-to-signed conversion for values exceeding INT_MAX
works as expected (it's implementation-defined).

> Is there any (majorm) current
> implementation that disagrees with that?


I don't believe there are any conforming *hosted* implementations
that don't meet those assumptions. There tends to be more variation
among freestanding implementations, but I don't know the details.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      03-20-2011
Ben Bacarisse <(E-Mail Removed)> wrote:
> pozz <(E-Mail Removed)> writes:

<snip>
> > unsigned int x = <value>;
> > unsigned int y = (unsigned)((int)x);

>
> Neither case is safe. *Both can involve an out-of-range
> conversion from unsigned to int.


More subtle is the case...

unsigned short x = USHRT_MAX;
unsigned short y = ++x;

The second initialisor expression can overflow if USHRT_MAX <=
INT_MAX. Since ++x is just shorthand for x = x + 1, the sub-
expression x + 1 can overflow if x is promoted to int and not
unsigned int. That can be true even if USHRT_MAX == UINT_MAX in
the case where UINT_MAX == INT_MAX.

--
Peter
 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      03-20-2011
Peter Nilsson <(E-Mail Removed)> writes:

> Ben Bacarisse <(E-Mail Removed)> wrote:
>> pozz <(E-Mail Removed)> writes:

> <snip>
>> > unsigned int x = <value>;
>> > unsigned int y = (unsigned)((int)x);

>>
>> Neither case is safe. Both can involve an out-of-range
>> conversion from unsigned to int.

>
> More subtle is the case...
>
> unsigned short x = USHRT_MAX;
> unsigned short y = ++x;
>
> The second initialisor expression can overflow if USHRT_MAX <=
> INT_MAX. [snip elaboration]


Presumably you mean ++x can overflow if USHRT_MAX == INT_MAX.
There is no overflow if USHRT_MAX < INT_MAX.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loose Internet with WEP or WPA =?Utf-8?B?TmF2aWxl?= Wireless Networking 4 06-15-2005 11:26 AM
loose internet =?Utf-8?B?TmVpbA==?= Wireless Networking 2 01-28-2005 02:31 PM
OT: The lusers are loose LnkWizard MCSE 35 01-18-2005 10:02 PM
Loose encrypted connection after re-boot joekohn@att.net Wireless Networking 0 12-26-2004 03:51 AM
If i am changing aspx file will i loose session? George Ter-Saakov ASP .Net 2 08-15-2003 12:23 AM



Advertisments