M
Mark McIntyre
To Flash and Walter,
Are you clinically thick? You're /still/ top posting. In CLC thats the
height of rudeness. Stop it.
To Flash and Walter,
Dave Hansen said:FWIW, my bit-bang SPI output function looks something like
bit_ctr = 8;
do
{
Set_IO(SPI_DATA, (data&0x80) != 0);
Set_IO(SPI_CLOCK, 1);
data <<= 1;
Set_IO(SPI_CLOCK, 0);
} while (--bit_ctr);
which seems intuitive for the function at hand, and generates nearly
optimal assembly on all the platforms it's used on.
Joe said:What if the difference is between fitting into memory and not?
David said:..."Use
word-size variables instead of chars" (great for PPC, indifferent for
msp430, terrible for AVR),
"Addition is faster than multiplication - use
'val + val + val' instead of 'val * 3' " (wrong for most compiler/target
combinations).
Mark McIntyre said:I slightly agree with your conclusions tho. I'd modify (1) and (2) as
follows
1) Top Post (and totally remove the old message) if you are only
making 1 brief point which has no relation to any of the old message.
And then ask yourself why the hell you're replying to this post with
an irrelevant remark.
Richard Henry said:A little alarm goes off in my brain every time I see an arithmetic variable
(bit_ctr) used as the parameter of a logical expression (while).
The following line better expresses what I think you mean:
while (--bit_ctr != 0);
The compiler optimizer may well deliver the same machine code for both
methods.
And please don't let me get started with the use of NULL pointers in similar
situations.
"Richard Henry said:A little alarm goes off in my brain every time I see an arithmetic variable
(bit_ctr) used as the parameter of a logical expression (while).
The following line better expresses what I think you mean:
while (--bit_ctr != 0);
The compiler optimizer may well deliver the same machine code for both
methods.
And please don't let me get started with the use of NULL pointers in similar
situations.
Keith Thompson said:I tend to agree, in that I try to use only expressions that are
logically Boolean values as conditions, adding an explicit comparison
for anything else. But things like "while (--bit_ctr)", "if (ptr)",
and "if (!ptr)" are common C idioms. You might consider disconnecting
the little alarm in your brain, or at least turning down the volume.
The set of C code that you can easily read should be much wider than
the set of C code that you'd be willing to write.
Christian said:...
Well, if anyone writes code like
then they will get what they deserve!
Hold on there. Is there any guarantee that any amount of -- applicationstoby said:Do you really think
for( p = /*whatever*/ ; p ; --p ){
/*somestuff*/
}
is any better?
Of course, the idea of counting a *pointer* down to NULL is rather
implausible in real code. Maybe you intended an integer variable.
Skarmander said:Hold on there. Is there any guarantee that any amount of -- applications
eventually yield a null pointer? Could this loop forever or does it
summon nasal demons?
toby said:It's not guaranteed by the standard but on all implementations I know
of, it will eventually get there, unless of course, p isn't a multiple
of sizeof(*p) to begin with ;-) -- which can only arise through
naughtiness anyway.
It's not guaranteed by the standard but on all implementations I know
of, it will eventually get there, unless of course, p isn't a multiple
of sizeof(*p) to begin with ;-) -- which can only arise through
naughtiness anyway.
Mark McIntyre said:Are you clinically thick? You're /still/ top posting. In CLC thats the
height of rudeness. Stop it.
Richard Henry said:A little alarm goes off in my brain every time I see an arithmetic
variable
(bit_ctr) used as the parameter of a logical expression (while).
The following line better expresses what I think you mean:
while (--bit_ctr != 0);
And please don't let me get started with the use of NULL pointers in
similar
situations.
There comes a point where one can be *too* explicit. I remember arguing over
a coding standards document with someone who insisted that all boolean tests
should be tested for equality with TRUE or FALSE (previously defined in
their own pet/preferred fashion, of course) - hence:
if ( boolean_flag == TRUE )
where I would have argued that:
if ( boolean_flag )
was safer, since it didn't depend on a specific value of TRUE.
<snip>I pointed out that his argument could be extended to:
if (((( boolean_flag == TRUE ) == TRUE ) == TRUE ) == TRUE ) // etc
which seemed dumb to me. He didn't understand the point.
Steve at fivetrees said:Whoa. As others have observed, --value/value-- as a boolean test is a common
idiom in C - check K&R's version of strcpy for an example.
There comes a point where one can be *too* explicit. I remember arguing over
a coding standards document with someone who insisted that all boolean tests
should be tested for equality with TRUE or FALSE (previously defined in
their own pet/preferred fashion, of course) - hence:
if ( boolean_flag == TRUE )
where I would have argued that:
if ( boolean_flag )
was safer, since it didn't depend on a specific value of TRUE. I pointed out
that his argument could be extended to:
if (((( boolean_flag == TRUE ) == TRUE ) == TRUE ) == TRUE ) // etc
which seemed dumb to me. He didn't understand the point.
That's a whole different situation, and not relevant/applicable to the
above.
<snip>Steve said:Whoa. As others have observed, --value/value-- as a boolean test is a common
idiom in C - check K&R's version of strcpy for an example.
There comes a point where one can be *too* explicit. I remember arguing over
a coding standards document with someone who insisted that all boolean tests
should be tested for equality with TRUE or FALSE (previously defined in
their own pet/preferred fashion, of course) - hence:
if ( boolean_flag == TRUE )
where I would have argued that:
if ( boolean_flag )
was safer, since it didn't depend on a specific value of TRUE.
And sometimes the intermediates go missing entirely.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.