user136821: I was wondering how the nBits are calculated from the target, I know how the the target is calculated from nBits, but how is the nBits calculated from the target?

user208: I believe the algorithm is this:

Let b be the number of bits in the target: b = ⌈ log2(target+1)⌉.
Let e be the exponent: e = ⌊ (b/8)+1⌋.
Let m be the mantissa: m = ⌊ target/(256e-3)⌋
The nBits value encodes both m and e: nBits = 224e + m.

The computed mantissa should always be between 215 and 223-1. If it's larger, it would be interpreted as negative. If it's smaller, a lower exponent would provide more accuracy.

user136006: You can calculate nBits from the target value by first taking its base 256 logarithm and rounding that to the next higher number, which will give you the first Byte out of four, the exponent, of nBits. You now know how many trailing Bytes you can cut off the target (the exponent minus three) and you're left with the mantissa.
Putting that into a closed formula is a bit messy in my opinion. The existing formula for calculating the target is not very nice in the first place, since it actually tries to express an algorithm. But here you go (x being the target):

Example with the current nBits value 0x17073039:
x = 0x730390000000000000000000000000000000000000000

(floor(log(x, 256)) + 1) * 0x1000000 = 0x17000000

x / 256 ^ (0x17 - 3) = 0x73039

nBits = 0x17000000 + 0x73039 = 0x17073039

Please note that this formula does not work for all values. The mantissa, being a signed integer, should not exceed 2^23 - 1, which is why the exponent needs to be adjusted where this would be the case; e.g. for the maximum target 0x1d00ffff, where the expected exponent of 0x1c would lead to a mantissa of 0xffff00 and thus be interpreted negative.

136821: <p>I was wondering how the nBits are calculated from the target, I know how the the target is calculated from nBits, but how is the nBits calculated from the target?</p>
 208: <p>I believe the algorithm is this:</p>
<ul>
<li>Let <em>b</em> be the number of bits in the target: <em>b = ⌈ log<sub>2</sub>(target+1)⌉</em>.</li>
<li>Let <em>e</em> be the exponent: <em>e = ⌊ (b/8)+1⌋</em>.</li>
<li>Let <em>m</em> be the mantissa: <em>m = ⌊ target/(256<sup>e-3</sup>)⌋</em></li>
<li>The <em>nBits</em> value encodes both <em>m</em> and <em>e</em>: <em>nBits = 2<sup>24</sup>e + m</em>.</li>
</ul>
<p>The computed mantissa should always be between <em>2<sup>15</sup></em> and <em>2<sup>23</sup>-1</em>. If it's larger, it would be interpreted as negative. If it's smaller, a lower exponent would provide more accuracy.</p>
 136006: <p>You can calculate <code>nBits</code> from the target value by first taking its base 256 logarithm and rounding that to the next higher number, which will give you the first Byte out of four, the exponent, of <code>nBits</code>. You now know how many trailing Bytes you can cut off the target (the exponent minus three) and you're left with the mantissa.</p>
<p>Putting that into a closed formula is a bit messy in my opinion. <a href="https://bitcoin.stackexchange.com/a/57186/136006">The existing formula</a> for calculating the target is not very nice in the first place, since <a href="https://bitcoin.stackexchange.com/a/96484/136006">it actually tries to express an algorithm</a>. But here you go (x being the target):</p>
<p><a href="https://i.stack.imgur.com/ADVhB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ADVhB.png" alt="nbits_formula" /></a></p>
<p>Example with the <a href="https://mempool.space/block/00000000000000000000f52d760c45d1907250deb55d3e4a3e30d798784673cf" rel="nofollow noreferrer">current</a> <code>nBits</code> value <code>0x17073039</code>:</p>
<pre><code>x = 0x730390000000000000000000000000000000000000000

(floor(log(x, 256)) + 1) * 0x1000000 = 0x17000000

x / 256 ^ (0x17 - 3) = 0x73039

nBits = 0x17000000 + 0x73039 = 0x17073039
</code></pre>
<p><strong>Please note</strong> that this formula does not work for all values. The mantissa, being a signed integer, should not exceed 2^23 - 1, which is why the exponent needs to be adjusted where this would be the case; e.g. for the maximum target <code>0x1d00ffff</code>, where the expected exponent of <code>0x1c</code> would lead to a mantissa of <code>0xffff00</code> and thus be interpreted negative.</p>
