$Trainer.id is the player's full 32-bit trainer ID. The upper 16 bits of this number are the secret ID, and the lower 16 bits are the public ID. Both numbers on their own go between 0 and 65535. The two numbers are stuck together in binary form, like so:
Secret ID:
1010 1010 1010 1010
Public ID:
1100 1100 1100 1100
$Trainer.id:
1010 1010 1010 1010 1100 1100 1100 1100
To get either the secret ID or public ID back from this, we're going to have to fiddle with the number's bits. There are several different ways you can do this.
Code:
def publicID(id=nil)
return id ? id&0xFFFF : (@id&0xFFFF)
end
This is how you get the public ID. What it does is perform a bitwise AND operation on the ID number with the number 0xFFFF (where & is the symbol used for this operator, like how = is the symbol meaning "set to equal" or == means "check if equals"). The number 0xFFFF is a hexadecimal number (you know it's hex because it has 0x at the beginning - 0xFFFF is the number 65535), which in binary is the number
1111 1111 1111 1111. A bitwise AND operation looks at each bit in both numbers in turn, and in the result, makes that bit 1 if the first number's bit is 1 AND the second number's bit is 1 (or 0 otherwise).
$Trainer.id:
1010 1010 1010 1010 1100 1100 1100 1100
0xFFFF:
0000 0000 0000 0000 1111 1111 1111 1111
Result: 0000 0000 0000 0000
1100 1100 1100 1100
There's a bunch of zeroes at the beginning of the result, but just like the number 00042, they can be ignored. The result is the public ID (in blue).
The same approach would work if you want to get the secret ID (the red number). Remember that you can stick zeroes in front of a number, so 0xFFFF is the same as 0x0000FFFF. If that got just the second half of the $Trainer.id, and we now want just the first half, how about using 0xFFFF0000 instead?
$Trainer.id:
1010 1010 1010 1010 1100 1100 1100 1100
0xFFFF0000:
1111 1111 1111 1111 0000 0000 0000 0000
Result:
1010 1010 1010 1010 0000 0000 0000 0000
Success! Of sorts. We've gotten rid of the blue number, but we're still left with a bunch of zeroes at the end. 300 is a different number to 3, so we can't just ignore them. How do we get 3 from 300, then? Divide by 100! In this case, we want to divide by the binary number 1 0000 0000 0000 0000 to get rid of those 16 zeroes. In other words, divide by 65536.
Putting all this together, we get this code:
Code:
def secretID
return (@id&0xFFFF0000) / 65536
end
This isn't the most elegant solution, but it'll do what you want. The most elegant solution involves bitwise shifting, but you've already got something that works so there's no point trying to explain something new just for the sake of having some slightly prettier code.
My explanation of binary/hexadecimal numbers and the bitwise AND operator is quite poor, I know.
There's a nice page on Bulbapedia which explains these things too, and even though it's about calculating a Pokémon's gender/nature/ability/shininess from its personal ID, the maths and explanation is still good.