fork download
  1. #include <iostream>
  2.  
  3. void swap1(int *a, int *b)
  4. {
  5. *a ^= *b;
  6. *b ^= *a;
  7. *a ^= *b;
  8. }
  9.  
  10. void swap2(int *a, int *b)
  11. {
  12. __asm__ volatile (".intel_syntax noprefix\n\t\
  13. xor rax,rcx\n\t\
  14. xor rcx,rax\n\t\
  15. xor rax,rcx" : "=a" (*a), "=c" (*b) : "a" (*a), "c" (*b) : "memory");
  16. }
  17.  
  18. void swap3(int *a, int *b)
  19. {
  20. __asm__ volatile ("" : "=a" (*a), "=c" (*b) : "c" (*a), "a" (*b) : "memory");
  21. }
  22.  
  23. int main()
  24. {
  25. int a=6,b=7,c=8;
  26. std::cout<<a<<b<<c<<"\n";
  27. swap1(&a,&a);
  28. swap2(&b,&b);
  29. swap3(&c,&c);
  30. std::cout<<a<<b<<c<<"\n";
  31. swap1(&a,&b);
  32. swap2(&b,&c);
  33. swap3(&a,&b);
  34. std::cout<<a<<b<<c<<"\n";
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5528KB
stdin
Standard input is empty
stdout
678
078
870