Posts

Showing posts with the label swap

a, b 값 바꾸기(XOR,swap)

XOR 연산으로 a, b 값 서로 바꾸기 public class test0917 { public static void main(String[] args){ int a = 5; int b =3; System.out.println(a+","+ b);   System.out.println("a^=b :"+(a^=b));//6 0101 xor 0011 = 0110 System.out.println("b^=a :"+(b^=a));//5 0011 xor 0110 = 0101 System.out.println("a^=b :"+(a^=b));//3 0110 xor 0101 = 0011 System.out.println(a+","+ b); } } ------------------------------------------------- 5,3 a^=b :6 b^=a :5 a^=b :3 3,5