<p><code>nBits</code> is a compressed representation of the target value that the block's hash must be less than. Bitcoin does not actually care about difficulty. Difficulty is just for humans to think about how much work is actually being done. Bitcoin only cares about <code>nBits</code> and the target value it represents. A block's hash must be less than that target value. <code>nBits</code> is recalculated every time the difficulty changes. The calculation takes the previous target (previous <code>nBits</code>) and scales it by the ratio of the actual timespan it took for that difficulty period to the timespan that is targeted for a difficulty period.</p>
<p>What it code does is (in pseudocode):</p>
<pre><code>new_target = last_block.nBits.uncompress();
new_target *= actual_timespan;
new_target /= target_timespan;
this_block.nBits = new_target.compress();
</code></pre>
<p>So <code>nBits</code> starts at the value of the genesis block, and then scales up and down depending on how quickly blocks are being mined.</p>
<hr />
<p>The relationship between <code>nBits</code> and difficulty is inverted. As <code>nBits</code> represents a smaller and smaller target, the difficulty value goes up. The computation is essentially just the ratio between the current target value and the maximum target value, which also happens to be the target value found in the genesis block. The difficulty is the current target divided by the the maximum target.</p>

CC BY-SA 4.0
2021-02-10T19:00:47.040Z
So how did the Genesis Block gets its nBit value if it relies on the previous block? Is it similar to the previous hash in the Genesis Block where it was just given a random value?
L M 
CC BY-SA 4.0
2021-02-10T19:22:06.050Z
The genesis block is valid by definition; its contents does not matter.
Pieter Wuille
CC BY-SA 4.0
2023-08-06T22:07:57.647Z
"A block's hash must be less than that target value." *less than or equal to according to this... https://developer.bitcoin.org/reference/block_chain.html
Bradley Thomas
CC BY-SA 4.0
2021-02-10T17:31:37.600Z
2021-02-10T19:26:22.367Z
2021-02-10T19:26:22.367Z
Ava Chow
<p>So I've been trying to figure out how the nBits of a block is calculated, but I can't seem to come up with an answer. I know (think) that it's based off the difficulty of the block, but I'm still not sure exactly how it works. The Genesis Block for example has a difficulty of <strong>1.0</strong>, yet the nBits for that block was <strong>486,604,799 (0x1d00ffff)</strong>. Then if we look at the most recent block, it has a difficulty level of <strong>21,434,395,961,348.92</strong>, yet an nBits of <strong>386,736,569 (0x170d21b9)</strong>.</p>
<p>So my question is, how exactly is the nBits of a block calculated and what is its relation to the difficulty of a block?</p>
<p>Thanks</p>

CC BY-SA 4.0
2021-02-10T14:54:08.953Z
Does this answer your question? [What does the nBits value represent?](https://bitcoin.stackexchange.com/questions/57184/what-does-the-nbits-value-represent)
MCCCS
CC BY-SA 4.0
2021-02-10T15:17:57.857Z
@MCCCS I did look at that before, but I don't see where it says the formula for calculating the nBits of a Block? I was under the impression Difficulty and nBits were different.
L M 
CC BY-SA 4.0
2021-02-10T19:23:39.033Z
@LM No, they're arguably the same. The difficulty is just a human-friendly way of representing the same information. Target, nBits, and difficulty are all conceptually the same.
Pieter Wuille
<blockchain><block><difficulty>
How are the nBits of a block calculated?
L M 
# How are the nBits of a block calculated?

(Asked by: username_0 on 2021-02-10)

So I've been trying to figure out how the nBits of a block is calculated, but I can't seem to come up with an answer. I know (think) that it's based off the difficulty of the block, but I'm still not sure exactly how it works. The Genesis Block for example has a difficulty of **1.0**, yet the nBits for that block was **486,604,799 (0x1d00ffff)**. Then if we look at the most recent block, it has a difficulty level of **21,434,395,961,348.92**, yet an nBits of **386,736,569 (0x170d21b9)**.

So my question is, how exactly is the nBits of a block calculated and what is its relation to the difficulty of a block?

Thanks

**Tags:** blockchain, block, difficulty

> **Comments:**
> **username_1**: Does this answer your question? [What does the nBits value represent?](https://bitcoin.stackexchange.com/questions/57184/what-does-the-nbits-value-represent)  
> **username_0**: @username_1 I did look at that before, but I don't see where it says the formula for calculating the nBits of a Block? I was under the impression Difficulty and nBits were different.  
> **username_2**: @LM No, they're arguably the same. The difficulty is just a human-friendly way of representing the same information. Target, nBits, and difficulty are all conceptually the same.  

---

## 1 answers

### Answer by username_3 on 2021-02-10. Score: 5 | Accepted answer

`nBits` is a compressed representation of the target value that the block's hash must be less than. Bitcoin does not actually care about difficulty. Difficulty is just for humans to think about how much work is actually being done. Bitcoin only cares about `nBits` and the target value it represents. A block's hash must be less than that target value. `nBits` is recalculated every time the difficulty changes. The calculation takes the previous target (previous `nBits`) and scales it by the ratio of the actual timespan it took for that difficulty period to the timespan that is targeted for a difficulty period.

What it code does is (in pseudocode):

```
new_target = last_block.nBits.uncompress();
new_target *= actual_timespan;
new_target /= target_timespan;
this_block.nBits = new_target.compress();

```

So `nBits` starts at the value of the genesis block, and then scales up and down depending on how quickly blocks are being mined.

---

The relationship between `nBits` and difficulty is inverted. As `nBits` represents a smaller and smaller target, the difficulty value goes up. The computation is essentially just the ratio between the current target value and the maximum target value, which also happens to be the target value found in the genesis block. The difficulty is the current target divided by the the maximum target.

> **Comments:**
> **username_0**: So how did the Genesis Block gets its nBit value if it relies on the previous block? Is it similar to the previous hash in the Genesis Block where it was just given a random value?  
> **username_2**: The genesis block is valid by definition; its contents does not matter.  
> **username_4**: "A block's hash must be less than that target value." *less than or equal to according to this... https://developer.bitcoin.org/reference/block_chain.html  

---

