 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jiøí Zárevúcky Guest
|
Posted: Fri May 04, 2012 10:30 pm Post subject: Re: portable code |
|
|
On Monday, 23 April 2012 15:31:46 UTC+2, Dag-Erling Smørgrav wrote:
| Quote: | then the following:
unsigned int i = 0x01020304;
write((i >> 24) & 0xFF);
write((i >> 16) & 0xFF);
write((i >> & 0xFF);
write(i & 0xFF);
rewind(f);
i = 0;
i += (read() >> 24);
i += (read() >> 16);
i += (read() >> ;
i += (read());
printf("i = 0x%x\n", i);
will print
i = 0x4
|
The snippet has wrong direction of the bit shifts for reads.
Correct would be:
i += (read() << 24);
i += (read() << 16);
i += (read() << ;
i += (read());
I suspect it's a simple copy-paste error of the original author.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Jorgen Grahn Guest
|
Posted: Wed May 09, 2012 6:06 am Post subject: Re: portable code |
|
|
On Fri, 2012-05-04, Dag-Erling Smørgrav wrote:
| Quote: | Jorgen Grahn <grahn+nntp (AT) snipabacken (DOT) se> writes:
Dag-Erling Smørgrav <des (AT) des (DOT) no> writes:
There is no write() in C, but there is one in POSIX that doesn't match
this usage:
I am pretty sure rangsynth just used "write" as a generic "write an
octet to somewhere" function. The details are irrelevant here.
The details are not irrelevant. When you post code that operates on the
value returned by a function, the semantics of that function and the
range and representation of its return value are highly relevant.
|
I agree that the types are important in this case, e.g. that you
shift and mask unsigned values.
| Quote: | then the following:
unsigned int i = 0x01020304;
write((i >> 24) & 0xFF);
write((i >> 16) & 0xFF);
write((i >> & 0xFF);
write(i & 0xFF);
rewind(f);
i = 0;
i += (read() >> 24);
i += (read() >> 16);
i += (read() >> ;
i += (read());
printf("i = 0x%x\n", i);
will print
i = 0x4
If it does, it's because you introduced a bug somewhere. Why? I feel
I'm missing some point here; can you please explain it more clearly?
I didn't introduce a bug anywhere. On the contrary - I was pointing out
a bug in rangsynth's code. I'm sure you'll see it too if you look
closely.
|
There are less indirect ways of saying "there's a bug in that code" ...
Combined with your advice to print the number as text instead
(snipped), I didn't understand what point you were trying to make.
For a while it seemed you were saying "this is too hard to implement,
so you should switch to another output format". I tend to dislike
binary data formats too. They have many problems, but this (portably
reading/writing unsigned integers) isn't one of them.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|