user75243: When generating a new block, how is the nbits field in block header calculated? Is nbits calculated for every new block like:
nbits=nbits of last block * (timstamp of lastblock-timestamp of (lastblocknum-2016))/20160 

Or, nbits is only updated every 2016 blocks like:
if(currentblocknum%2016==0)
    nbits=nbits of last block * (timstamp of lastblock-timestamp of (lastblocknum-2016))/20160 
else
    nbits=nbits of lastblock

And when a node receives a new block, it will calculate the correct nbits itself for the new block (instead of using the nbits in the received block) before verifying the hash result, right?
I have not read the source code of bitcoin, and am confused by the introduction of nbits in some books. 

user85663: Difficulty is only recalculated/updated once every 2016 blocks. 
The node itself should only calculate the difficulty once every 2016 blocks.

75243: <p>When generating a new block, how is the nbits field in block header calculated? Is nbits calculated for every new block like:</p>

<pre><code>nbits=nbits of last block * (timstamp of lastblock-timestamp of (lastblocknum-2016))/20160 
</code></pre>

<p>Or, nbits is only updated every 2016 blocks like:</p>

<pre><code>if(currentblocknum%2016==0)
    nbits=nbits of last block * (timstamp of lastblock-timestamp of (lastblocknum-2016))/20160 
else
    nbits=nbits of lastblock
</code></pre>

<p>And when a node receives a new block, it will calculate the correct nbits itself for the new block (instead of using the nbits in the received block) before verifying the hash result, right?</p>

<p>I have not read the source code of bitcoin, and am confused by the introduction of nbits in some books. </p>
 85663: <p>Difficulty is only recalculated/updated once every 2016 blocks. 
The node itself should only calculate the difficulty once every 2016 blocks.</p>
