Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
You can xor it for the swag :)
boolean ^= 1;
XOR:
x1 x2 u
0 0 0
0 1 1 *
1 0 0
1 1 0 *
In your case x1 is the variabile and x1 is always 1 so you'll have the * cases that basically it's a NOT :)
It's the same simplified bitwise trick to swap two variables without a third variable
a ^= b
b ^= a
a ^= b
For example, if a = 5 = 0101 and b = 3 = 0011
The first xor produces
a = 0110
The second with the new a value
b = 0101 = 5
And the third with the new b
a = 0011 = 3 -
AllenII16178y@localghost. Is this doable in JavaScript? If yes, i would try it just to see it work
-
@AllenII Yes it works, you can test it on the fly writing in your browser:
javascript: a=5; b=3; alert(a+" "+b); a^=b; b^=a; a^=b; alert(a+" "+b); -
I actually thought that looked cute. It looks more sympathetic, like you are politely asking it to change this value, whereas the negation looks more like that poor variable had done something wrong.
I'll start using your notation from now on! -
It's better than my company's current codebase:
boolean = boolean || true;
😎😎😎😎 -
@antonis179 how does that even work? If the boolean was true it would remain true after that statement. Did you mean to xor it?
-
@siljamicke no. Fucking. Idea. Found it like this didn't make it ;)
A colleague is rewriting that part thankfully!!! -
@antonis179 tell your colleague that it should be:
if(boolean) boolean = boolean && false;
else boolean = boolean || true
How i used to toggle a boolean:
foo ? false : true;
How i decided to toggle a boolean last night:
foo = !foo
I wondered why i only just thought of that.
undefined
sometimes i worry about myself