Boolean “implies” operator

1 07 2010

A cow-orker recently asked me if XOR worked on boolean expressions. I told him that it did but I would consider it unreadable and he should use != instead:

// if either ptr is not null or length is not zero
// (but not both)
if ( (ptr != NULL) ^ (length != 0) ) 

// same but IMO clearer
if ( (ptr != NULL) != (length != 0) )

He then asked whether there is an implies operator, after meditating on this for some time I was inspired by the infamous goes to operator:

int x = 10;
while( x --> 0 ) { // x goes to 0
    printf("%d ", x);
}

I’m very proud to present the implies operator:

// ptr != NULL implies length != 0
if ( (length != 0) <= (ptr != NULL) )

Unfortunately this operator has the quality that it’s read right to left (X implies Y Y <= X) .

 

Edit: Two years after writing this I showed it to a friend who pointed out this is completely wrong, X implies Y X <= Y which pretty much blows the whole thing out of the water. Sorry for wasting your time 😦

Advertisements

Actions

Information

One response

5 09 2010
phimuemue

Haha, that’s nice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: