How to generate a block header?
check this:

https://en.bitcoin.it/wiki/Block_hashing_algorithm

How is a block header hash compared to the target (bits)?

[EDIT]
Also this Calculate hash of block header