rulururu

post Force all non-zero values to 1

June 20th, 2008

Filed under: C++ — Kai @ 11:56 am

Let’s assume that you want to set all non-zero values to 1.
Usually, I’d prefer the simple and understandable way:

if(a != 0)
   a = 1;

or

a = (a==0) ? a : 1;

I found out a cool trick that I didn’t want to keep back from you:

a = !!a;

Funny, but too adventurous to use it in real life ;)

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)