Defending the ternary operator

Andrew Koenig ark at research.att.com
Tue Feb 11 18:21:46 EST 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 research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list