-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswap_integer.c
More file actions
39 lines (34 loc) · 782 Bytes
/
swap_integer.c
File metadata and controls
39 lines (34 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*====================================================================
* Description: swap the integer, not use temporary variable
* DATE: 2013/11/12
* Modify:
* Conclusion:
===================================================================*/
/* include */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Macro definition */
#define true 1
#define false 0
#define SWAP(a,b) (a) ^= (b), (b) ^= (a), (a) ^= (b)
void
swap_pointer(int *a, int *b)
{
*b = *a + *b;
*a = *b - *a;
*b = *b - *a;
}
int
main(int argc, char **argv)
{
int a = 0x55;
int b = 0x11;
/* use pointer */
swap_pointer(&a, &b);
printf("a:%x b:%x\n", a, b);
/* use XOR */
SWAP(a, b);
printf("a:%x b:%x\n", a, b);
return 0;
}