I have a piece of code like
if (A[i][j] === 1)
A[i][j] = 0;
else
A[i][j] = 1;
and I'm wondering how I can optimize it like
var elem = A[i][j]; // obviously A[i][j] is an R-value ... I want an L-value
if (elem === 1)
elem = 0;
else
elem = 1;
In addition, I'm wondering if there's a single operation that can perform the equivalent of toggling between 0
and 1
, the equivalent of
if (elem === 1)
elem = 0;
else
elem = 1;
Is there some fancy bitwise operation that can do it?