Advertisement
KDOXG

swap

Feb 21st, 2019
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.32 KB | None | 0 0
  1. void swapSigned(int *a, int *b)
  2. {
  3.     /** 1st method: sum and subtraction swap
  4.     * Beware of overflow if using high values.
  5.     */
  6.    
  7.     *a=*a+*b;
  8.     *b=*a-*b;
  9.     *a=*a-*b;
  10. }
  11.  
  12. void swap(int *a, int *b)
  13. {
  14.     /** 2nd method: boolean swap
  15.     * It completely avoids overflow.
  16.     */
  17.    
  18.     *a=(*a)&(*b);
  19.     *b=(*a)&(*b);
  20.     *a=(*a)&(*b);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement