Algorithms Forever ...: Circular Swap
http://stackoverflow.com/ questions/8635686/swap-three- numbers-in-single-statement
However, other rather frightening bits of Java code are legal, and do compile, and require a fair bit of thought to understand. Consider these snippets from the Java Language Specification itself:
This code prints 9, 12, and 12.
public SQLInteger(Integer obj) {
if (isnull = (obj == null))
;
else
value = obj.intValue();
}
if(a=b) // when a, b is boolean variable, it compiles, otherwise not.
Read full article from Algorithms Forever ...: Circular Swap
Given three numbers a, b and c. Write a program to swap them circularly (as per following example) using only one single statement in any programming language.
Ex. a=10 b=20 c=30
Answer a=20 b=30 c=10
Ex. a=10 b=20 c=30
Answer a=20 b=30 c=10
Solution :
a = a^b^c^(b=c)^(c=a);
This is equivalent to a = (a^b^c^c^a) = b along with the values of b and c being changed by b=c and c=a assignments.
This is an excellent answer and defeats the proposed ones in the reference.
Reference :This is equivalent to a = (a^b^c^c^a) = b along with the values of b and c being changed by b=c and c=a assignments.
This is an excellent answer and defeats the proposed ones in the reference.
http://stackoverflow.com/
The operator ^ is undefined for the argument type(s) float, float
http://www.dummies.com/how-to/content/assignment-statements-in-java.html
An assignment expression has a return value just as any other expression does; the return value is the value that’s assigned to the variable. For example, the return value of the expression a = 5 is 5. This allows you to create some interesting, but ill-advised, expressions by using assignment expressions in the middle of other expressions. For example:
int a; int b; a = (b = 3) * 2; // a is 6, b is 3
Using assignment operators in the middle of an expression can make the expression harder to understand, so it’s not recommend that.
http://stackoverflow.com/questions/8635686/swap-three-numbers-in-single-statement
You can use this in many languages like
C,C++ and Java
.
It will work for
float
and long
also.a=(a+b+c) - (b=c) - (c=a);
http://bryanpendleton.blogspot.com/2009/05/java-assignment-expression.html
Java's "if" statement only accepts boolean-valued expressions in its test, which means that some of the worst types of accidental mistakes from C will not occur in Java; the following code will not compile:
int i = 2;
if (i = 3)
System.out.println("I is 3.");
However, other rather frightening bits of Java code are legal, and do compile, and require a fair bit of thought to understand. Consider these snippets from the Java Language Specification itself:
int i = 2;
int j = (i=3) * i;
System.out.println(j);
int a = 9;
a += (a = 3);
System.out.println(a);
int b = 9;
b = b + (b = 3);
System.out.println(b);
This code prints 9, 12, and 12.
public SQLInteger(Integer obj) {
if (isnull = (obj == null))
;
else
value = obj.intValue();
}
if(a=b) // when a, b is boolean variable, it compiles, otherwise not.
Read full article from Algorithms Forever ...: Circular Swap