a +=2 is the same as a = a+ 2

Jan 24, 2008 21:40

Compound operators are SO COOL!

Leave a comment

Comments 7

seanb January 25 2008, 03:44:36 UTC
In some languages, a |= b is even cooler.
"a" is set to the value of "b" IF AND ONLY IF a does not already have a value that avaluates to "true". This can be used as a simple mechanism to get defaults into your code.

Reply

freeptop January 25 2008, 16:30:33 UTC
As a software engineer who works on embedded systems a fair amount, I like being able to use a|=b to mean "do a bitwise OR of a with b and store the result in a". The use you mention doesn't make logical sense to me. Of course, to get the result you want in C, all I need is: "a ? true : a=b;" It's slightly longer, but it gets the trick done ;)

Reply


whizzy January 25 2008, 04:30:41 UTC
ooh, you're hooked now. geek. ;P

also, a++ is the same as a += 1

Reply


guilhermeseljuk January 25 2008, 07:01:26 UTC
Wait until you start overloading operators :))

Reply


sdaemon January 25 2008, 07:33:59 UTC
they're also easy to make obfuscated fuckups with. and things that work but are hard as hell to figure out. and things that just don't work.

i+++++i; is a valid statement in C.

however it probably won't compile.

it SHOULD be written as i++ + ++i;

but even better would be to use parentheses.

even better than that would be to not do something that fucking obnoxious and obfuscated.

Reply

sdaemon January 25 2008, 07:35:12 UTC
oh, and the reason it probably w on't compile is due tot he maximal munch strategy used by C parsers. Basically, the parser will interpret it as i++ ++ + i; and throw an error on the ++ operator just hanging there in space.

Reply

sdaemon January 25 2008, 07:35:46 UTC
and just wait til you see the difference between i++ and ++i;

Reply


Leave a comment

Up