github.com/decred/dcrlnd@v0.7.6/shachain/utils.go (about)

     1  package shachain
     2  
     3  // getBit returns bit on index at position.
     4  func getBit(index index, position uint8) uint8 {
     5  	return uint8((uint64(index) >> position) & 1)
     6  }
     7  
     8  func getPrefix(index index, position uint8) uint64 {
     9  	//	+ -------------------------- +
    10  	// 	| №  | value | mask | return |
    11  	//	+ -- + ----- + ---- + ------ +
    12  	//	| 63 |	 1   |  0   |	 0   |
    13  	//	| 62 |	 0   |  0   |	 0   |
    14  	//	| 61 |   1   |  0   |	 0   |
    15  	//		....
    16  	//	|  4 |	 1   |  0   |	 0   |
    17  	//	|  3 |   1   |  0   |	 0   |
    18  	//	|  2 |   1   |  1   |	 1   | <--- position
    19  	//	|  1 |   0   |  1   |	 0   |
    20  	//	|  0 |   1   |  1   |	 1   |
    21  	//	+ -- + ----- + ---- + ------ +
    22  
    23  	var zero uint64
    24  	mask := (zero - 1) - uint64((1<<position)-1)
    25  	return (uint64(index) & mask)
    26  }
    27  
    28  // countTrailingZeros counts number of trailing zero bits, this function is
    29  // used to determine the number of element bucket.
    30  func countTrailingZeros(index index) uint8 {
    31  	var zeros uint8
    32  	for ; zeros < maxHeight; zeros++ {
    33  		if getBit(index, zeros) != 0 {
    34  			break
    35  		}
    36  	}
    37  
    38  	return zeros
    39  }