def bits_from_target(target)
      exponent = 3
      signed = (target < 0)
      target = -target if signed
      while target > 0x7fffff
        target >>= 8
        exponent += 1
      end
      # The 0x00800000 bit denotes the sign.
      # Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
      if (target & 0x00800000) > 0
        target >>= 8
        exponent += 1
      end
      result = (exponent << 24) + target
      result = result | 0x00800000 if signed
      result
    end
def bits_from_target(target)
      exponent = 3
      signed = (target < 0)
      target = -target if signed
      while target > 0x7fffff
        target >>= 8
        exponent += 1
      end
      # The 0x00800000 bit denotes the sign.
      # Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
      if (target & 0x00800000) > 0
        target >>= 8
        exponent += 1
      end
      result = (exponent << 24) + target
      result = result | 0x00800000 if signed
      result
    end
Note on Satoshi Compact format (used for 'bits' value).

 The "compact" format is a representation of a whole
 number N using an unsigned 32bit number similar to a
 floating point format.
 The most significant 8 bits are the unsigned exponent of base 256.
 This exponent can be thought of as "number of bytes of N".
 The lower 23 bits are the mantissa.
 Bit number 24 (0x800000) represents the sign of N.
 N = (-1^sign) * mantissa * 256^(exponent-3)

 Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
 MPI uses the most significant bit of the first byte as sign.
 Thus 0x1234560000 is compact (0x05123456)
 and  0xc0de000000 is compact (0x0600c0de)
 (0x05c0de00) would be -0x40de000000
 Converts 256-bit integer to 32-bit compact representation.