Who would win?
Who would win?
Other urls found in this thread:
godbolt.org
godbolt.org
godbolt.org
twitter.com
If enabled is true, it is false, if its not true, its true, after those operations you would set the Boolean to false, and the setting it to true again.
Second one. The first one is BS and the last one is pretty much the same as the second one but less intuitive and less used (and not all languages have similar construct available).
Terms of speed? Last one.
> doing pre-mature optimization
second and third should be equivalent after compiling
>and not all languages have similar construct available
>not all languages have basic boolean operators
kys
enabled = enabled? false:true
Middle.
Not all languages have a bitwise xor that works this way on boolean values.
All three would probably be the same speed with a reasonably optimizing compiler. I bet gcc would generate the exact same code at -O2 and above, if not -O1.
>Not all languages have a bitwise xor that works this way on boolean values.
just kys
>>>www.reddit.com/r/programming
Here's -O2
>volatile
well memed
What?
enabled = 1 - enabled
Why not use atomic operations while you're at it?
Are you retarded?
>-O2
FUCKING NORMIE LEEEEEEEEEEAVE
-Ofast is only accepted optimization level here
Are you? Adding volatile will completely change code generation compared to OPs question. The same way atomic operations would.
yep, as expected 2nd and 3rd are equivalent. first one isn't because if statements often prevent optimization opportunities.
you're retarded if you don't understand why volatile is necessary here.
>Adding volatile will completely change code generation
No it won't. It's only there to stop the compiler from precomputing the final value of the variable and generating nothing but a single instruction.
I'm not going to write code to randomly generate an initial value just because some idiot doesn't understand simple qualifiers.
>you're retarded if you don't understand why volatile is necessary here.
Babbys first benchmark? If you don't know how to check the generated assembly without adding volatile then you've no place talking in this thread at all.
>No it won't.
Yeah? Then why does GCC at O2 generate completely different code here:
#include
extern bool enabled;
void f(void)
{
if (enabled == true) {
enabled = false;
} else {
enabled = true;
}
}
void g(void)
{
enabled = !enabled;
}
void h(void)
{
enabled ^= true;
}
f:
cmpb $0, enabled(%rip)
sete enabled(%rip)
ret
h:
xorb $1, enabled(%rip)
ret
g:
jmp h
>completely different code
>jmp h
This guy really is fucking retarded
Nr. 2
pretty entertaining troll to be honest. lel at jmp h.
>troll
I don't think so, he's just a moron on his way up to mount stupid
Second one is more common than the third and is faster than the firs. We can all agree though that the first is High School course-teir
just do ~enable
2nd one is more readable
>muh bool ar fester then ur bools bcus i xor dem lul
if your compiler doesn't generate the same assembly for all three, you've got a shit compiler.
Compilers generate the same code for the second two, not the first one.
What kind of operator is ^=?
quantum exclusion
bitwise xor assignment
logical xor equals.
1 1 = 0
0 1 = 1
1 0 = 1
0 0 = 0
You sure famalam?
clang: godbolt.org
gcc: godbolt.org
icc: godbolt.org
Last one. Only because my normie coworkers don't understand it.
>-O1
Test other -O parameters yourself, it's not that hard and all of the three cases still produce the same code. C'mon don't be a lazy ass.
a ^= b is equivalent to a = a ^ b
What Editor do u used?
Cute font, what is it?
Set a while loop before this code and put a print/cout in the end that verifies the state of it. That'd be interesting.
vim
profont
It's bitwise, not logical. != is the logical xor, and there is no logical xor equals operator in C
I meant bitwise logic. In this situation it really doesn't matter.
bitwise operations are slow as fuck
No they aren't. Where did you get that idea?
try doing them in javascript
> javascript
hahahahahahahahha
You should have mentioned you were a web dev in the first place so I knew to ignore you
u mad bro?
mad that javascript is the most popular and useful language?
What is this theme?