Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!orchid!rbutterworth From: rbutterworth@orchid.UUCP Newsgroups: comp.lang.c Subject: Generating Boolean values on BSD 4.3 Message-ID: <10249@orchid.waterloo.edu> Date: Fri, 14-Aug-87 11:20:35 EDT Article-I.D.: orchid.10249 Posted: Fri Aug 14 11:20:35 1987 Date-Received: Sat, 15-Aug-87 16:42:21 EDT Distribution: comp Organization: U of Waterloo, Ontario Lines: 35 The problem with trying to fake a Boolean type in C is that a Boolean variable may end up with a value that isn't restricted to 0 and 1. One way to help prevent such mistakes is to have Boolean as an enumerated type, with a macro that turns an expression into exactly one of the two values. e.g. typedef enum {FALSE=0, TRUE=1} Boolean; #define TRUTH(expression) ( (expression) ? TRUE : FALSE ) But of course this can generate fairly inefficient code in many common cases, such as when the expression is already a 0 or 1. #define TRUTH(expression) ((Boolean) ((expression)!=0)) or #define TRUTH(expression) ((Boolean) (!!(expression))) should generate better code. On BSD 4.3 though, the third example generates much better code than the second. e.g. try the following: extern int a, b, c; a = ((b==c)!=0); a = !!(b==c); The compiler does well with the double not, but fails to optimize the logically equivalent double comparison verison. Perhaps this is an obvious optimization that BSD might want to add to its compiler?