The basic thing to learn are the binary numbers 1 , 2 , 4 , 8 , 16 , 32 , and so on. Get used to counting these on your fingers, starting from the right, with 1 on the thumb of your right hand, through to 16 on the little finger, 32 on the little finger of the left hand and through to 512 on the thumb of the left hand.
If you need to calculate the mask for a certain number of hosts, count up until you get to a binary number greater than the required number, take the previous number of fingers, take that away from 32 and you have the /? mask. So for 50 hosts, that is 1 , 2, 4, 8, 16, 32, 64 which is 6 digits before the number larger than the one you want, take that from 32 and you get a /26 mask.
For subnets, do the same thing, but add the number of fingers to the classfull subnet mask. You have a 10.0.0.0 network and want 300 subnets, that is 1 , 2, 4, 8, 16, 32, 64, 124, 256, 512 which is 9 before the number higher than the one you want. Add that to the classful mask /8 and you get a /17 subnet mask for 300 subnets (actually you get 512 but it is binary).
That is how I calculate them, it works, is quick, and unless you lose a finger, you can count up to 1023 on your fingers !
When I said start with the thumb on the right hand, I was menaing that you have the palms of your hands facing you. So the right thumb is 1, right-index is 2, right middle is 4 and so on.