<p>Here is the relevant code from main.cpp:</p>
<pre class="lang-cpp prettyprint-override"><code>static const int64 nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
static const int64 nTargetSpacing = 10 * 60;
static const int64 nInterval = nTargetTimespan / nTargetSpacing;

...

// Go back by what we want to be 14 days worth of blocks
const CBlockIndex* pindexFirst = pindexLast;
for (int i = 0; pindexFirst &amp;&amp; i &lt; nInterval-1; i++)
    pindexFirst = pindexFirst-&gt;pprev;
assert(pindexFirst);

// Limit adjustment step
int64 nActualTimespan = pindexLast-&gt;GetBlockTime() - pindexFirst-&gt;GetBlockTime();
printf(&quot; nActualTimespan = %&quot;PRI64d&quot; before bounds\n&quot;, nActualTimespan);
if (nActualTimespan &lt; nTargetTimespan/4)
    nActualTimespan = nTargetTimespan/4;
if (nActualTimespan &gt; nTargetTimespan*4)
    nActualTimespan = nTargetTimespan*4;

// Retarget
CBigNum bnNew;
bnNew.SetCompact(pindexLast-&gt;nBits);
bnNew *= nActualTimespan;
bnNew /= nTargetTimespan;

if (bnNew &gt; bnProofOfWorkLimit)
    bnNew = bnProofOfWorkLimit;

...

return bnNew.GetCompact();
</code></pre>
<p>So the important steps are:</p>
<ol>
<li>Translate the bits of block n+2015 to a BigNum target.</li>
<li>Calculate the timespan between blocks n and n+2015 (as an integer number of seconds).</li>
<li>Multiply the old target by the timespan.</li>
<li>Divide the result by the desired timespan 2 weeks = 1209600 seconds. This is integer arithmetic so it's rounded down. The result is the new target.</li>
<li>Convert the target to bits.</li>
</ol>
<p>The code for the CBigNum class is at bignum.h.</p>

CC BY-SA 4.0
2012-02-15T06:24:06.430Z
2023-08-15T19:46:45.673Z
2023-08-15T19:46:45.673Z
Meni Rosenfeld
<p>i use the following python code to convert back and forth between &quot;target&quot; and &quot;bits&quot;:</p>
<pre class="lang-py prettyprint-override"><code>import binascii

def target_int2bits(target):
    # comprehensive explanation here: bitcoin.stackexchange.com/a/2926/2116

    # get in base 256 as a hex string
    target_hex = int2hex(target)

    bits = &quot;00&quot; if (hex2int(target_hex[: 2]) &gt; 127) else &quot;&quot;
    bits += target_hex # append
    bits = hex2bin(bits)
    length = int2bin(len(bits), 1)

    # the bits value could be zero (0x00) so make sure it is at least 3 bytes
    bits += hex2bin(&quot;0000&quot;)

    # the bits value could be bigger than 3 bytes, so cut it down to size
    bits = bits[: 3]

    return length + bits

def bits2target_int(bits_bytes):
    exp = bin2int(bits_bytes[: 1]) # exponent is the first byte
    mult = bin2int(bits_bytes[1:]) # multiplier is all but the first byte
    return mult * (2 ** (8 * (exp - 3)))

def int2hex(intval):
    hex_str = hex(intval)[2:]
    if hex_str[-1] == &quot;L&quot;:
        hex_str = hex_str[: -1]
    if len(hex_str) % 2:
        hex_str = &quot;0&quot; + hex_str
    return hex_str

def hex2int(hex_str):
    return int(hex_str, 16)

def hex2bin(hex_str):
    return binascii.a2b_hex(hex_str)

def int2bin(val, pad_length = False):
    hexval = int2hex(val)
    if pad_length: # specified in bytes
        hexval = hexval.zfill(2 * pad_length)
    return hex2bin(hexval)

def bin2hex(binary):
    # convert raw binary data to a hex string. also accepts ascii chars (0 - 255)
    return binascii.b2a_hex(binary)

