Working with binary

Started by
21 comments, last by Shannon Barber 18 years ago
Embedded platforms typically have terrible compilers, even when said compilers cost insanely large amounts of money.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Advertisement
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
!! 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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement