BaZi for Beginners: From Heavenly Stems and Earthly Branches to Five Elements — A Programmer's Mental Model
An attempt to understand the underlying logic of BaZi (Chinese Four Pillars of Destiny) through the lens of a programmer — where the stems and branches are enums, the five elements are a type system, and their generating and controlling cycles are dependency relationships.
I learned to write code before I ever started studying BaZi (八字, Eight Characters / Four Pillars of Destiny).
That sequence gave me an unusual perspective: when I first encountered the system of Heavenly Stems and Earthly Branches (干支, Ganzhi), what came to mind wasn’t “destiny” but a beautifully designed enum type.
The Stems and Branches: A Base-60 Time Encoding System
The Ten Heavenly Stems (天干, Tiangan) — Jia, Yi, Bing, Ding, Wu, Ji, Geng, Xin, Ren, Gui (甲、乙、丙、丁、戊、己、庚、辛、壬、癸) — and the Twelve Earthly Branches (地支, Dizhi) — Zi, Chou, Yin, Mao, Chen, Si, Wu, Wei, Shen, You, Xu, Hai (子、醜、寅、卯、辰、巳、午、未、申、酉、戌、亥) — combine in pairs to form a circular sequence of 60 elements: Jia-Zi, Yi-Chou, Bing-Yin… all the way to Gui-Hai, then back to Jia-Zi.
In code, that looks like this:
STEMS = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸']
BRANCHES = ['子','醜','寅','卯','辰','巳','午','未','申','酉','戌','亥']
def get_ganzhi(n: int) -> str:
return STEMS[n % 10] + BRANCHES[n % 12]
A person’s birth moment can be precisely described by four pairs of stems and branches (the Year Pillar, Month Pillar, Day Pillar, and Hour Pillar). That’s what “BaZi” means — four pillars, each with two characters, making eight characters in total.
The Five Elements: A Type System
The Five Elements (五行, Wuxing) — Wood, Fire, Earth, Metal, Water (木、火、土、金、水) — serve as “type annotations” for the stems and branches:
| Element | Heavenly Stems | Earthly Branches |
|---|---|---|
| Wood | Jia, Yi (甲、乙) | Yin, Mao (寅、卯) |
| Fire | Bing, Ding (丙、丁) | Si, Wu (巳、午) |
| Earth | Wu, Ji (戊、己) | Chen, Xu, Chou, Wei (辰、戌、醜、未) |
| Metal | Geng, Xin (庚、辛) | Shen, You (申、酉) |
| Water | Ren, Gui (壬、癸) | Hai, Zi (亥、子) |
The five elements interact through “generating and controlling cycles” (生克, Sheng-Ke): Wood generates Fire, Fire generates Earth, Earth generates Metal, Metal generates Water, Water generates Wood; Wood controls Earth, Earth controls Water, Water controls Fire, Fire controls Metal, Metal controls Wood.
This is a directed graph — every node is both a “generator” for some nodes and a “controller” for others.
The Birth Chart: A Dependency Analysis
The core of BaZi analysis is examining the generating and controlling relationships among these eight characters (plus the additional element information hidden in their “hidden stems” or 藏干), to identify the “Yong Shen” (用神, the Useful God) — the element that the chart most needs to be supplemented or restrained.
This reminds me of dependency analysis in software architecture: identifying bottleneck nodes, finding overly coupled modules, and spotting places that need decoupling.
Of course, the study of destiny is far more complex than this. Patterns (格局), auspicious stars (神煞), great luck cycles and annual flows (大運流年) — each dimension is its own complete analytical system. But from this programmer’s perspective, I’ve at least found an entry point into understanding it.
The “BaZi Chart” tool above can generate your complete birth chart data — feel free to try it.