Ok, first you need to realize that everything is in binary.
Reread that first line until you're ok with it. You don't need to understand binary, though it would be nice.
Ok, here we go:
There are 16 bits that are randomly generated when a monster is caught. Each bit can contain two values. If you had a penny, it could be heads or tails. If you had a penny and a nickel, however, you have four states:
Now, genes range from 0-15. That is 16 possibilities. How many bits is this? Well, if you had a penny, a nickel, a dime, and a quarter, you would have 2*2*2*2 =16 possibilities.
So you need four bits. Picture yourself as a Game Freak designer. You need twenty bits (5 stats * 4 bits), but have only 16 without writing a couple extra lines of code and wasting a precious byte of save ram on each monster. Add an extra byte, and players can store less monsters, or less items, or something. So you save the byte. Here's how:
The 16 bit value is broken into 4 groups. Each means, in order, attack, defense, speed, and special. Hitpoints is constructed from the last bit of each value. In base 10, if a number ends in 0 it is divisible by 10. In base 2, if a number ends in zero it is divisible by 2. So for a hit point gene of 15 (max) all values need to end in 1 aka be odd numbers. The odd numbers are 1,3,5,7,9,B,D,F. (B=11, D=13, F=15).
If you want a specific number, you have to realize that it is binary. Here is the simple way:
HP gene= 8*W + 4*X + 2*Y + Z
W = 1 if attack is odd, 0 if even.
X = 1 if defense is odd, 0 if even.
Y = 1 if speed is odd, 0 if even.
Z = 1 if special is odd, 0 if even
Hope this helps,