Force all non-zero values to 1
June 20th, 2008
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