def bin2int(binary):
    return hex2int(bin2hex(binary))
</code></pre>

CC BY-SA 4.0
2018-05-12T17:59:28.827Z
Where is **def bin2int()** block in your code?
James Bond
CC BY-SA 4.0
2018-05-15T04:31:34.220Z
@JamesBond I just added it
mulllhausen
CC BY-SA 4.0
2014-09-10T16:20:01.623Z
2023-08-12T19:55:01.500Z
2023-08-12T19:55:01.500Z
mulllhausen
<h2>What does the <code>bits</code> field represent?</h2>

<p>First of all, we need to understand what the 'bits' field means.</p>

<p>Bits is in 'compact' format.  This is kind of like a floating point format, but it represents big integers rather than arbitrary real numbers.  The first byte indicates the number of bytes the represented number takes up, and the next one to three bytes give the most significant digits of the number.  If the 2nd byte has a value greater than 127 then the number is interpreted as being negative.</p>

<p>To convert a positive integer to 'compact' format, we:</p>

<ul>
<li>convert the integer into base 256.</li>
<li>if the first (most significant) digit is greater than 127 (0x7f), prepend a zero digit</li>
<li>the first byte of the 'compact' format is the number of digits in the above base 256 representation, including the prepended zero if it's present</li>
<li>the following three bytes are the first three digits of the above representation.  If less than three digits are present, then one or more of the last bytes of the compact representation will be zero.</li>
</ul>

<h3>Example 1 - Convert 1000 to 'compact' format</h3>

<p>For example, to represent 1000 in 'compact' format, we convert to base 256:</p>

<pre><code>1000 = (0x03)*256 + (0xe8)*1
</code></pre>

<p>So we have a 2 digit base 256 number:</p>

<pre><code>03 e8
</code></pre>

<p>The first digit is not greater than 0x7f, so we don't prepend a zero digit:</p>

<pre><code>03 e8
</code></pre>

<p>Then the compact representation becomes:</p>

<pre><code>02 03 e8 00
</code></pre>

<h3>Example 2 - Convert max target to 'compact' format</h3>

<p>The minimum difficulty has a target of 2^(256-32)-1.  Let's represent that in 'compact' format.  First we convert it to base 256:</p>

<pre><code>ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
</code></pre>

<p>That's 28 0xff digits.  The first digit is greater than 0x7f, so we prepend a zero digit:</p>

<pre><code>00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
</code></pre>

<p>Now it's 29 digits long.  hex(29) = 0x1d.  So the 'compact' representation of this is:</p>

<pre><code>1d 00 ff ff
</code></pre>

<p>Notice we've lost a lot of 'ff' digits there.  We've only kept 2 bytes of precision, what with the size byte and the prepended zero byte using up two of the four available bytes.  If we were to convert back from 'compact' format to see what number we've actually stored, we get:</p>

<pre><code>ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
</code></pre>

<p>which is in fact the maximum target used by Bitcoin.  This is what a difficulty of 1 sets the block hash target to be.</p>

<h2>How is the value of the <code>bits</code> field calculated?</h2>

<p>Now that we know what the <code>bits</code> field means, we can look at how its value is decided.  In the official client, the <code>bits</code> value is calculated by function <code>GetNextWorkRequired()</code> in <code>src/main.cpp</code>, which does the following:</p>

