<h2>How to calculate the target from bits</h2>

<p>Let's start with a block-header, always 80-bytes that looks like this:</p>

<p><code>04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b577627061880564bec</code></p>

<p>From the 80-bytes, the bits are actually the 72nd to 76th byte:</p>

<p><code>04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b57**76270618**80564bec</code></p>

<p>or</p>

<p><code>76270618</code></p>

<p>This number, however, is in little-endian, so we have to reverse the bytes:</p>

<p><code>18062776</code></p>

<p>The first byte is the "exponent"</p>

<p><code>e = 0x18</code></p>

<p>The next 3 bytes are the "coefficient"</p>

<p><code>c = 0x062776</code></p>

<p>You plug this into a formula:</p>

<p><code>target = c * 2**(8*(e - 3))</code></p>

<p>In our case, that is:</p>

<p><code>target = 0x062776 * 2**(8*(0x18 - 3))</code></p>

<p>Which turns out to be:</p>

<p><code>0000000000000000062776000000000000000000000000000000000000000000</code></p>

<p>Let's calculate the hash of this block header using Python 2:</p>

<pre><code>from hashlib import sha256
header = "04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b577627061880564bec".decode('hex')
print sha256(sha256(header).digest()).digest()[::-1].encode('hex')
</code></pre>

<p>The output is</p>

<pre><code>0000000000000000040199a6c7b922f711ee7e98cd58863b8b981b02d2b83e13
</code></pre>

<p>You can compare this to the target</p>

<pre><code>&gt;&gt;&gt; 0x0000000000000000040199a6c7b922f711ee7e98cd58863b8b981b02d2b83e13 &lt; 0x0000000000000000062776000000000000000000000000000000000000000000 
True
</code></pre>

<p>That's how we know a block satisfies the proof-of-work.</p>

CC BY-SA 4.0
2018-06-25T17:09:44.233Z
Could difficulty be argued to be a percent then? i.e. d = (target hash)/(most difficult target hash)
Josh
CC BY-SA 4.0
2022-10-04T22:28:42.283Z
In the last line of code the comparison shouldn't be `<=` instead of `<`?
smartmouse
CC BY-SA 3.0
2016-06-01T19:51:21.327Z
2016-06-01T19:51:21.327Z
Jimmy Song
<p>I'm trying to wrap my head around the mining process by doing a small example of block hashing. </p>

<p>According to the <a href="https://en.bitcoin.it/wiki/Difficulty" rel="noreferrer">Wiki entry</a> about difficulty, the target for a block hash can be read from the "bits" part of the header as follows:
In this example, the bits part is <code>535f0119</code>.</p>

<pre><code>535f0119 * 2**(8*(0x1b - 3))
</code></pre>

<p>My resulting target would be: </p>

<pre><code>535f0119000000000000000000000000000000000000000000000000
</code></pre>

<p>The target in decimal would be: </p>

<pre><code>8780002705592212783085671453687210878315895819816253650256038723584
</code></pre>

<p>Let's say the hash I got with my current nonce is</p>

<pre><code>4d47599dd86834282a8ae6f20ba454704ddbe6eb23aa31b9fdec97fc7679b559
</code></pre>

<p>How can I now compare if the hash is smaller than the target? What do I have to do with the hash to be able to say "hash &lt; target"?</p>

<mining-theory><hash><difficulty>
How is a block header hash compared to the target (bits)?
NthDegree
# How is a block header hash compared to the target (bits)?

(Asked by: username_0 on 2016-06-01)

I'm trying to wrap my head around the mining process by doing a small example of block hashing.

According to the [Wiki entry](https://en.bitcoin.it/wiki/Difficulty) about difficulty, the target for a block hash can be read from the "bits" part of the header as follows:
In this example, the bits part is `535f0119`.

```
535f0119 * 2**(8*(0x1b - 3))

```

My resulting target would be:

```
535f0119000000000000000000000000000000000000000000000000

```

The target in decimal would be:

```
8780002705592212783085671453687210878315895819816253650256038723584

```

Let's say the hash I got with my current nonce is

```
4d47599dd86834282a8ae6f20ba454704ddbe6eb23aa31b9fdec97fc7679b559

```

How can I now compare if the hash is smaller than the target? What do I have to do with the hash to be able to say "hash < target"?

**Tags:** mining-theory, hash, difficulty


---

## 1 answers

### Answer by username_1 on 2016-06-01. Score: 16 | Accepted answer

## How to calculate the target from bits

Let's start with a block-header, always 80-bytes that looks like this:

`04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b577627061880564bec`

From the 80-bytes, the bits are actually the 72nd to 76th byte:

`04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b57**76270618**80564bec`

or

`76270618`

This number, however, is in little-endian, so we have to reverse the bytes:

`18062776`

The first byte is the "exponent"

`e = 0x18`

The next 3 bytes are the "coefficient"

`c = 0x062776`

You plug this into a formula:

`target = c * 2**(8*(e - 3))`

In our case, that is:

`target = 0x062776 * 2**(8*(0x18 - 3))`

Which turns out to be:

`0000000000000000062776000000000000000000000000000000000000000000`

Let's calculate the hash of this block header using Python 2:

```
from hashlib import sha256
header = "04000000b9e2784a84e5d2468cee60ad14e08d0fee5dda49a37148040000000000000000e9dd2b13157508891880ef68729a1e5ecdde58062ebfa214a89f0141e5a4717faefd2b577627061880564bec".decode('hex')
print sha256(sha256(header).digest()).digest()[::-1].encode('hex')

```

The output is

```
0000000000000000040199a6c7b922f711ee7e98cd58863b8b981b02d2b83e13

```

You can compare this to the target

```
>>> 0x0000000000000000040199a6c7b922f711ee7e98cd58863b8b981b02d2b83e13 < 0x0000000000000000062776000000000000000000000000000000000000000000 
True

```

That's how we know a block satisfies the proof-of-work.

> **Comments:**
> **username_2**: Could difficulty be argued to be a percent then? i.e. d = (target hash)/(most difficult target hash)  
> **username_3**: In the last line of code the comparison shouldn't be `<=` instead of `<`?  

---

