[sdiy] Quick C query

cheater cheater cheater00 at gmail.com
Sun Nov 21 18:45:31 CET 2010

On Sun, Nov 21, 2010 at 17:41, Steff <steff at steff.name> wrote:
> On 21 November 2010 13:31, Tom Wiltshire <tom at electricdruid.net> wrote:
>> Hi there,
>> Not directly synth-related, but I'm learning C in order to write code for my synth's main processor board!
>> What's the best way to deal with booleans in C? I see that we don't have a Boolean type explicitly.
>> Presumably there's a standard practice for this, since I'm not the first person to need a boolean.
>> If it makes any odds, I'm using Microchip's C Lite on dsPIC.
> If your specific compiler implements C99, you should find there's a
> header stdbool.h which introduces a type "bool". It's just an int
> really, though, so bitfields may make more sense if memory's tight.
> Traditionally, one sees quite of a lot of this sort of thing:
> [in a header file somewhere]
> #define FOO 1
> #define BAR 2
> #define BAZ 4
> #define BLARP 8
> followed by:
> [in code somewhere]
> if(flags == (BLARP | BAR)) { /* do stuff here */ }

I think you mean:

if(flags & (A | B) == (A | B)) {

Doing it the way you have mentioned is bad because it necessitates a
check of the other bit fields. So, a very good compiler could end up
having to do much less work if only two fields are compared. So you're
not only checking for those two flags to be on, you're also checking
for all the other flags to be off.

Additionally, you disable code reuse and code expandability, because
if you suddenly have new flags that you hadn't had before, they
disable this branch - even if the branch has nothing to do with those
new flags (and almost certainly doesn't, because how could it be using
the value of things that didn't exist when you were making it work the
first time)

Tom: if you want to have a look at how C performs in the critical
places, get good with a disassembler. Also learn how to inline
assembly code.

There's little shame in copying asm code step by step over to C, since
C is a low-level language itself. They really aren't that far apart,
especially in the situation given by the application you're using C


> Regards,
> Steff
> Disclaimer: I'm a sysadmin who occasionally has to read or write C,
> not a C language scientist.
> _______________________________________________
> Synth-diy mailing list
> Synth-diy at dropmix.xs4all.nl
> http://dropmix.xs4all.nl/mailman/listinfo/synth-diy

More information about the Synth-diy mailing list