How is mining difficulty defined (formula)?
The nBits field is basically scientific notation in base-256 (256 is 2^8). As an example, we take the one found on the Bitcoin.org Developer Reference: 0x181bc330 (Big-Endian order). This is split into two parts, the 0x18 exponent (24 in decimal), and the 0x1bc330 mantissa. The mantissa is 3 bytes long, so subtract 3 from the exponent and then raise 256 to that power, and multiply by the mantissa just as in scientific notation:


  0x1bc330 × 256 ^ (0x18 - 3)


Giving the target, the mantissa followed by 21 0x00 bytes, or 42 zeroes in hexadecimal. 

See this answer for another example: https://bitcoin.stackexchange.com/a/61967/51948

EDIT: You can also think of it as allocating the number of bytes given in the exponent, in this case 0x18=24 bytes, and than filling the first 3 bytes with the mantissa.