{"id":17040860,"url":"https://github.com/m4gnv5/cppfun","last_synced_at":"2026-02-27T19:41:22.079Z","repository":{"id":80537212,"uuid":"90051103","full_name":"M4GNV5/CppFun","owner":"M4GNV5","description":"Fun with the C preprocessor","archived":false,"fork":false,"pushed_at":"2020-01-25T19:56:25.000Z","size":23,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-29T10:59:41.921Z","etag":null,"topics":["cpreprocessor","macros","preprocessor"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"eupl-1.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/M4GNV5.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-05-02T15:50:52.000Z","updated_at":"2025-03-20T04:41:13.000Z","dependencies_parsed_at":"2023-03-09T07:45:27.576Z","dependency_job_id":null,"html_url":"https://github.com/M4GNV5/CppFun","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/M4GNV5/CppFun","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M4GNV5%2FCppFun","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M4GNV5%2FCppFun/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M4GNV5%2FCppFun/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M4GNV5%2FCppFun/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/M4GNV5","download_url":"https://codeload.github.com/M4GNV5/CppFun/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M4GNV5%2FCppFun/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29910806,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T19:37:42.220Z","status":"ssl_error","status_checked_at":"2026-02-27T19:37:41.463Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpreprocessor","macros","preprocessor"],"created_at":"2024-10-14T09:10:44.992Z","updated_at":"2026-02-27T19:41:22.064Z","avatar_url":"https://github.com/M4GNV5.png","language":"C","readme":"# C pre-processor fun\nMath in the C pre-processor by breaking up Numbers into arrays of 8 bits and implementing common operations on those arrays.\n\n## Stuff it can do\n```C\n#include \"convert.h\"\n#include \"math.h\"\n\n//this is esentially (6 / 2) * (15 - 13)\nBYTE2CONST(\n\tMUL(\n\t\tDIV(\n\t\t\tCONST2BYTE(6),\n\t\t\tCONST2BYTE(2)\n\t\t),\n\t\tSUB(\n\t\t\tCONST2BYTE(15),\n\t\t\tCONST2BYTE(13)\n\t\t)\n\t)\n)\n```\n\nrunning with `cpp test.h` gives\n```\n6\n```\n\n## See also\nI got the idea when looking at [orangeduck's cpp Brainfuck interpreter](https://github.com/orangeduck/CPP_COMPLETE)\nThe readme gives a good overview on how one can have fun with the C pre-processor. Be sure to also check out [C Preprocessor tricks, tips, and idioms](https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms) by pfultz2. However they both do math by simply incrementing/decrementing numbers using macros in the form of\n```C\n#define INC(x) INC_##x\n#define INC_0 1\n#define INC_1 2\n#define INC_2 3\n//...\n```\nWhen i saw this together with orangeduck's list functionality, i thought why not represent numbers as a list of 8 ones and zeros. Allowing us to easily implement common operations like addition/subtraction/multiplication and of course bitwise operations such as not/and/or/xor. See below for more information\n\n## How this works\n\n### Basics\nFirst of all the very basics. When you \"program\" in/with/for the C pre-processor you often use variable pasting. This looks something like this:\n```C\n#define NOT_1 0\n#define NOT_0 1\n#define NOT(X) NOT_##X\n```\nWhen we now enter `NOT(1)` it gets evaluated to `NOT_1` which is again a macro for `0`.\n\n### Lists\nBasically cpp only pastes strings, so we need some trickery to create lists or arrays if you wish. However when we pass a list as `(0,0,0,0)` we can just use the list as arguments like. Let me give an example\n```C\n#define _NOT_4BIT(b3, b2, b1, b0) (NOT(b3), NOT(b2), NOT(b1), NOT(b0))\n#define NOT_4BIT(X) _NOT_4BIT X\n```\nNow when we run `NOT_4BIT((0,1,0,1))` we get `(1,0,1,0)`. Thats great! Now using the same scheme we can create macros for the other bitwise operations and/or/xor. Then we can implement addition, and using `ADD(X, NOT(Y))` and setting the carry-in we can subtract.\n\nIn this repo all of this is done on 8 bit values\n\n### Conversion\nFor now conversion between between numbers and bit arrays (`BYTE2CONST` and `CONST2BYTE`) is done using 256 macros each.\nThat code gets generated by [generate_convert](generate_convert.sh). You can run it using `./generate_convert.sh`\n\n## License\n\nThe code is published under the EUPL\n\u003e The “European Union Public Licence” (EUPL) is a copyleft free/open source software license created on the initiative of and approved by the European Commission in 22 official languages of the European Union.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm4gnv5%2Fcppfun","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm4gnv5%2Fcppfun","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm4gnv5%2Fcppfun/lists"}