A problem on difficulty equation
The code you're citing does not actually deal with difficulty. It deals with targets. The target is the maximum 256-bit value than the block hash is allowed to have.

The difficulty is defined as 2^208 * 65535 / target.

As for the book, that looks wrong indeed.