<p>How to calculate difficulty from Bits fields (which is stored in block).</p>

<ol>
<li>Convert value to hex: <code>bits=471067731 -&gt; 0x1c13ec53</code>  </li>
<li>Split hex value, take 8 high bits (<code>0x1c</code>), and 24 low (<code>0x13ec53</code>)</li>
<li>Use <a href="https://en.bitcoin.it/wiki/Difficulty#How_is_difficulty_stored_in_blocks.3F" rel="nofollow noreferrer">formula</a> to get 256-bit target value: <code>T = low * 2 ^ (8 * (high - 3))</code>. In our case it will be: <code>T = 0x13ec53 * 2 ^ (8 * (0x1c - 3)) = 0x0000000013ec5300000000000000000000000000000000000000000000000000</code></li>
<li>Divide highest possible target Tmax (<code>0x00000000ffff0000000000000000000000000000000000000000000000000000</code>) by our T. If we truncate zeros, it will be: <code>0x13ec53 / 0xffff00 = 12.849</code> </li>
</ol>

<hr>

<p>Network recalculates difficulty every 2016 blocks (2 weeks). This is how 12.85 is getting recalculated from block timestamps:</p>

<pre><code>block -  diff - time stamp
...
52415 -  7.82 (2010-04-21 21:50:53)
52416 - 11.46 (2010-04-21 21:52:52) &lt;- difficulty update
...
54430 - 11.46 (2010-05-04 09:39:19)
54431 - 11.46 (2010-05-04 09:40:03)
54432 - 12.85 (2010-05-04 09:46:16) &lt;- difficulty update
...
55798 - 12.85 (2010-05-14 16:21:05) &lt;- your block
</code></pre>

<p>When network recalculates difficulty, it takes time to mine the latest 2015 blocks (due to bug in code, it should take 2016). In our example, it would be:</p>

<pre><code>2010-05-04 09:40:03 - 2010-04-21 21:52:52 = 12.491 days
</code></pre>

<p>In normal scenario 2016 block should be mined in 14 days. So, new difficulty:</p>

<pre><code>11.46 * 14 / 12.491 = 12.844
</code></pre>

CC BY-SA 4.0
2018-05-28T11:11:25.333Z
Thanks for your help. But why we added 11.46 in our calculation? any reason for this? and why you added 12.491? what about the formula D = T(max) / T
Shubham
CC BY-SA 4.0
2018-05-28T11:23:04.887Z
11.46 is previous difficulty. To calculate new difficulty network takes previous one. You can't use Bits field directly and do division. It has internal format, You can read wiki for details: https://en.bitcoin.it/wiki/Difficulty "How is difficulty stored in blocks?"
Zergatul
CC BY-SA 4.0
2018-05-28T11:24:39.703Z
Please let me know what formula you have used to calculate the difficulty.
Shubham
CC BY-SA 4.0
2018-05-28T11:28:19.457Z
`new_diff = old_diff * ratio`
Zergatul
CC BY-SA 4.0
2018-05-28T14:54:35.793Z
I think the question is about how difficulty is calculated from bits, not about how the difficulty is actually determined from previous block timestamps.
Nate Eldredge
CC BY-SA 4.0
2018-05-28T16:06:57.863Z
@Shubham oh, sorry, I updated the answer.
Zergatul
CC BY-SA 4.0
2018-05-29T05:35:05.037Z
@Zergatul Thanks you so much for your great help. I really appreciate it.
Shubham
CC BY-SA 4.0
2018-05-29T06:07:12.830Z
@Zergatul Can you please let me know how to convert the '0x0000000013ec5300000000000000000000000000000000000000000000000000' floating point number into decimal. ? Any online conversion link would really help me.
Shubham
CC BY-SA 4.0
2018-05-29T07:36:33.060Z
@Shubham I prefer to use programming languages, for example in javascript: `0x13ec53 * Math.pow(2, 8 * (0x1c - 3))`. And result: `2.0981516864422112e+66`
Zergatul
CC BY-SA 4.0
2018-05-28T10:41:36.773Z
2019-06-21T15:58:02.397Z
2019-06-21T15:58:02.397Z
Zergatul
<p>I know that the formula for calculating the difficulty is below - </p>

<pre><code>Difficulty = T (max) / T
Where T (max) = 1d00ffff in decimal it is 486604799.
</code></pre>

