c - Is it possible to swap two variables with only += and -= no other operators or variables? -


i want swap 2 variables += , -= operators , without temporary variables. know standard solutions like:

a = a+b; b = a-b; = a-b; 

and xor:

a ^= b; b ^= a; ^= b; 

but cannot figure out how += , -=. possible?

my classmates offer nice solution: answer no

lets denote a (1 0) , b (0 1)

the matrix a is

1 0 0 1 

+= , -= mean adding or subtracting lines each other. means determinant either not change sign or equal 0. end matrix is

0 1 1 0 

with determinant equal -1, can not it

update: have these operations:

  1. a-=a. 1 line becomes 0, det=0
  2. a+=a. means multiplying line 2, det a'= 2*det a
  3. a+=b. means elementary transformation, not change value of det
  4. a-=b. same thing 3.

then apply proof b-=b, b+=b, b+=a, b-=a. determinant not change sign or 0

update 2: @tom said here example of how in c#: http://ideone.com/uzvlml. however, in c thats not correct: http://codepad.org/rmhn9iqb. can clarify difference of b -= b += b in c , c#?


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -