Working with binary
Embedded platforms typically have terrible compilers, even when said compilers cost insanely large amounts of money.
ALl you need to do to convert an intergral type to a bool is to compare it with zero. i.e.
if ((x & 0x20) != 0)
It's the way to do it in strongly typed languages such as Pascal. It makes sense to do the same in C.
!! guarantees the result is either 0 or 1 in both C and C++. It is cast over from C due to the very problem BeetNutts mentioned. There is no native bool type in C89 (one was added in C99); in C++ it avoids warnings. Sometimes you still get a warning about possible truncation even when you cast it to a bool. If you use a C++ cast, it should look like:
bool b = bool(expr & mask);
The bool snippet program above would print "peppers" with a C compiler assuming it had a typedef for bool, but in C++ bool is converted to 0 or 1 so it prints "cheese". There are other subtle type issues that relate to overloading that necessitated the addition of the bool type in C++ (e.g. the result of comparisons such as operator==() needed a unique return type, int did not work 100% correctly).
For the most part embedded compilers are caught up, and MISRA C++ is in progress. In a few more years we may finally start to see a transition from C to C++ in the embedded automotive sector.
bool b = bool(expr & mask);
The bool snippet program above would print "peppers" with a C compiler assuming it had a typedef for bool, but in C++ bool is converted to 0 or 1 so it prints "cheese". There are other subtle type issues that relate to overloading that necessitated the addition of the bool type in C++ (e.g. the result of comparisons such as operator==() needed a unique return type, int did not work 100% correctly).
For the most part embedded compilers are caught up, and MISRA C++ is in progress. In a few more years we may finally start to see a transition from C to C++ in the embedded automotive sector.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement