How to calculate new "bits" value? Every 2016 blocks one needs to calculate new bits value. What is the formula to calculate it?
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