user54635: I have two values 
1. h = hash of the block (32 bytes long in memory representation - not hex)
2. bits (uint32_t)

To make the calculation easier we create a function difficulty such that:
auto d = difficulty(bits)

Now what is the best way to create such a difficulty function that, I believe should, return a 256 bit value?
Then I am thinking casting both h and d values into an array of type uint64_t and compare all 4 elements from msb (bit 255) to make sure the condition holds.
Is there a better way for comparing as well?

user5757: Bitcoin Core has its own arith_uint256 class for calculations on 256-bit unsigned integers.  Its code to convert "bits" (so-called compact format) to arith_uint256 can be found in arith_uint256::SetCompact in the file arith_uint256.cpp.  
So you could always just use its code directly.  This might be a good idea because if your version differs in any way from their behavior (e.g. rounding errors), you could disagree with Bitcoin Core on the validity of a block.  If your version is in a widely-used client, this would fork the block chain.

54635: <p>I have two values </p>

<pre><code>1. h = hash of the block (32 bytes long in memory representation - not hex)
2. bits (uint32_t)
</code></pre>

<p>To make the calculation easier we create a function difficulty such that:</p>

<pre><code>auto d = difficulty(bits)
</code></pre>

<p>Now what is the best way to create such a difficulty function that, I believe should, return a 256 bit value?</p>

<p>Then I am thinking casting both h and d values into an array of type uint64_t and compare all 4 elements from msb (bit 255) to make sure the condition holds.</p>

<p>Is there a better way for comparing as well?</p>
 5757: <p>Bitcoin Core has its own <code>arith_uint256</code> class for calculations on 256-bit unsigned integers.  Its code to convert "bits" (so-called compact format) to <code>arith_uint256</code> can be found in <code>arith_uint256::SetCompact</code> in the file <a href="https://github.com/bitcoin/bitcoin/blob/master/src/arith_uint256.cpp" rel="nofollow noreferrer"><code>arith_uint256.cpp</code></a>.  </p>

<p>So you could always just use its code directly.  This might be a good idea because if your version differs in any way from their behavior (e.g. rounding errors), you could disagree with Bitcoin Core on the validity of a block.  If your version is in a widely-used client, this would fork the block chain.</p>