<p><a href="https://i.sstatic.net/8Gv28.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8Gv28.png" alt="**enter image description here**"></a></p>

<p>As you can see in the above screenshot the difficulty of block '55798' is <strong>12.85</strong>. I'm trying to calculate this value using formula but getting the different result.</p>

<pre><code>D = T (max) / T
</code></pre>

<p>When I put the correct values in the formula, It becomes like - </p>

<pre><code>D = (486604799 / 471067731) = 1.03
</code></pre>

<p>I don't know why I'm getting the different difficulty. The correct value is 12.85. Can you please let me know the reason of this?</p>

<bitcoin-core><blockchain>
How does difficulty is defined for block '55798'?
Shubham
# How does difficulty is defined for block '55798'?

(Asked by: username_0 on 2018-05-28)

I know that the formula for calculating the difficulty is below - 
Difficulty = T (max) / T
Where T (max) = 1d00ffff in decimal it is 486604799.


As you can see in the above screenshot the difficulty of block '55798' is 12.85. I'm trying to calculate this value using formula but getting the different result.
D = T (max) / T

When I put the correct values in the formula, It becomes like - 
D = (486604799 / 471067731) = 1.03

I don't know why I'm getting the different difficulty. The correct value is 12.85. Can you please let me know the reason of this?

**Tags:** bitcoin-core, blockchain


---

## 1 answers

### Answer by username_1 on 2018-05-28. Score: 3 | Accepted answer

How to calculate difficulty from Bits fields (which is stored in block).

Convert value to hex: bits=471067731 -> 0x1c13ec53 
Split hex value, take 8 high bits (0x1c), and 24 low (0x13ec53)
Use formula to get 256-bit target value: T = low * 2 ^ (8 * (high - 3)). In our case it will be: T = 0x13ec53 * 2 ^ (8 * (0x1c - 3)) = 0x0000000013ec5300000000000000000000000000000000000000000000000000
Divide highest possible target Tmax (0x00000000ffff0000000000000000000000000000000000000000000000000000) by our T. If we truncate zeros, it will be: 0x13ec53 / 0xffff00 = 12.849 


Network recalculates difficulty every 2016 blocks (2 weeks). This is how 12.85 is getting recalculated from block timestamps:
block -  diff - time stamp
...
52415 -  7.82 (2010-04-21 21:50:53)
52416 - 11.46 (2010-04-21 21:52:52) <- difficulty update
...
54430 - 11.46 (2010-05-04 09:39:19)
54431 - 11.46 (2010-05-04 09:40:03)
54432 - 12.85 (2010-05-04 09:46:16) <- difficulty update
...
55798 - 12.85 (2010-05-14 16:21:05) <- your block

When network recalculates difficulty, it takes time to mine the latest 2015 blocks (due to bug in code, it should take 2016). In our example, it would be:
2010-05-04 09:40:03 - 2010-04-21 21:52:52 = 12.491 days

In normal scenario 2016 block should be mined in 14 days. So, new difficulty:
11.46 * 14 / 12.491 = 12.844

> **Comments:**
> **username_0**: Thanks for your help. But why we added 11.46 in our calculation? any reason for this? and why you added 12.491? what about the formula D = T(max) / T  
> **username_1**: 11.46 is previous difficulty. To calculate new difficulty network takes previous one. You can't use Bits field directly and do division. It has internal format, You can read wiki for details: https://en.bitcoin.it/wiki/Difficulty "How is difficulty stored in blocks?"  
> **username_0**: Please let me know what formula you have used to calculate the difficulty.  
> **username_1**: `new_diff = old_diff * ratio`  
> **username_2**: I think the question is about how difficulty is calculated from bits, not about how the difficulty is actually determined from previous block timestamps.  
> **username_1**: @username_0 oh, sorry, I updated the answer.  
> **username_0**: @username_1 Thanks you so much for your great help. I really appreciate it.  
> **username_0**: @username_1 Can you please let me know how to convert the '0x0000000013ec5300000000000000000000000000000000000000000000000000' floating point number into decimal. ? Any online conversion link would really help me.  
> **username_1**: @username_0 I prefer to use programming languages, for example in javascript: `0x13ec53 * Math.pow(2, 8 * (0x1c - 3))`. And result: `2.0981516864422112e+66`  

---