<ul>
<li>if we're working on a block that's a multiple of 2016 (every 2 weeks)
<ul>
<li>look at the timestamps on the last block, and the block 2015 blocks before it</li>
<li>calculate the difference in these two timestamps</li>
<li>if the difference is greater than 8 weeks, set it to 8 weeks; this prevents the difficulty decreasing by more than a factor of 4</li>
<li>if the difference is less than half a week, set it to half a week; this prevents the difficulty increasing by more than a factor of 4</li>
<li>multiply the difference by the current target (ie. the current <code>bits</code> converted from 'compact' representation to the target it represents)</li>
<li>divide the result by 2 weeks</li>
<li>if the result is greater than the maximum target (<code>2^(256-32)-1</code>), set it to the maximum target</li>
<li>convert the result to 'compact' form, and use that as the new <code>bits</code> value</li>
</ul></li>
<li>otherwise (we're working on a block that's NOT a multiple of 2016
<ul>
<li>if we're on testnet and it's later than 15 Feb 2012
<ul>
<li>if it's been more than 20 minutes since the last block was found
<ul>
<li>set <code>bits</code> to its highest possible value, <code>0x1d00ffff</code>, which represents a difficulty of 1; this is the '<code>special-min-difficulty</code> rule'</li>
</ul></li>
<li>otherwise
<ul>
<li>set <code>bits</code> to the same as in the last non-<code>special-min-difficulty</code> rule block</li>
</ul></li>
</ul></li>
<li>otherwise (we're not on testnet, or it's before 15 Feb 2012)
<ul>
<li>set <code>bits</code> to the same as in the last block</li>
</ul></li>
</ul></li>
</ul>

CC BY-SA 3.0
2013-03-01T13:02:22.763Z
One thing to add is that the target never changes +- a factor of 4 per change
makerofthings7
CC BY-SA 3.0
2013-03-01T19:58:26.667Z
@makerofthings7 that's covered by the "if the difference is greater than 8 weeks, set it to 8 weeks" and the following line.  I expanded those lines to make it clear what they're for.
Chris Moore
CC BY-SA 3.0
2013-03-01T20:19:43.827Z
Thank you & I see now, I'm still working on my C++ to English translation skills ;)
makerofthings7
CC BY-SA 3.0
2012-02-15T06:42:05.040Z
2^224 is not what you presented, did you mean 2^224-1? http://www.wolframalpha.com/input/?i=2%5E224+in+hex
ThePiachu
CC BY-SA 3.0
2012-02-15T06:45:39.573Z
Yes, I did.  Thanks!  I've fixed my answer accordingly.
Chris Moore
CC BY-SA 3.0
2012-02-15T06:18:39.663Z
2013-03-01T19:57:35.370Z
2013-03-01T19:57:35.370Z
Chris Moore
<p>Every 2016 blocks one needs to calculate new <a href="https://en.bitcoin.it/wiki/Target">bits</a> value. What is the formula to calculate it?</p>

CC BY-SA 3.0
2012-02-15T05:19:14.873Z
Are you asking how the difficulty is calculated, or how bits corresponds to difficulty?  Or both?
Chris Moore
CC BY-SA 3.0
2012-02-15T05:51:13.710Z
I understand how to convert bits->difficulty, I'm asking how difficulty is adjusted. I'm guessing given timestamp from block n and n+2015, there is a formula for it that also takes into account some specific rounding in order to fir the bits field in a block n+2016.
ThePiachu
CC BY-SA 3.0
2012-02-15T06:20:57.937Z
OK.  I hope my answer tells you what you wanted to know.  It's kind of long, but I wanted to explain the format as well as the details of how 'bits' is calculated.
Chris Moore
CC BY-SA 3.0
2012-02-15T06:23:23.470Z
It's interesting that you ask about this just as the rules are about to change.  After Feb 15th 2012, testnet gets easy after 20 minutes of fruitless block searching.
Chris Moore
<protocol><development><block><difficulty>
How to calculate new "bits" value?
ThePiachu
# How to calculate new "bits" value?

(Asked by: username_0 on 2012-02-14)

Every 2016 blocks one needs to calculate new [bits](https://en.bitcoin.it/wiki/Target) value. What is the formula to calculate it?

**Tags:** protocol, development, block, difficulty

> **Comments:**
> **username_1**: Are you asking how the difficulty is calculated, or how bits corresponds to difficulty?  Or both?  
> **username_0**: I understand how to convert bits->difficulty, I'm asking how difficulty is adjusted. I'm guessing given timestamp from block n and n+2015, there is a formula for it that also takes into account some specific rounding in order to fir the bits field in a block n+2016.  
> **username_1**: OK.  I hope my answer tells you what you wanted to know.  It's kind of long, but I wanted to explain the format as well as the details of how 'bits' is calculated.  
> **username_1**: It's interesting that you ask about this just as the rules are about to change.  After Feb 15th 2012, testnet gets easy after 20 minutes of fruitless block searching.  

---

## 3 answers

### Answer by username_1 on 2012-02-15. Score: 27 | Accepted answer

## What does the `bits` field represent?

First of all, we need to understand what the 'bits' field means.

Bits is in 'compact' format. This is kind of like a floating point format, but it represents big integers rather than arbitrary real numbers. The first byte indicates the number of bytes the represented number takes up, and the next one to three bytes give the most significant digits of the number. If the 2nd byte has a value greater than 127 then the number is interpreted as being negative.

To convert a positive integer to 'compact' format, we:

* convert the integer into base 256.
* if the first (most significant) digit is greater than 127 (0x7f), prepend a zero digit
* the first byte of the 'compact' format is the number of digits in the above base 256 representation, including the prepended zero if it's present
* the following three bytes are the first three digits of the above representation. If less than three digits are present, then one or more of the last bytes of the compact representation will be zero.

### Example 1 - Convert 1000 to 'compact' format

For example, to represent 1000 in 'compact' format, we convert to base 256:

```
1000 = (0x03)*256 + (0xe8)*1

```

So we have a 2 digit base 256 number:

```
03 e8

```

The first digit is not greater than 0x7f, so we don't prepend a zero digit:

```
03 e8

```

Then the compact representation becomes:

```
02 03 e8 00

```

### Example 2 - Convert max target to 'compact' format

The minimum difficulty has a target of 2^(256-32)-1. Let's represent that in 'compact' format. First we convert it to base 256:

```
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff

```

That's 28 0xff digits. The first digit is greater than 0x7f, so we prepend a zero digit:

```
00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff

```

Now it's 29 digits long. hex(29) = 0x1d. So the 'compact' representation of this is:

```
1d 00 ff ff

```

Notice we've lost a lot of 'ff' digits there. We've only kept 2 bytes of precision, what with the size byte and the prepended zero byte using up two of the four available bytes. If we were to convert back from 'compact' format to see what number we've actually stored, we get:

```
ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

```

which is in fact the maximum target used by Bitcoin. This is what a difficulty of 1 sets the block hash target to be.

## How is the value of the `bits` field calculated?

Now that we know what the `bits` field means, we can look at how its value is decided. In the official client, the `bits` value is calculated by function `GetNextWorkRequired()` in `src/main.cpp`, which does the following:

* if we're working on a block that's a multiple of 2016 (every 2 weeks)
  + look at the timestamps on the last block, and the block 2015 blocks before it
  + calculate the difference in these two timestamps
  + if the difference is greater than 8 weeks, set it to 8 weeks; this prevents the difficulty decreasing by more than a factor of 4
  + if the difference is less than half a week, set it to half a week; this prevents the difficulty increasing by more than a factor of 4
  + multiply the difference by the current target (ie. the current `bits` converted from 'compact' representation to the target it represents)
  + divide the result by 2 weeks
  + if the result is greater than the maximum target (`2^(256-32)-1`), set it to the maximum target
  + convert the result to 'compact' form, and use that as the new `bits` value
* otherwise (we're working on a block that's NOT a multiple of 2016
  + if we're on testnet and it's later than 15 Feb 2012
    - if it's been more than 20 minutes since the last block was found
      * set `bits` to its highest possible value, `0x1d00ffff`, which represents a difficulty of 1; this is the '`special-min-difficulty` rule'
    - otherwise
      * set `bits` to the same as in the last non-`special-min-difficulty` rule block
  + otherwise (we're not on testnet, or it's before 15 Feb 2012)
    - set `bits` to the same as in the last block

> **Comments:**
> **username_0**: 2^224 is not what you presented, did you mean 2^224-1? http://www.wolframalpha.com/input/?i=2%5E224+in+hex  
> **username_1**: Yes, I did.  Thanks!  I've fixed my answer accordingly.  
> **username_5**: One thing to add is that the target never changes +- a factor of 4 per change  
> **username_1**: @username_5 that's covered by the "if the difference is greater than 8 weeks, set it to 8 weeks" and the following line.  I expanded those lines to make it clear what they're for.  
> **username_5**: Thank you & I see now, I'm still working on my C++ to English translation skills ;)  

---

### Answer by username_2 on 2012-02-15. Score: 5

Here is the relevant code from main.cpp:

```cpp
static const int64 nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
static const int64 nTargetSpacing = 10 * 60;
static const int64 nInterval = nTargetTimespan / nTargetSpacing;

...

// Go back by what we want to be 14 days worth of blocks
const CBlockIndex* pindexFirst = pindexLast;
for (int i = 0; pindexFirst && i < nInterval-1; i++)
    pindexFirst = pindexFirst->pprev;
assert(pindexFirst);

// Limit adjustment step
int64 nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
if (nActualTimespan < nTargetTimespan/4)
    nActualTimespan = nTargetTimespan/4;
if (nActualTimespan > nTargetTimespan*4)
    nActualTimespan = nTargetTimespan*4;

// Retarget
CBigNum bnNew;
bnNew.SetCompact(pindexLast->nBits);
bnNew *= nActualTimespan;
bnNew /= nTargetTimespan;

if (bnNew > bnProofOfWorkLimit)
    bnNew = bnProofOfWorkLimit;

...

return bnNew.GetCompact();

```

So the important steps are:

1. Translate the bits of block n+2015 to a BigNum target.
2. Calculate the timespan between blocks n and n+2015 (as an integer number of seconds).
3. Multiply the old target by the timespan.
4. Divide the result by the desired timespan 2 weeks = 1209600 seconds. This is integer arithmetic so it's rounded down. The result is the new target.
5. Convert the target to bits.

The code for the CBigNum class is at bignum.h.


---

### Answer by username_3 on 2014-09-10. Score: 2

i use the following python code to convert back and forth between "target" and "bits":

```py
import binascii

def target_int2bits(target):
    # comprehensive explanation here: bitcoin.stackexchange.com/a/2926/2116

    # get in base 256 as a hex string
    target_hex = int2hex(target)

    bits = "00" if (hex2int(target_hex[: 2]) > 127) else ""
    bits += target_hex # append
    bits = hex2bin(bits)
    length = int2bin(len(bits), 1)

    # the bits value could be zero (0x00) so make sure it is at least 3 bytes
    bits += hex2bin("0000")

    # the bits value could be bigger than 3 bytes, so cut it down to size
    bits = bits[: 3]

    return length + bits

def bits2target_int(bits_bytes):
    exp = bin2int(bits_bytes[: 1]) # exponent is the first byte
    mult = bin2int(bits_bytes[1:]) # multiplier is all but the first byte
    return mult * (2 ** (8 * (exp - 3)))

def int2hex(intval):
    hex_str = hex(intval)[2:]
    if hex_str[-1] == "L":
        hex_str = hex_str[: -1]
    if len(hex_str) % 2:
        hex_str = "0" + hex_str
    return hex_str

def hex2int(hex_str):
    return int(hex_str, 16)

def hex2bin(hex_str):
    return binascii.a2b_hex(hex_str)

def int2bin(val, pad_length = False):
    hexval = int2hex(val)
    if pad_length: # specified in bytes
        hexval = hexval.zfill(2 * pad_length)
    return hex2bin(hexval)

def bin2hex(binary):
    # convert raw binary data to a hex string. also accepts ascii chars (0 - 255)
    return binascii.b2a_hex(binary)

def bin2int(binary):
    return hex2int(bin2hex(binary))

```

> **Comments:**
> **username_4**: Where is **def bin2int()** block in your code?  
> **username_3**: @username_4 I just added it  

---

