Defending the ternary operator

Andrew Koenig ark at
Wed Feb 12 00:21:46 CET 2003

>> If C++ has a bool type and builtin true/false then the C++ code
>> quoted above can be replaced with

>> return bool(arr[i] & mask)

Anders> Since we're in the process of tearing all the other languages apart,
Anders> I offer this for C/C++ bool when you want a 1 or 0 result (remember,
Anders> this is C, and C loves punctuation):

Anders> return !!(arr[i] & mask);

In C++, if this statement appears in the body of a function that
is defined as returning a bool, you can write

        return arr[i] & mask;

Andrew Koenig, ark at,

More information about the Python-list mailing list