# Xor Party Tricks

> Source: <https://gist.github.com/rexim/6f2349b548fdead7ed790d1a40915ae1>
> Published: 2025-12-07 20:16:27+00:00

dup.c

      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      
Learn more about bidirectional Unicode characters

 
    Show hidden characters

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int xs[] = {7,62,2,46,73,43,26,82,5,95,57,56,44,21,40,79,13,6,9,8,72,59,65,81,60,78,13,85,87,58,48,25,32,47,67,4,31,19,33,1,92,14,53,89,84,54,29,10,17,3,77,70,45,97,34,23,86,55,15,64,68,83,76,41,18,39,94,22,74,11,69,49,12,35,20,90,100,98,36,63,91,38,66,93,50,96,61,71,75,37,52,88,30,28,99,27,42,51,80,24,16};

int main()

{

    int x = 0;

    for (int i = 1; i <= 100; ++i) {

        x ^= i;

    }

    size_t n = sizeof(xs)/sizeof(xs[0]);

    for (int i = 0; i < n; ++i) {

        x ^= xs[i];

    }

    printf("%d\n", x);

    return 0;

}

encrypt.py

      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      
Learn more about bidirectional Unicode characters

 
    Show hidden characters

def
 
encrypt
(
m
, 
k
):

 
return
 
''
.
join
([
chr
(
ord
(
a
)
^
k
) 
for
 
a
 
in
 
m
])

ll.c

      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      
Learn more about bidirectional Unicode characters

 
    Show hidden characters

#include <assert.h>

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

#include <string.h>

typedef struct {

    int value;

    uintptr_t xored;

} Node;

Node *node_create(int value)

{

    Node *node = malloc(sizeof(*node));

    memset(node, 0, sizeof(*node));

    node->value = value;

    return node;

}

typedef struct {

    Node *begin;

    Node *end;

} Linked_List;

void ll_append(Linked_List *ll, int value)

{

    if (ll->end == NULL) {

        assert(ll->begin == NULL);

        ll->end = node_create(value);

        ll->begin = ll->end;

    } else {

        Node *node = node_create(value);

        node->xored     = (uintptr_t)ll->end;

        ll->end->xored ^= (uintptr_t)node;

        ll->end         = node;

    }

}

Node *node_next(Node *node, uintptr_t *prev)

{

    Node *next = (Node*)(node->xored^(*prev));

    *prev = (uintptr_t)node;

    return next;

}

int main()

{

    Linked_List xs = {0};

    for (int x = 5; x <= 10; ++x) {

        ll_append(&xs, x);

    }

    uintptr_t prev = 0;

    for(Node *iter = xs.end; iter; iter = node_next(iter, &prev)) {

        printf("%d\n", iter->value);

    }

    return 0;

}

swap.c

      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      
Learn more about bidirectional Unicode characters

 
    Show hidden characters

#include
 
<stdio.h>

int
 
main
()

{

 
int
 
a
 
=
 
69
;

 
int
 
b
 
=
 
420
;

 
printf
(
"%d %d\n"
, 
a
, 
b
);

 
a
 ^= 
b
; 
// a = 69^420, b = 420

 
b
 ^= 
a
; 
// a = 69^420, b = 69

 
a
 ^= 
b
; 
// a = 420, b = 69

 
printf
(
"%d %d\n"
, 
a
, 
b
);

 
return
 
0
;

}
