Hi,
I think the intension was
digdata[digNr] = (bitdata[bitNr] * 8) + (bitdata[bitNr+1] * 4) +
(bitdata[bitNr+2] * 2) + (bitdata[bitNr+3]);
bitNr = bitNr+4;
Though I didn't say that this is defined in C..
Regards,
Jozsef
On 29 December 2010 11:39, Les May <zen53397{([at]})nowhere.xy
Les May wrote
// z = x++ + ++y;
++y;
z = x + y;
x++;
But which forms best express what the program writer intended?
------------------------------------------------------------------------------------------------
Chris Burrows wrote
The original point made by Wirth was related to the problem of parsing:
x+++++y
It was your interpretation that it must have been intended to be:
z = x++ + ++y;
-----------------------------------------------------------------------------------------
I think that this rather reinforces the point I was trying to make. Wirth suggested that x+++++y is a riddle. More complex expressions make it difficult to understand what the program writer intended that the code should do. Parsing problems are not confined to mechanical parsers.
Even if we ignore the vagaries of the C compiler then neither the intention of the programmer nor the outcome of the expression
digdata[digNr] = (bitdata[bitNr++] * 8) + (bitdata[bitNr++] * 4) +
(bitdata[bitNr++] * 2) + (bitdata[bitNr++]);
is immediately obvious.
It could be made obvious by expanding it to
bitNr++;
digdata[digNr] = (bitdata[bitNr] * 8) + (bitdata[bitNr] * 4) +
(bitdata[bitNr] * 2) + (bitdata[bitNr]);
or to
digdata[digNr] = (bitdata[bitNr] * 8) + (bitdata[bitNr] * 4) +
(bitdata[bitNr] * 2) + (bitdata[bitNr]);
bitNr++;
depending on what the writer intended.
It also follows the convention that text is to be read from left to right and from top to bottom. This means that there are fewer intermediate results (e.g. bitNr++)
to be stored on the reader's 'mental stack' which I believe aids understanding. The line
RETURN (ch <= '9') & (ch >= '0');
does not because once the comparisons and boolean have been mentally evaluated what to do with the result is at the beginning of the line.
I don't think this is a problem confined to C. I believe it is a applicable to all languages. Programs give the wrong 'answer' when there is a difference between what the programmer intended the code the to do and what it actually does. This suggests that it is a good idea to make your intentions explicit when writing programs especially if you might have to maintain them.
Les May
---- To unsubscribe, send a message with body "SIGNOFF BLACKBOX" to LISTSERV{([at]})nowhere.xy
---- To unsubscribe, send a message with body "SIGNOFF BLACKBOX" to LISTSERV{([at]})nowhere.xy
Received on Thu Dec 30 2010 - 14:20:00 UTC