Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to use bitwise XOR in C

2 Answers

0 votes
#include <stdio.h>

int main(void) {

    int x = 3, y = 3;
    printf("%d\n", x ^ y);
    
    x = 3, y = 0;
    printf("%d\n", x ^ y);
    
    x = 0, y = 3;
    printf("%d\n", x ^ y);

    x = 0, y = 0;
    printf("%d\n", x ^ y);

    return 0;
}




/*
run:

0
3
3
0

*/

 



answered Apr 13, 2023 by avibootz
0 votes
#include <stdio.h>

/*

X Y | X ^ Y
0 0 |   0
0 1 |   1
1 0 |   1
1 1 |   0

*/

void print_bits(int n, int size) {
    for (int i = 1 << (size - 1); i > 0; i = i / 2)
        (n & i) ? printf("1") : printf("0");
}

int main(void) {

    int x = 5, y = 5;
    print_bits(x, 8);
    printf("\n^\n");
    print_bits(y, 8);
    printf("\n=\n");
    print_bits(x ^ y, 8);
    printf("\n\n\n");

    x = 7, y = 0;
    print_bits(x, 8);
    printf("\n^\n");
    print_bits(y, 8);
    printf("\n=\n");
    print_bits(x ^ y, 8);
    printf("\n\n\n");
    
    x = 0, y = 6;
    print_bits(x, 8);
    printf("\n^\n");
    print_bits(y, 8);
    printf("\n=\n");
    print_bits(x ^ y, 8);
    printf("\n\n\n");

    x = 0, y = 0;
    print_bits(x, 8);
    printf("\n^\n");
    print_bits(y, 8);
    printf("\n=\n");
    print_bits(x ^ y, 8);
    printf("\n\n\n");

    return 0;
}




/*
run:

00000101
^
00000101
=
00000000


00000111
^
00000000
=
00000111


00000000
^
00000110
=
00000110


00000000
^
00000000
=
00000000

*/

 



answered Apr 13, 2023 by avibootz

Related questions

1 answer 171 views
171 views asked Jun 13, 2015 by avibootz
1 answer 61 views
61 views asked Jul 12, 2025 by avibootz
2 answers 204 views
1 answer 174 views
2 answers 327 views
1 answer 59 views
59 views asked Jul 11, 2025 by avibootz
...