<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p><br>
</p>
<div class="moz-cite-prefix">On 14/07/2020 07:48,
<a class="moz-txt-link-abbreviated" href="mailto:david.haworth@elektrobit.com">david.haworth@elektrobit.com</a> wrote:
</div>
<blockquote type="cite"
cite="mid:20200714064811.GB3934@elektrobit.com">
<pre class="moz-quote-pre" wrap="">
In the first program, any verifier worth its salt would complain at least
once (quite aside from the use of stdio), because bitwise operations
have (unspecified) implementation defined and undefined aspects (C99)
and char might be either signed or unsigned.</pre>
</blockquote>
<p>Indeed. The QAC static checker moans prolifically about this sort
of thing. But it is, in fact, a very controlled use of unspecified
behaviour (or it would be if I'd got it right - see below). The
C99 definitions of the bitwise operators actually say what the
effect on the bit patterns is. What is unspecified is what the
corresponding resulting value is in the case of signed operands.
The use of bitwise operators in the first example will produce all
ones in ch (though whether this is stored as all-ones is
unspecified - it may be stored as all zeros in 1C representation).</p>
<p><font size="+3">BIG CONFESSION:</font> <font size="+3">I SCREWED
THIS ONE UP ...AARGH !!!</font> .... Derek Jones will, *quite
rightly*, never let me forget it ... :-)).</p>
<p>What I posted was an out-of-context fragment of a larger program
that subsequently checks the different possible values to
discriminate between 1C, 2C, and Sm representations. In a 1 C
representation , this will not tell you whether ch is signed or
unsigned. In fact, I should have used an alternating patterns of
0s, and 1s instead of setting things all to 1. I'll post a correct
version later on.<br>
</p>
<p>But the *principle* is the same. You put a controlled bit-pattern
with a MSB of 1 into a plain char and then check whether it is
negative. This kind of controlled use of unspecified aspects of
program behaviour is typically used by compiler testers to
determine by testing what the implementation-defined and
unspecified aspects of a C implementation actually are.
</p>
<blockquote type="cite"
cite="mid:20200714064811.GB3934@elektrobit.com">
<pre class="moz-quote-pre" wrap="">
MISRA (for all its warts and blemishes) ...</pre>
</blockquote>
<p>For which I beseech the Gods to forgive me.</p>
<p><snip><br>
</p>
<blockquote type="cite"
cite="mid:20200714064811.GB3934@elektrobit.com">
<pre class="moz-quote-pre" wrap="">
If I were a competent compiler author, I'd be tempted to write
such a compiler that produces random results every time a construct with
undefined or implementation-defined is used. Any takers? :-)</pre>
</blockquote>
<p>You're certainly not the first to have mulled that one over. :-))</p>
<p><br>
</p>
<p>Olwen</p>
<p><br>
</p>
<p><br>
</p>
</body>
</html>