1 Star 1 Fork 0

zhuyuns / basic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bitset.go 986 Bytes
一键复制 编辑 原始数据 按行查看 历史
wing 提交于 2021-12-18 11:42 . 添加基础包
package bitset
type BitSet struct {
Size uint32
Bits []byte
}
func New(numBits uint32) *BitSet {
bs := &BitSet{}
byte_len := numBits/8 + 1
bs.Size = byte_len * 8
bs.Bits = make([]byte, byte_len)
return bs
}
//---------------------------------------------------------- set 1 to position [bit]
func (bs *BitSet) Set(bit uint32) {
if bit >= bs.Size {
return
}
n := bit / 8
off := bit % 8
bs.Bits[n] |= 128 >> off
}
//---------------------------------------------------------- set 0 to position [bit]
func (bs *BitSet) Unset(bit uint32) {
if bit >= bs.Size {
return
}
n := bit / 8
off := bit % 8
bs.Bits[n] &^= 128 >> off
}
//---------------------------------------------------------- test wheather a bit is set
func (bs *BitSet) Test(bit uint32) bool {
if bit >= bs.Size {
return false
}
n := bit / 8
off := bit % 8
if bs.Bits[n]&(128>>off) != 0 {
return true
} else {
return false
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zhuyuns/basic.git
git@gitee.com:zhuyuns/basic.git
zhuyuns
basic
basic
v0.0.23

搜索帮助

344bd9b3 5694891 D2dac590 5694891