Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > unsigned int with signed long int (same width) - how is conversion done?

Reply
Thread Tools

unsigned int with signed long int (same width) - how is conversion done?

 
 
G Fernandes
Guest
Posts: n/a
 
      02-16-2005
Hello,

I have a question about using an unsigned int and a signed long int in
an expression (where UINT_MAX > LINT_MAX).

Based on the conversion rules, the unsigned int will be convered to
long int, but what if it can't fit? Is this undefined behaviour?

Any help will be appreciated.

[I guess the same thing can happen with unsigned long and signed long
long]

 
Reply With Quote
 
 
 
 
Christian Kandeler
Guest
Posts: n/a
 
      02-16-2005
G Fernandes wrote:

> I have a question about using an unsigned int and a signed long int in
> an expression (where UINT_MAX > LINT_MAX).
>
> Based on the conversion rules, the unsigned int will be convered to
> long int


No, it won't. If signed long can't hold all possible values for unsigned
int, both operands are converted to unsigned long. This is part of the
"usual arithmetic conversions", which are explained in great detail in the
standard.


Christian
 
Reply With Quote
 
 
 
 
Lawrence Kirby
Guest
Posts: n/a
 
      02-16-2005
On Tue, 15 Feb 2005 21:25:25 -0800, G Fernandes wrote:

> Hello,
>
> I have a question about using an unsigned int and a signed long int in
> an expression (where UINT_MAX > LINT_MAX).
>
> Based on the conversion rules, the unsigned int will be convered to
> long int, but what if it can't fit? Is this undefined behaviour?


In that case both are converted to unsigned long.

> Any help will be appreciated.
>
> [I guess the same thing can happen with unsigned long and signed long
> long]


That's the normal case where the signed value is converted to the
unsigned type.

Any integer value can be converted to an unsigned type with a well defined
result. That may not preserve the value, and obviously won't if the
initial value is negative. When the value of an integer conversion can't
be represented in the new type the standard says:

"Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or
subtracting one more than the maximum value that can be represented in
the new type until the value is in the range of the new type."

So, for example, it follows that if you convert the value -1 to any
unsigned type the result is the maximum value that the unsigned type can
represent.

Lawrence





Lawrence

 
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
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
what does this double conversion mean (unsigned long)(unsigned int) CFAN C Programming 6 04-04-2007 01:24 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments