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