A bit of confusion with conversions

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: ANN (retry): C compliler, new download available
    ... The context between them is irrelevant to my statements. ... architecture or is compiled by a "normal" compiler, ... creating portable code for some architecture you can't test on. ...
    (alt.lang.asm)
  • Re: Adding Older Versions of GCC To The Tool Chain ... safely?!?!
    ... doing optimizing? ... The original "vulnerability" report, not just the Heise article, uses ... There is by definition no such thing as 'an unsigned overflow', ... and a C compiler is still free to do whatever it chooses. ...
    (comp.os.linux.development.apps)
  • Re: extended operators
    ... between the sequence point X and the last sequence point. ... Perhaps it should be a macro, defined in a new header, that expands to some implementation-defined expression (possibly involving compiler magic). ... So you'd mandate specific restrictions on generated code in the presence of calls to _carry, even if the compiler is able to prove that it can perform an optimization without changing the effect? ... The ``result'' member is set to the sum of x and y; unlike an ordinary "+" operator, the result is well defined even on overflow. ...
    (comp.std.c)
  • Re: operators similar to functions?
    ... undefined behaviour; however the compiler cannot know that. ... probably has a pretty good idea of the consequences of overflow ... The HAL StinkWad represents an opposite pole in system design. ...
    (comp.lang.c)
  • Re: Passing string parameter to funtion
    ... You didn't quote any context from the previous articles in this ... Copy-and-paste the exact source file that you fed to the compiler; ... process_word function behave like printf is going to be useful. ...
    (comp.lang.c)