<p>When encoding from target to bits, in the custom uint8 encoding used in bitcoin, bit <code>0x00800000</code> denotes the sign.</p>
<p>So any value greater than <code>0x7fffff</code> would actually be considered a negative number.</p>
<p>Therefore, <code>0x00d86a</code> must be used when encoding the target to bits instead of <code>0xd86a52</code>.</p>
<hr />
<p>Pieter Wuille's answer here: <a href="https://bitcoin.stackexchange.com/questions/113535/why-1d00ffff-and-not-1cffffff-as-target-in-genesis-block">Why 1D00FFFF and not 1CFFFFFF as target in genesis block</a></p>
<p>I believe the actual code itself can be found in the <code>GetCompact()</code> method  in <a href="https://github.com/bitcoin/bitcoin/blob/master/src/arith_uint256.cpp" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/master/src/arith_uint256.cpp</a></p>

CC BY-SA 4.0
2022-05-08T19:59:49.717Z
2022-05-08T20:07:19.550Z
2022-05-08T20:07:19.550Z
inersha
<p>The target on the block at height <strong>32,255</strong> was:</p>
<pre><code>0x00000000ffff0000000000000000000000000000000000000000000000000000
</code></pre>
<p>After the adjustment, the target on the following block at height <strong>32,256</strong> was:</p>
<pre><code>0x00000000d86a0000000000000000000000000000000000000000000000000000
</code></pre>
<p>The time between block <strong>30,240</strong> and block <strong>30,255</strong> was <code>1022578</code> seconds. If we divide this by the expected time over 2016 blocks we get a ratio of:</p>
<pre><code>1022578 / 1209600 = 0.84538525132275
</code></pre>
<p>If we multiply the previous target by this ratio to get the new target, we get:</p>
<pre><code>0x00000000ffff0000000000000000000000000000000000000000000000000000 * 0.84538525132275 =
0x00000000d86a528bc8bc90000000000000000000000000000000000000000000
</code></pre>
<p>What are the rules for truncating this result to get the target as stored the in the block header?</p>
<p>I understand that the target is converted to bits and loses some precision, but why is it that we're only including the first two significant bytes of <code>d86a</code> when we could include the three bytes of <code>d86a52</code> in the bits field?</p>

<difficulty-retargetting>
Recalculating the target and truncating
inersha
# Recalculating the target and truncating

(Asked by: username_0 on 2022-05-08)

The target on the block at height **32,255** was:

```
0x00000000ffff0000000000000000000000000000000000000000000000000000

```

After the adjustment, the target on the following block at height **32,256** was:

```
0x00000000d86a0000000000000000000000000000000000000000000000000000

```

The time between block **30,240** and block **30,255** was `1022578` seconds. If we divide this by the expected time over 2016 blocks we get a ratio of:

```
1022578 / 1209600 = 0.84538525132275

```

If we multiply the previous target by this ratio to get the new target, we get:

```
0x00000000ffff0000000000000000000000000000000000000000000000000000 * 0.84538525132275 =
0x00000000d86a528bc8bc90000000000000000000000000000000000000000000

```

What are the rules for truncating this result to get the target as stored the in the block header?

I understand that the target is converted to bits and loses some precision, but why is it that we're only including the first two significant bytes of `d86a` when we could include the three bytes of `d86a52` in the bits field?

**Tags:** difficulty-retargetting


---

## 1 answers

### Answer by username_0 on 2022-05-08. Score: 2

When encoding from target to bits, in the custom uint8 encoding used in bitcoin, bit `0x00800000` denotes the sign.

So any value greater than `0x7fffff` would actually be considered a negative number.

Therefore, `0x00d86a` must be used when encoding the target to bits instead of `0xd86a52`.

---

Pieter Wuille's answer here: [Why 1D00FFFF and not 1CFFFFFF as target in genesis block](https://bitcoin.stackexchange.com/questions/113535/why-1d00ffff-and-not-1cffffff-as-target-in-genesis-block)

I believe the actual code itself can be found in the `GetCompact()` method in <https://github.com/bitcoin/bitcoin/blob/master/src/arith_uint256.cpp>


---

