Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

typecast reminder

3 views
Skip to first unread message

David

unread,
May 12, 2011, 8:17:51 PM5/12/11
to
Every once in a while my brain goes dead. in type casting rules, presuming
int is 32 bits, would this always end up 0xFFFFFFFFFFFFFFFF or could it end
up 0x00000000FFFFFFFF:

unsigned __int64 var=(unsigned __int64) -1;

TIA!!

Hans-Bernhard Bröker

unread,
May 13, 2011, 7:51:09 AM5/13/11
to

Assuming __int64 is what it claims, the former. Unsigned arithmetic
works modulo its own cardinality. To get 2^32-1, it would take a
two-step cast:

(unsigned __int64)(unsigned int) -1;

E. S. Fabian

unread,
May 13, 2011, 8:54:36 AM5/13/11
to
Hans-Bernhard Br�ker:

Just as a matter of curiosity, how would these work on a one's complement
machine, or on a sign-and-magnitude machine?
--
Steve


Hans-Bernhard Bröker

unread,
May 13, 2011, 4:42:56 PM5/13/11
to
On 13.05.2011 14:54, E. S. Fabian wrote:
> Hans-Bernhard Bröker:

The same. It doesn't really matter what the representation of signed
numbers is --- the result is defined in terms of values, not representation.

Mark Wezelenburg

unread,
May 13, 2011, 6:45:22 PM5/13/11
to
Hi,

>> --- the result is defined in terms of values, not representation

>> Just as a matter of curiosity, how would these work on a one's complement

For one's complement, the cardinality is 2^N - 1 (rather than 2^N, +/- 0)
so (unsigned)-1 = 2^N-2 (0xfffe) rather than 2^N-1 (0xffff)

Mark


"Hans-Bernhard Bröker" <bro...@physik.rwth-aachen.de> wrote in message
news:iqk543$hgm$1...@www.openwatcom.org...

Hans-Bernhard Bröker

unread,
May 13, 2011, 8:09:58 PM5/13/11
to
On 14.05.2011 00:45, Mark Wezelenburg wrote:
> Hi,
>
>>> --- the result is defined in terms of values, not representation
>>> Just as a matter of curiosity, how would these work on a one's complement
>
> For one's complement, the cardinality is 2^N - 1 (rather than 2^N, +/- 0)

But that only applies to the -1 part of the cast expression. It has
_no_ influence whatsoever on the result of that cast, since that is
performed following the rules for _unsinged_ arithmetic. Signed integer
representation doesn't even enter the picture.

> so (unsigned)-1 = 2^N-2 (0xfffe) rather than 2^N-1 (0xffff)

No.

0 new messages