Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Mixed signed/unsigned

Reply
Thread Tools

Mixed signed/unsigned

 
 
BartC
Guest
Posts: n/a
 
      04-05-2012

I understand that when C does arithmetic on mixed signed/unsigned operands,
it will perform an unsigned operation (converting the signed value as best
it can to unsigned). That is, after mixed width operands have been taken
care of.

Is this actually correct, and if so what is the rationale behind that?

(I'm in the middle of a compiler for a similar language, and have to choose
between doing the same as C, using signed arithmetic instead (as this could
be more useful for typical values that would be encountered), or possibly
reporting an error).)

--
Bartc

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      04-05-2012
On 04/05/2012 04:54 PM, BartC wrote:
>
> I understand that when C does arithmetic on mixed signed/unsigned operands,
> it will perform an unsigned operation (converting the signed value as best
> it can to unsigned). That is, after mixed width operands have been taken
> care of.
>
> Is this actually correct, and if so what is the rationale behind that?


What it does is a little more complicated than that, and varies
depending upon the operation being performed. For full details, see
section 6.5 of the standard. In many cases, integer promotions
(6.3.1.1p2) are performed - search for any word starting with "promot".
In most cases the "usual arithmetic conversions" (6.1.8p1) are
performed: if both operands have integer type, that includes the integer
promotions as a first step.

Mixed signed/unsigned operations are not always performed as unsigned
operations:

The integer promotions will convert an unsigned operand to 'int', rather
than 'unsigned int', if it is of lower integer conversion rank than
'int' and all values of that type can be represented in 'int'.

If the promoted type of one operand is unsigned, and has an integer
conversion rank lower than the promoted type of the other operand, which
happens to be signed, and if all values of unsigned type can be
represented in the signed type, then the usual arithmetic conversions
require that the operation be performed using signed arithmetic.

Note: the expression a + b can have a type different from the promoted
type of either a or b. For instance, adding a float complex and a long
double will result in a value of long double complex type. Adding an
unsigned int and a signed long long will result in an unsigned long long.

> (I'm in the middle of a compiler for a similar language, and have to choose
> between doing the same as C, using signed arithmetic instead (as this could
> be more useful for typical values that would be encountered), or possibly
> reporting an error).)

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      04-05-2012
On 04/05/2012 05:28 PM, James Kuyper wrote:
....
> If the promoted type of one operand is unsigned, and has an integer
> conversion rank lower than the promoted type of the other operand, which
> happens to be signed, and if all values of unsigned type can be
> represented in the signed type, then the usual arithmetic conversions
> require that the operation be performed using signed arithmetic.
>
> ... Adding an
> unsigned int and a signed long long will result in an unsigned long long.


Correction: it "may result" in unsigned long long, but only if UINT_MAX
> LLONG_MAX.


That's permitted by the C standard, though rather unlikely. A much more
plausible case would be unsigned int and signed long, which will result
in unsigned long if UINT_MAX > LONG_MAX, or unsigned long and signed
long long, which will result in unsigned long long if ULONG_MAX > LLONG_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
Mixed language programming in Visual Basic.NET Robert Liles ASP .Net 2 12-03-2009 05:47 AM
VS Authentication (mixed mode) tripwater ASP .Net 3 04-10-2005 02:51 AM
Mixed network issues =?Utf-8?B?UmFnbGU=?= Wireless Networking 6 02-07-2005 07:27 AM
VHDL verilog mixed design, strange problem Akshaye VHDL 1 02-09-2004 11:57 AM
edif and vhdl files mixed Frank VHDL 6 10-23-2003 07:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57