github.com/suiyunonghen/DxCommonLib@v0.5.3/DxBits.go (about) 1 package DxCommonLib 2 3 import ( 4 "unsafe" 5 ) 6 7 //DxBits 用来存放位 8 type DxBits struct { 9 buffer []byte 10 fsize uint //存放的bit位数是多少 11 } 12 13 func (bt *DxBits) Count() uint { 14 return bt.fsize 15 } 16 17 //CheckedCount 选中的位的个数 18 func (bt *DxBits) CheckedCount() uint { 19 if bt.fsize == 0 { 20 return 0 21 } 22 result := uint(0) 23 for _, v := range bt.buffer { 24 for i := 0; i < 8; i++ { 25 realv := byte(1 << uint(i)) 26 if v&realv == realv { 27 result++ 28 } 29 } 30 } 31 return result 32 } 33 34 //ReSet 重置 35 func (bt *DxBits) ReSet(bsize uint) { 36 if bsize == 0 { 37 bt.buffer = nil 38 bt.fsize = 0 39 return 40 } 41 if bt.fsize == bsize { 42 return 43 } 44 bt.fsize = bsize 45 buflen := bt.fsize / 8 46 if bt.fsize%8 != 0 { 47 buflen += 1 48 } 49 bt.buffer = make([]byte, buflen) 50 } 51 52 func (bt *DxBits) Clear() { 53 for i := 0; i < len(bt.buffer); i++ { 54 bt.buffer[i] = 0 55 } 56 } 57 58 func (bt *DxBits) ReSetByInt32(v int32) { 59 bt.ReSet(32) 60 *(*int32)(unsafe.Pointer(&bt.buffer[0])) = v 61 } 62 63 func (bt *DxBits) ReSetByInt64(v int64) { 64 bt.ReSet(64) 65 *(*int64)(unsafe.Pointer(&bt.buffer[0])) = v 66 } 67 68 func (bt *DxBits) Bits(index uint) bool { 69 if index < bt.fsize { 70 btindex := index / 8 71 index = index % 8 72 realv := byte(1 << uint(index)) 73 return bt.buffer[btindex]&realv == realv 74 } 75 return false 76 } 77 78 func (bt *DxBits) SetBits(index uint, v bool) { 79 if index < bt.fsize { 80 btindex := index / 8 81 index = index % 8 82 if v { 83 realv := byte(1 << uint(index)) 84 bt.buffer[btindex] = bt.buffer[btindex] | realv 85 } else { 86 realv := byte(1 << uint(index)) 87 realv = ^realv 88 bt.buffer[btindex] = bt.buffer[btindex] & realv 89 } 90 } 91 } 92 93 // NotBits 将指定的位取反,如果指定的位为-1,则将全部的位各自取反,1变0,0变1 94 func (bt *DxBits) NotBits(index int) { 95 if index < 0 { 96 for idx, v := range bt.buffer { 97 for i := 0; i < 8; i++ { 98 realv := byte(1 << uint(i)) 99 if v&realv == realv { 100 realv = ^realv 101 v = v & realv 102 } else { 103 v = v | realv 104 } 105 } 106 bt.buffer[idx] = v 107 } 108 } else { 109 btindex := index / 8 110 index = index % 8 111 realv := byte(1 << uint(index)) 112 oldBitValid := bt.buffer[btindex]&realv == realv 113 if oldBitValid { 114 realv = ^realv 115 bt.buffer[btindex] = bt.buffer[btindex] & realv 116 } else { 117 bt.buffer[btindex] = bt.buffer[btindex] | realv 118 } 119 } 120 } 121 122 func (bt *DxBits) AsUInt64() uint64 { 123 bflen := len(bt.buffer) 124 if bflen > 0 { 125 result := uint(bt.buffer[0]) 126 for i := 1; i < bflen; i++ { 127 if i == 8 { 128 break 129 } 130 result = result | uint(bt.buffer[i])<<uint(8*i) 131 } 132 return uint64(result) 133 } 134 return 0 135 } 136 137 func (bt *DxBits) AsInt64() int64 { 138 return int64(bt.AsUInt64()) 139 } 140 141 func (bt *DxBits) AsUInt32() uint32 { 142 bflen := len(bt.buffer) 143 if bflen > 0 { 144 result := uint(bt.buffer[0]) 145 for i := 1; i < bflen; i++ { 146 if i == 4 { 147 break 148 } 149 result = result | uint(bt.buffer[i])<<uint(8*i) 150 } 151 return uint32(result) 152 } 153 return 0 154 } 155 156 func (bt *DxBits) AsInt32() int32 { 157 return int32(bt.AsUInt32()) 158 }