Pokemon Gold and Silver - Magikarp's Length Formula
The formula used to calculate the length of a Magikarp in the Lake of Rage is very complex. It involves Magikarp's ID and its DVs, and uses the help of a lookup table, shown below.
The procedure uses a bit operation called a "right rotate carry" (RRC), in which the low bit is saved, a right shift is made, and the saved bit is placed as the high bit of the byte. The following function in C can calculate the RRC for a byte, for illustration.
char rrc(char n){
return ((n>>1)|((n&1)<<7));
}
To begin, the formula calculates two numbers called B and C as follows.
B=RRC(RRC(X)) xor RRC(V)
C=RRC(RRC(Y)) xor RRC(W)
- V is the high byte of Magikarp's ID
- W is the low byte of Magikarp's ID
- X = (16*Attack DV)+Defense DV
- Y = (16*Speed DV)+Special DV
Next, the procedure utilizes a lookup table with four columns as shown here. Values are in hexadecimal.
E D F A 6E,00,01,02 36,01,02,03 C6,02,04,04 96,0A,14,05 1E,1E,32,06 2E,45,64,07 C6,7F,96,08 5E,BA,96,09 6E,E1,64,0A F6,F4,32,0B C6,FC,14,0C BA,FE,05,0D 82,FF,02,0E E6,FF,01,10
The procedure checks the value of D in each row against B. If B is less than D, then the formula uses the values of E, F, and A in that row.
The final formula is below, which calculates Magikarp's length in centimeters. The maximum is 162.5 cm, which occurs when all DVs equal 15 and the ID equals 0.
When converting to inches, the game rounds down to the nearest inch.
Length = (A*100+((int(((BC+65536-DE)%65536)/F))%256))/10
where:
- BC = (B*256+C)
- DE = (D*256+E)