48. Pointer- Swap Value

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

void swap(int *p1, int *p2) {

  
    *p1=(*p1)^(*p2) ;

    *p2=*p1^*p2;
    *p1=(*p1)^(*p2);

}

int main() {
    int a, b;
    scanf("%d %d", &a, &b);

    swap(&a, &b);

    printf("a = %d ", a);
    printf("b = %d", b);

    return 0;
}

Solving Approach

Using X0R to swap numbers without using third variable.

XOR can be used for :
1. swapping numbers
2. Flipping specific bit ... a^1
 

 

 

Was this helpful?
Upvote
Downvote