Fundamentals 14 min read

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.

IT Services Circle
IT Services Circle
IT Services Circle
Implementing an Odd/Even Checker with 4 Billion If Statements: From C to Assembly and Performance Limits

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 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
uint8_t number = atoi(argv[1]); // No problems here
if (number == 0)
printf("even\n");
if (number == 1)
printf("odd\n");
/* ... many more if statements ... */
}
PS > cl.exe /Od program.c
PS > .\program.exe 0
even
PS > .\program.exe 3
odd
print("/* Copyright 2023. All unauthorized distribution of this source code")
print("   will be persecuted to the fullest extent of the law*/")
print("#include <stdio.h>")
print("#include <stdint.h>")
print("#include <stdlib.h>")
print("int main(int argc, char* argv[])")
print("{")
print("    uint8_t number = atoi(argv[1]); // No problems here")
for i in range(2**8):
print("    if (number == "+str(i)+")")
if i % 2 == 0:
print("        printf(\"even\\n\");")
else:
print("        printf(\"odd\\n\");")
print("}")
; Argument is stored in ECX, return value in EAX
XOR EAX, EAX ; Set eax to zero (return value for odd number)
CMP ECX, 0h ; Compare arg to 0
JNE 3h ; Skip next two instructions if it wasn't equal
INC EAX ; It was even, set even return value (1)
RET ; Return
CMP ECX, 1h ; Compare arg to 1
JNE 2 ; Skip next instruction if not equal
RET ; Odd return value already in EAX, just RET
; add the next 2...2^32-1 comparisons here
RET ; Fallback return
#include <stdio.h>
#include <Windows.h>
#include <stdint.h>
int main(int argc, char* argv[])
{
uint32_t number = strtoul(argv[1], NULL, 10); // No problems here
// Open code file
HANDLE binFile = CreateFileA("isEven.bin", GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// Get 64 bit size of file
LARGE_INTEGER codeSize;
GetFileSizeEx(binFile, &codeSize);
// Create memory map of the file
HANDLE mapping = CreateFileMapping(binFile, NULL, PAGE_EXECUTE_READ, 0, 0, NULL);
// Get a pointer to the code
LPVOID code = MapViewOfFile(mapping, FILE_MAP_EXECUTE | FILE_MAP_READ, 0, 0, codeSize.QuadPart);
int (*isEven)(int) = (int(*)(int))code;
if (isEven(number))
printf("even\n");
else
printf("odd\n");
CloseHandle(binFile);
}
performancecode generationalgorithmCassemblyLow-level programming
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.