!! c operator, is a two NOT?

JuanPablo picture JuanPablo · Apr 25, 2012 · Viewed 12.1k times · Source

I reading this code, and have this line

 switch (!!up + !!left) {

what is !! operator ? two logical NOT ?

Answer

Armen Tsirunyan picture Armen Tsirunyan · Apr 25, 2012

yes, it's two nots.

!!a is 1 if a is non-zero and 0 if a is 0

You can think of !! as clamping, as it were, to {0,1}. I personally find the usage a bad attempt to appear fancy.