cd /news/cybersecurity/xor-party-tricks · home topics cybersecurity article
[ARTICLE · art-13104] src=gist.github.com ↗ pub= topic=cybersecurity verified=true sentiment=· neutral

Xor Party Tricks

The article presents four code examples demonstrating the use of the XOR bitwise operator in programming. The examples include finding a missing number in a sequence, encrypting text with a key, traversing a linked list using XOR-linked pointers, and swapping two integer values without a temporary variable.

read2 min views33 publishedDec 7, 2025

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 ;

}

── more in #cybersecurity 4 stories · sorted by recency
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/xor-party-tricks] indexed:0 read:2min 2025-12-07 ·