<p>Here's the function that implements this. Each annotation refers to the line below.</p>

<hr>

<pre><code>unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&amp; params)
{
</code></pre>

<p>If we're on a network without retargeting, don't retarget.</p>

<pre><code>    if (params.fPowNoRetargeting)
        return pindexLast-&gt;nBits;
</code></pre>

<p>Don't adjust up or down by more than 4x.</p>

<pre><code>    // Limit adjustment step
    int64_t nActualTimespan = pindexLast-&gt;GetBlockTime() - nFirstBlockTime;
    if (nActualTimespan &lt; params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan &gt; params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;

    // Retarget
    const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
    arith_uint256 bnNew;
</code></pre>

<p>Take the old nBits in compact form, and turn it into 256-bit form. I explain that in more detail here: <a href="https://bitcoin.stackexchange.com/questions/48707/difficulty-target-representation-in-bitcoin-wiki/48708#48708">Difficulty target representation in bitcoin wiki</a></p>

<pre><code>    bnNew.SetCompact(pindexLast-&gt;nBits);
</code></pre>

<p>Multiply by the timespan (the time between the first block in the retargeting period, and the last block in the retargeting period.) Then, divide by the targeted timespan.</p>

<pre><code>    bnNew *= nActualTimespan;
    bnNew /= params.nPowTargetTimespan;
</code></pre>

<p>If the result would be less than difficulty 1, change to difficulty 1 instead.</p>

<pre><code>    if (bnNew &gt; bnPowLimit)
        bnNew = bnPowLimit;
</code></pre>

<p>Re-encode as compact form for bignums. This is the reverse of <code>SetCompact(int32_t)</code>.</p>

<pre><code>    return bnNew.GetCompact();
}
</code></pre>

<hr>

<p>Let's also work through the example you give. 0x1c05a3f4 should change to 0x1c0168fd.</p>

<pre><code>0x1c05a3f4
1c          05a3f4
^ exponent  ^ mantissa
</code></pre>

<p>To decimal:</p>

<pre><code>369652 * 256^28
</code></pre>

<p>Let's work out the timespans, actual and targeted</p>

<pre><code>Actual: 1279297671 - 1279008237 = 289434
Target: 2016*600 = 1209600
Actual/Target ~= 0.239
</code></pre>

<p>Note that Target is more than 4x larger than Actual. We need to cap this, so we don't raise the difficulty too fast.</p>

<pre><code>Capped timespan: Target/4 = 302400
</code></pre>

<p>Final calculation:</p>

<pre><code>369652 * 256^28 * (302400/1209600) = 92413 * 256^28
</code></pre>

<p>Back to hex:</p>

<pre><code>1c          0168fd
^ exponent  ^ mantissa
</code></pre>

<p>Re-encode:</p>

<pre><code>0x1c0168fd
</code></pre>

<p>...and that is in fact correct.</p>

CC BY-SA 3.0
2017-03-31T05:34:46.577Z
2017-03-31T05:34:46.577Z
2017-04-13T12:47:36.707Z
Nick ODell
<p>How can I get this value, as I understand this complexity</p>

<p><a href="https://github.com/bitcoin/bitcoin/blob/master/src/test/pow_tests.cpp#L55" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/master/src/test/pow_tests.cpp#L55</a></p>

<p><strong>0x1c0168fd</strong></p>

<bitcoind><bitcoin-core>
How to Get Bits 0x1c0168fd?
DATSEC
# How to Get Bits 0x1c0168fd?

(Asked by: username_0 on 2017-03-30)

How can I get this value, as I understand this complexity

<https://github.com/bitcoin/bitcoin/blob/master/src/test/pow_tests.cpp#L55>

**0x1c0168fd**

**Tags:** bitcoind, bitcoin-core


---

## 1 answers

### Answer by username_1 on 2017-03-31. Score: 2 | Accepted answer

Here's the function that implements this. Each annotation refers to the line below.

---

```
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{

```

If we're on a network without retargeting, don't retarget.

```
    if (params.fPowNoRetargeting)
        return pindexLast->nBits;

```

Don't adjust up or down by more than 4x.

```
    // Limit adjustment step
    int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
    if (nActualTimespan < params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan > params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;

    // Retarget
    const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
    arith_uint256 bnNew;

```

Take the old nBits in compact form, and turn it into 256-bit form. I explain that in more detail here: [Difficulty target representation in bitcoin wiki](https://bitcoin.stackexchange.com/questions/48707/difficulty-target-representation-in-bitcoin-wiki/48708#48708)

```
    bnNew.SetCompact(pindexLast->nBits);

```

Multiply by the timespan (the time between the first block in the retargeting period, and the last block in the retargeting period.) Then, divide by the targeted timespan.

```
    bnNew *= nActualTimespan;
    bnNew /= params.nPowTargetTimespan;

```

If the result would be less than difficulty 1, change to difficulty 1 instead.

```
    if (bnNew > bnPowLimit)
        bnNew = bnPowLimit;

```

Re-encode as compact form for bignums. This is the reverse of `SetCompact(int32_t)`.

```
    return bnNew.GetCompact();
}

```

---

Let's also work through the example you give. 0x1c05a3f4 should change to 0x1c0168fd.

```
0x1c05a3f4
1c          05a3f4
^ exponent  ^ mantissa

```

To decimal:

```
369652 * 256^28

```

Let's work out the timespans, actual and targeted

```
Actual: 1279297671 - 1279008237 = 289434
Target: 2016*600 = 1209600
Actual/Target ~= 0.239

```

Note that Target is more than 4x larger than Actual. We need to cap this, so we don't raise the difficulty too fast.

```
Capped timespan: Target/4 = 302400

```

Final calculation:

```
369652 * 256^28 * (302400/1209600) = 92413 * 256^28

```

Back to hex:

```
1c          0168fd
^ exponent  ^ mantissa

```

Re-encode:

```
0x1c0168fd

```

...and that is in fact correct.


---

