algo/go/45_bitmap/bitmap.go
2019-01-17 22:33:19 +08:00

30 lines
571 B
Go

package bitmap
// BitMap implement bitmap
type BitMap []byte
// New create BitMap
func New(length uint) BitMap {
return make([]byte, length/8+1)
}
// Set set value in bitmap
func (b BitMap) Set(value uint) {
byteIndex := value / 8
if byteIndex >= uint(len(b)) {
return
}
bitIndex := value % 8
[]byte(b)[byteIndex] |= 1 << bitIndex
}
// Get check whether value exist or not
func (b BitMap) Get(value uint) bool {
byteIndex := value / 8
if byteIndex >= uint(len(b)) {
return false
}
bitIndex := value % 8
return []byte(b)[byteIndex]&(1<<bitIndex) != 0
}