A bit of confusion with conversions
- From: beginwithl@xxxxxxxxx
- Date: Tue, 6 Jan 2009 16:13:06 -0800 (PST)
Hi
Sorry for making three threads at the same time, but the number of
questions has quickly build up and … next time I will do my best to
post just one thread at a time
1) I don’t quite understand when does the explicit conversion from
smaller source type to longer target type T causes sign-extend and
when does it cause zero-extend to the length of T? I assume when
explicitly converting between types where implicit cast would be
sufficient, sign/zero-extend is correctly chosen. But what about in
other cases, where implicit cast would not be possible:
byte b = 128;
ushort sh = ( ushort ) b; // sh now has a value of 128;
sbyte sb= -128;
sh = ( ushort ) sb; // sh now has a value of 65408;
-128 and 128 have the same binary representation ( 1000 0000 ), but
why did compiler decide to sign-extend sb ( instead of zero extend ) -
in either case the result would be wrong?! What rules did compiler
follow?
BTW – I know that if source type in larger than target type, that the
extra most significant bits are discarded. My question is only for
when the situation is reverse
2)
a) If by default the code in a program has overflow checking context
unchecked, why does compiler then report an error:
int i = 100;
byte b =i;
Isn’t the above assignment under the supervision/jurisdiction of an
overflow checking context ( aka under the control of checked/unchecked
statements )?
* Similary, isn’t the following considered an overflow and thus should
also be under the control of overflow checking context:
sbyte b = 200; // this should produce an exception, not a compile time
error
b) “In a checked context, if an expression produces a value that is
outside the range of the destination type, the result depends on
whether the expression is constant or non-constant. Constant
expressions cause compile time errors, while non-constant expressions
are evaluated at run time and raise exceptions.”
What is considered a constant expression? When only literal values are
involved in an expression?
c) Why can’t the expression inside checked operator also be a method?
thank you
.
- Prev by Date: Why does a client code need to be recompiled also?
- Next by Date: RE: Class Library Configuration Files
- Previous by thread: Why does a client code need to be recompiled also?
- Next by thread: RE: Class Library Configuration Files
- Index(es):
Relevant Pages
|