Implementing an Odd/Even Checker with 4 Billion If Statements: From C to Assembly and Performance Limits
The article explores how a developer generated billions of if‑statements in C to decide odd or even for 32‑bit numbers, scaling the approach with Python‑generated code, custom assembly, memory‑mapped binaries, and discusses the practical limits and performance of such an extreme solution.
When a TikTok video sparked curiosity, developer Andreas Karlsson created a program that determines whether a number is odd or even using a massive chain of if statements, eventually reaching 4 billion comparisons for 32‑bit integers.
The article walks through the initial 8‑bit C implementation, shows how the author generated the repetitive if blocks with a Python script, and demonstrates compilation and testing of the resulting executable.
Scaling the approach to 16‑bit and then 32‑bit numbers produces source files of hundreds of gigabytes, which exceed the limits of typical compilers and executable formats; the author discusses compiler warnings, heap‑space errors, and the need to map the generated binary into memory.
To avoid the atoi limitation for large unsigned values, the code is updated to use strtoul, fixing incorrect results for numbers above 2³¹.
Finally, a handcrafted assembly routine and a custom binary loader are presented, illustrating how the massive if table can be executed directly from a memory‑mapped file, achieving reasonable performance despite the extreme code size.
The piece concludes with reflections on the practicality of such over‑engineered solutions and community reactions.
/* Copyright 2023. All unauthorized distribution of this source code */</code>
<code>#include <stdio.h></code>
<code>#include <stdint.h></code>
<code>#include <stdlib.h></code>
<code>int main(int argc, char* argv[])</code>
<code>{</code>
<code> uint8_t number = atoi(argv[1]); // No problems here</code>
<code> if (number == 0)</code>
<code> printf("even
");</code>
<code> if (number == 1)</code>
<code> printf("odd
");</code>
<code> /* ... many more if statements ... */</code>
<code>} PS > cl.exe /Od program.c</code>
<code>PS > .\program.exe 0</code>
<code>even</code>
<code>PS > .\program.exe 3</code>
<code>odd print("/* Copyright 2023. All unauthorized distribution of this source code")</code>
<code>print(" will be persecuted to the fullest extent of the law*/")</code>
<code>print("#include <stdio.h>")</code>
<code>print("#include <stdint.h>")</code>
<code>print("#include <stdlib.h>")</code>
<code>print("int main(int argc, char* argv[])")</code>
<code>print("{")</code>
<code>print(" uint8_t number = atoi(argv[1]); // No problems here")</code>
<code>for i in range(2**8):</code>
<code> print(" if (number == "+str(i)+")")</code>
<code> if i % 2 == 0:</code>
<code> print(" printf(\"even\
\");")</code>
<code> else:</code>
<code> print(" printf(\"odd\
\");")</code>
<code>print("}") ; Argument is stored in ECX, return value in EAX</code>
<code>XOR EAX, EAX ; Set eax to zero (return value for odd number)</code>
<code>CMP ECX, 0h ; Compare arg to 0 </code>
<code>JNE 3h ; Skip next two instructions if it wasn't equal</code>
<code>INC EAX ; It was even, set even return value (1)</code>
<code>RET ; Return</code>
<code>CMP ECX, 1h ; Compare arg to 1</code>
<code>JNE 2 ; Skip next instruction if not equal</code>
<code>RET ; Odd return value already in EAX, just RET</code>
<code>; add the next 2...2^32-1 comparisons here</code>
<code>RET ; Fallback return #include <stdio.h></code>
<code>#include <Windows.h></code>
<code>#include <stdint.h></code>
<code>int main(int argc, char* argv[])</code>
<code>{</code>
<code> uint32_t number = strtoul(argv[1], NULL, 10); // No problems here</code>
<code> // Open code file</code>
<code> HANDLE binFile = CreateFileA("isEven.bin", GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);</code>
<code> // Get 64 bit size of file</code>
<code> LARGE_INTEGER codeSize;</code>
<code> GetFileSizeEx(binFile, &codeSize);</code>
<code> // Create memory map of the file</code>
<code> HANDLE mapping = CreateFileMapping(binFile, NULL, PAGE_EXECUTE_READ, 0, 0, NULL);</code>
<code> // Get a pointer to the code</code>
<code> LPVOID code = MapViewOfFile(mapping, FILE_MAP_EXECUTE | FILE_MAP_READ, 0, 0, codeSize.QuadPart);</code>
<code> int (*isEven)(int) = (int(*)(int))code;</code>
<code> if (isEven(number))</code>
<code> printf("even
");</code>
<code> else</code>
<code> printf("odd
");</code>
<code> CloseHandle(binFile);
</code>
<code>}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
