How to calculate mask from TCP port range

I'm trying to wrap my head around calculating a mask value which can denote L4 port range. Suppose the input range is 500-1000. Given the mask and lower value, how can the upper value be calculated?