go-位图

type BitMap struct {
    bytes []byte
    nbits int
}

func(bm *BitMap)set(k int){
    if k>bm.nbits {
        return
    }
    byteIndex := k/8 

//8 是因为go中byte占1个字节即8个位
    bitIndex := uint(k%8)
    bm.bytes[byteIndex] |= 1<<bitIndex
}
func(bm *BitMap)get(k int) bool{
    if k>bm.nbits {
        return false
    }
    byteIndex := k/8
    bitIndex := k%8
    return bm.bytes[byteIndex] & 1 << uint(bitIndex) !=0
}

猜你喜欢

转载自blog.csdn.net/u013755520/article/details/92079434
今日推荐