C——swap

/* swap.c */
/* function swap to swap two numbers */

#include <stdio.h>

void swap(int*, int*);
void swapBit(int*, int*);

int main(){
    int x, y;

    printf("Please enter two numbers: ");    
    scanf("%d %d", &x, &y);
    
    printf("Before: ");
    printf("x, y = %d, %d\n", x, y);

    swap(&x, &y);
    printf("After swap(): ");
    printf("x, y = %d, %d\n", x, y);

    swapBit(&x, &y);
    printf("After swapBit(): ");
    printf("x, y = %d, %d\n", x, y);

    return 0;
}

void swap(int* x, int* y){
    int tmp = *x;
    *x = *y;
    *y = tmp;    
}

void swapBit(int* x, int* y){
    *x ^= *y;
    *y ^= *x;
    *x ^= *y;
}

 

posted @ 2019-09-07 15:22  no樂on  阅读(252)  评论(0编辑  收藏  举报