Re: [BLACKBOX] Seasons Greetings from Linuxland

From: [at]} <Chris>
Date: Thu, 30 Dec 2010 09:51:54 +1030

----boundary-LibPST-iamunique-2076320106_-_-
Content-type: text/plain

>From: BlackBox [mailto:BLACKBOX{([at]})nowhere.xy
>Sent: Wednesday, 29 December 2010 9:10 PM
>To: BLACKBOX{([at]})nowhere.xy
>Subject: [BLACKBOX] Seasons Greetings from Linuxland
....
....
>
>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.
>

No (irrespective of what the writer intended) - neither of those expansions
is equivalent to the original statement.

The problem is that he has used *four* separate instances of the ++ operator
in the one statement. He mistakenly assumed that the C compiler would
evaluate the expression from left to right. Suppose that the initial value
of bitNr was 0. What he wanted was the equivalent of:

  digdata[digNr] = (bitdata[0] * 8) + (bitdata[1] * 4) + (bitdata[2] * 2) +
bitdata[3];

Instead the compiler he used evaluated the expression from right to left and
he got the equivalent of:

  digdata[digNr] = (bitdata[3] * 8) + (bitdata[2] * 4) + (bitdata[1] * 2) +
bitdata[0];

What scares me is that if he had got lucky and chosen another compiler (or
maybe even a different optimisation setting?) he might have got the answer
he wanted - but there's no guarantee. Both compilers would have been
conforming to the rules of the language.

-----------------------

One reliable way to write what he wanted is:

  digdata[digNr] = (bitdata[bitNr] * 8) + (bitdata[bitNr+1] * 4) +
(bitdata[bitNr+2] * 2) + (bitdata[bitNr+3]);
  bitNr += 4;


Regards,
Chris Burrows

CFB Software
http://www.cfbsoftware.com


----
To unsubscribe, send a message with body "SIGNOFF BLACKBOX" to LISTSERV{([at]})nowhere.xy----boundary-LibPST-iamunique-2076320106_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"
e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGRlZmYwe1xmb250dGJsDQp7XGYwXGZz
d2lzcyBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4gQ291cmllciBOZXc7fQ0Ke1xmMlxmbmlsXGZjaGFy
c2V0MiBTeW1ib2w7fQ0Ke1xmM1xmbW9kZXJuXGZjaGFyc2V0MCBDb3VyaWVyIE5ldzt9fQ0Ke1xj
b2xvcnRibFxyZWQwXGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBcYmx1ZTI1NTt9DQpcdWMxXHBh
cmRccGxhaW5cZGVmdGFiMzYwIFxmMFxmczIwID5Gcm9tOiBCbGFja0JveCBbbWFpbHRvOkJMQUNL
Qk9YQExJU1RTLk9CRVJPTi5DSF0gT24gQmVoYWxmIE9mIExlcyBNYXlccGFyDQo+U2VudDogV2Vk
bmVzZGF5LCAyOSBEZWNlbWJlciAyMDEwIDk6MTAgUE1ccGFyDQo+VG86IEJMQUNLQk9YQExJU1RT
Lk9CRVJPTi5DSFxwYXINCj5TdWJqZWN0OiBbQkxBQ0tCT1hdIFNlYXNvbnMgR3JlZXRpbmdzIGZy
b20gTGludXhsYW5kXHBhcg0KLi4uLlxwYXINCi4uLi5ccGFyDQo+XHBhcg0KPkV2ZW4gaWYgd2Ug
aWdub3JlIHRoZSB2YWdhcmllcyBvZiB0aGUgQyBjb21waWxlciB0aGVuIG5laXRoZXIgXHBhcg0K
PnRoZSBpbnRlbnRpb24gb2YgdGhlIHByb2dyYW1tZXIgbm9yIHRoZSBvdXRjb21lIG9mIHRoZSBl
eHByZXNzaW9uXHBhcg0KPlxwYXINCj5kaWdkYXRhW2RpZ05yXS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ydWxlcyBvZiB0aGUgbGFuZ3VhZ2UuXHBhcg0KXHBhcg0K
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1ccGFyDQpccGFyDQpPbmUgcmVsaWFibGUgd2F5IHRvIHdy
aXRlIHdoYXQgaGUgd2FudGVkIGlzOlxwYXINClxwYXINCiAgZGlnZGF0YVtkaWdOcl0gPSAoYml0
ZGF0YVtiaXROcl0gKiA4KSArIChiaXRkYXRhW2JpdE5yKzFdICogNCkgK1xwYXINCihiaXRkYXRh
W2JpdE5yKzJdICogMikgKyAoYml0ZGF0YVtiaXROciszXSk7XHBhcg0KICBiaXROciArPSA0O1xw
YXINClxwYXINClxwYXINClJlZ2FyZHMsXHBhcg0KQ2hyaXMgQnVycm93c1xwYXINClxwYXINCkNG
QiBTb2Z0d2FyZVxwYXINCmh0dHA6Ly93d3cuY2Zic29mdHdhcmUuY29tXHBhcg0KXHBhcg0KXHBh
cg0KLS0tLVxwYXINClRvIHVuc3Vic2NyaWJlLCBzZW5kIGEgbWVzc2FnZSB3aXRoIGJvZHkgIlNJ
R05PRkYgQkxBQ0tCT1giIHRvIExJU1RTRVJWQExJU1RTLk9CRVJPTi5DSFxwYXINCn0=
----boundary-LibPST-iamunique-2076320106_-_---
Received on Thu Dec 30 2010 - 00:21:54 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:30:20 UTC