In C (before C99), booleans are usually represented as
typedef int bool;
#define true 1
#define false 0
Why it is represented as ‘int’ rather than ‘float’?
This is an interview question, even I wonder why such question is asked!
Any convincing answers?
boolvalues are mostly used in comparisons, and using theinttype uses the integer ALU for these comparisons. It is very fast, as it’s in the CPU’s normal pipeline. If you were to use thefloattype, then it would have to use the floating-point unit, which would take more cycles.Also, if you wanted to support using your
booltype in mathematical expressions, i.e.:so as to avoid unnecessary branching, your use of the integer ALU would also be faster than using the floating point unit.
The above code is equivalent to the following branching code: