[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Translating CPP



PureBytes Links

Trading Reference Links

William Vedder wrote:

>What does "+=" mean?
>What does "/=" mean?

C, C++, and Java have shortcuts for arithmetically changing the
value of a variable.  Here is how they work.  If you will be doing
more of these translations to EL you will see these often:

++a or a++ is the same as  a = a + 1
--a or a-- is the same as  a = a - 1

a += b     is the same as  a = a + b
a -= b     is the same as  a = a - b
a *= b     is the same as  a = a * b
a /= b     is the same as  a = a / b

...and so on for every operator between two values.  These would also
include the binary operators &, |, ^, <<, >> (AND, OR, Exclusive OR,
bit-shift to left, bit-shift to right) as &=, |=, ^=, <<=, and >>=.

The reason for these shortcuts is that they compile into more
efficient machine code than the "standard" way of changing a
variable.  They save one machine instruction, which can be important
if these operations occur in large time-critical loops.

Sometimes you will see code like

a = b * c++;

...which will NOT give the same result as

a = b * ++c;

...because in the first case, the ++ operator increments c AFTER the
value of c is used in the formula, and in the second case, the ++
operator increments c BEFORE it is used in the formula.

Also you will occasionally see a formula used within another formula,
like:

z = x + (a *= ++c) / (b = 2);

...which would require four separate statements in EL:

b = 2;
c = c + 1;
a = a * c;
z = x + a / b;

Confused now?

-- 
  ,|___    Alex Matulich -- alex@xxxxxxxxxxxxxx
 // +__>   Director of Research and Development
 //  \ 
 // __)    Unicorn Research Corporation -- http://unicorn.us.com