public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* integral type conversions
@ 2006-03-24  3:00 Perry Smith
  2006-03-24 12:54 ` John Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Perry Smith @ 2006-03-24  3:00 UTC (permalink / raw)
  To: gcc-help

I can't understand the C++ spec.  It seems more vague than the old K&R.

If the source is a signed integral type and the destination is an  
unsigned integral type that is larger, are the additional bits done  
via sign extension or zero extension?

e.g.

int i;

unsigned long l = i;

Is i sign extended from int to long and then converted to unsigned  
long or is it converted to unsigned int and then zero extended to  
unsigned long?

(assume that long is bigger in this case).  I'm actually going from a  
long to an unsigned long long but I assume the same algorithm is used.

I guess, to be safe, I could do:

unsigned long l = static_cast<unsigned int>(i);

Thank you,
Perry

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: integral type conversions
  2006-03-24  3:00 integral type conversions Perry Smith
@ 2006-03-24 12:54 ` John Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2006-03-24 12:54 UTC (permalink / raw)
  To: Perry Smith, MSX to GCC

Hi Perry,

> If the source is a signed integral type and the destination is an
> unsigned integral type that is larger, are the additional bits done
> via sign extension or zero extension?

On 2's complement machines (the only sort I've ever used), the additional
bits are done via sign extension.

This behavior should be identical to the behavior in C.

Side note: I use the Java convention of converting a char to an int:

char c = '\xAA';
int i;
i = c & 0xFF;

Works the reliably on plain-char-is-signed and plain-char-is-unsigned
machines.  Presuming the architecture is 8-bit-bytes (such as all my
platforms are).  And I'm not sure if there's caveats for 1's complement
machines to worry about.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-03-24 12:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-24  3:00 integral type conversions Perry Smith
2006-03-24 12:54 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).