<p>The actual target (and thus difficulty) is determined by its compact 32-bit encoding.</p>

<p>After the computation you did above, you should round it to the nearest compactly-representable target (24-bit accuracy, multiple of 256). That is the target encoded inside blocks, and the one that matters.</p>

CC BY-SA 3.0
2014-02-23T19:52:51.543Z
2014-02-23T19:52:51.543Z
Pieter Wuille
<p>as pieter says, you need to convert the target into its "bits" value, which is a 4 byte compressed value for the target. the following python code converts from your original value in (7) into the value given in <a href="http://blockexplorer.com/block/000000004f2886a170adb7204cb0c7a824217dd24d11a74423d564c4e0904967" rel="nofollow noreferrer">blockexplorer.com</a>:</p>

<pre><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 = "00" if (hex2int(target_hex[: 2]) &gt; 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))

&gt;&gt;&gt; bits_bytes = target_int2bits(22791193517536179595645637622052884930882401463536451358196587084939)
&gt;&gt;&gt; bin2hex(bits_bytes)
'1d00d86a'
&gt;&gt;&gt; # this ^^ is the value in blockexplorer.com in brackets.
&gt;&gt;&gt; # display the "bits" as an integer:
&gt;&gt;&gt; bits2target_int(bits_bytes)
22791060871177364286867400663010583169263383106957897897309909286912L
&gt;&gt;&gt; # this ^^ is the value at the end of your answer.
</code></pre>

CC BY-SA 3.0
2017-07-06T01:54:37.823Z
bin2int is not defined here.
Philippe Remy
CC BY-SA 3.0
2017-07-06T09:09:55.453Z
@PhilippeRemy I have now added it
mulllhausen
CC BY-SA 3.0
2014-09-10T16:15:02.720Z
2017-07-06T09:09:39.170Z
2017-07-06T09:09:39.170Z
mulllhausen
<p>I'm trying to calculate the new target for Block 32256, but I'm not getting the expected result. Here's what I did:</p>

<ol>
<li>Timestamp of <a href="http://blockexplorer.com/block/00000000984f962134a7291e3693075ae03e521f0ee33378ec30a334d860034b" rel="nofollow noreferrer">the previous block (32255)</a> = <code>1262152739</code></li>
<li>Timestamp of <a href="http://blockexplorer.com/block/000000000fa8bfa0f0dd32f956b874b2c7f1772c5fbedcb1b35e03335c7fb0a8" rel="nofollow noreferrer">block (32255 - 2015 = 30240)</a> = <code>1261130161</code></li>
<li>Diff: <code>1262152739 - 1261130161 = 1022578</code></li>
<li>Note that there is no limit adjustment (1/2 weeks &lt; <code>1022578</code> &lt; 8 weeks)</li>
<li>Current target (0x1d00ffff) = <code>26959535291011309493156476344723991336010898738574164086137773096960</code></li>
<li>Multiply the difference with current target<br>
=  <code>1022578 * 26959535291011309493156476344723991336010898738574164086137773096960</code><br>
= <code>27568227678811762838892963267635169612395352810293691562874591737943162880</code></li>
<li>Divide by two weeks<br>
<code>27568227678811762838892963267635169612395352810293691562874591737943162880 / 1209600</code><br>
= <code>22791193517536179595645637622052884930882401463536451358196587084939</code></li>
</ol>

<p>From what I understand, this should be the new target for block 32256 and the following 2015 blocks. However, it isn't - the correct new target as given in blockexplorer is:</p>

<p><code>22791060871177364286867400663010583169263383106957897897309909286912</code></p>

<p>As you can see, my result was kind of in the ballpark, but not correct. What am I missing here?</p>

<p><strong>Related questions:</strong></p>

<ul>
<li><a href="https://bitcoin.stackexchange.com/questions/5838/how-is-difficulty-calculated">How is difficulty calculated?</a></li>
<li><a href="https://bitcoin.stackexchange.com/questions/2924/how-to-calculate-new-bits-value">How to calculate new “bits” value?</a></li>
</ul>

<protocol><difficulty>
How was the new target for block 32256 calculated?
kvikshaug
# How was the new target for block 32256 calculated?

(Asked by: username_0 on 2014-02-23)

I'm trying to calculate the new target for Block 32256, but I'm not getting the expected result. Here's what I did:

1. Timestamp of [the previous block (32255)](http://blockexplorer.com/block/00000000984f962134a7291e3693075ae03e521f0ee33378ec30a334d860034b) = `1262152739`
2. Timestamp of [block (32255 - 2015 = 30240)](http://blockexplorer.com/block/000000000fa8bfa0f0dd32f956b874b2c7f1772c5fbedcb1b35e03335c7fb0a8) = `1261130161`
3. Diff: `1262152739 - 1261130161 = 1022578`
4. Note that there is no limit adjustment (1/2 weeks < `1022578` < 8 weeks)
5. Current target (0x1d00ffff) = `26959535291011309493156476344723991336010898738574164086137773096960`
6. Multiply the difference with current target  
   = `1022578 * 26959535291011309493156476344723991336010898738574164086137773096960`  
   = `27568227678811762838892963267635169612395352810293691562874591737943162880`
7. Divide by two weeks  
   `27568227678811762838892963267635169612395352810293691562874591737943162880 / 1209600`  
   = `22791193517536179595645637622052884930882401463536451358196587084939`

From what I understand, this should be the new target for block 32256 and the following 2015 blocks. However, it isn't - the correct new target as given in blockexplorer is:

`22791060871177364286867400663010583169263383106957897897309909286912`

As you can see, my result was kind of in the ballpark, but not correct. What am I missing here?

**Related questions:**

* [How is difficulty calculated?](https://bitcoin.stackexchange.com/questions/5838/how-is-difficulty-calculated)
* [How to calculate new “bits” value?](https://bitcoin.stackexchange.com/questions/2924/how-to-calculate-new-bits-value)

**Tags:** protocol, difficulty


---

## 2 answers

### Answer by username_1 on 2014-02-23. Score: 3 | Accepted answer

The actual target (and thus difficulty) is determined by its compact 32-bit encoding.

After the computation you did above, you should round it to the nearest compactly-representable target (24-bit accuracy, multiple of 256). That is the target encoded inside blocks, and the one that matters.


---

### Answer by username_2 on 2014-09-10. Score: 0

as username_1 says, you need to convert the target into its "bits" value, which is a 4 byte compressed value for the target. the following python code converts from your original value in (7) into the value given in [blockexplorer.com](http://blockexplorer.com/block/000000004f2886a170adb7204cb0c7a824217dd24d11a74423d564c4e0904967):

```
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))

>>> bits_bytes = target_int2bits(22791193517536179595645637622052884930882401463536451358196587084939)
>>> bin2hex(bits_bytes)
'1d00d86a'
>>> # this ^^ is the value in blockexplorer.com in brackets.
>>> # display the "bits" as an integer:
>>> bits2target_int(bits_bytes)
22791060871177364286867400663010583169263383106957897897309909286912L
>>> # this ^^ is the value at the end of your answer.

```

> **Comments:**
> **username_3**: bin2int is not defined here.  
> **username_2**: @username_3 I have now added it  

---

