{"slug": "xor-party-tricks", "title": "Xor Party Tricks", "summary": "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.", "body_md": "dup.c\n\n      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.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\n#include <stdio.h>\n\n#include <stdlib.h>\n\n#include <time.h>\n\nint 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};\n\nint main()\n\n{\n\n    int x = 0;\n\n    for (int i = 1; i <= 100; ++i) {\n\n        x ^= i;\n\n    }\n\n    size_t n = sizeof(xs)/sizeof(xs[0]);\n\n    for (int i = 0; i < n; ++i) {\n\n        x ^= xs[i];\n\n    }\n\n    printf(\"%d\\n\", x);\n\n    return 0;\n\n}\n\nencrypt.py\n\n      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.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\ndef\n \nencrypt\n(\nm\n, \nk\n):\n\n \nreturn\n \n''\n.\njoin\n([\nchr\n(\nord\n(\na\n)\n^\nk\n) \nfor\n \na\n \nin\n \nm\n])\n\nll.c\n\n      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.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\n#include <assert.h>\n\n#include <stdio.h>\n\n#include <stdlib.h>\n\n#include <stdint.h>\n\n#include <string.h>\n\ntypedef struct {\n\n    int value;\n\n    uintptr_t xored;\n\n} Node;\n\nNode *node_create(int value)\n\n{\n\n    Node *node = malloc(sizeof(*node));\n\n    memset(node, 0, sizeof(*node));\n\n    node->value = value;\n\n    return node;\n\n}\n\ntypedef struct {\n\n    Node *begin;\n\n    Node *end;\n\n} Linked_List;\n\nvoid ll_append(Linked_List *ll, int value)\n\n{\n\n    if (ll->end == NULL) {\n\n        assert(ll->begin == NULL);\n\n        ll->end = node_create(value);\n\n        ll->begin = ll->end;\n\n    } else {\n\n        Node *node = node_create(value);\n\n        node->xored     = (uintptr_t)ll->end;\n\n        ll->end->xored ^= (uintptr_t)node;\n\n        ll->end         = node;\n\n    }\n\n}\n\nNode *node_next(Node *node, uintptr_t *prev)\n\n{\n\n    Node *next = (Node*)(node->xored^(*prev));\n\n    *prev = (uintptr_t)node;\n\n    return next;\n\n}\n\nint main()\n\n{\n\n    Linked_List xs = {0};\n\n    for (int x = 5; x <= 10; ++x) {\n\n        ll_append(&xs, x);\n\n    }\n\n    uintptr_t prev = 0;\n\n    for(Node *iter = xs.end; iter; iter = node_next(iter, &prev)) {\n\n        printf(\"%d\\n\", iter->value);\n\n    }\n\n    return 0;\n\n}\n\nswap.c\n\n      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.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\n#include\n \n<stdio.h>\n\nint\n \nmain\n()\n\n{\n\n \nint\n \na\n \n=\n \n69\n;\n\n \nint\n \nb\n \n=\n \n420\n;\n\n \nprintf\n(\n\"%d %d\\n\"\n, \na\n, \nb\n);\n\n \na\n ^= \nb\n; \n// a = 69^420, b = 420\n\n \nb\n ^= \na\n; \n// a = 69^420, b = 69\n\n \na\n ^= \nb\n; \n// a = 420, b = 69\n\n \nprintf\n(\n\"%d %d\\n\"\n, \na\n, \nb\n);\n\n \nreturn\n \n0\n;\n\n}", "url": "https://wpnews.pro/news/xor-party-tricks", "canonical_source": "https://gist.github.com/rexim/6f2349b548fdead7ed790d1a40915ae1", "published_at": "2025-12-07 20:16:27+00:00", "updated_at": "2026-05-24 04:35:17.604703+00:00", "lang": "en", "topics": ["cybersecurity"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/xor-party-tricks", "markdown": "https://wpnews.pro/news/xor-party-tricks.md", "text": "https://wpnews.pro/news/xor-party-tricks.txt", "jsonld": "https://wpnews.pro/news/xor-party-tricks.jsonld"}}