现在的位置: 首页 > 综合 > 正文

【比特币】大数的紧凑表示法

2019年05月15日 ⁄ 综合 ⁄ 共 1172字 ⁄ 字号 评论关闭
    // 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
    //
    // Bitcoin only uses this "compact" format for encoding difficulty
    // targets, which are unsigned 256bit quantities.  Thus, all the
    // complexities of the sign bit and using base 256 are probably an
    // implementation accident.

备注:

1) MSB 多字节整数的高字节位

>表示的数字大,更有意义的字节,知道这个字节,就大概知道了这个整数的大概范围,比如0x10aa, 知道10,大概知道这个数是4k以上的)

2) LSB 多字节整数的地字节位

>表示的数字小,不是很有意义的字节,知道这个字节,还是不知道这个整数到底有多大,比如0x10aa,知道aa,还是不知道这个数字有多大)

3) Big-endian:   一个大整数m的MSB存储于内存的低地址,而这个m的LSB存储于高地址

>因为存储于低地址的MSB字节在网络传输的时候先传送,所以叫big-endian,从大头开始)

4) Little-endian:一个大整数m的LSB存储于内存的低地址,而这个m的MSB存储于高地址

>因为存储于低地址的LSB字节在网络传输的时候先传送,所以叫little-endian,从小头开始)

参考:

http://zh.wikipedia.org/wiki/%E5%AD%97%E8%8A%82%E5%BA%8F

抱歉!评论已关闭.