bit: more efficient; debits
This commit is contained in:
parent
b3e1434b25
commit
0175d52eca
1 changed files with 29 additions and 0 deletions
29
bit/bit.go
29
bit/bit.go
|
@ -1,5 +1,7 @@
|
||||||
package bit
|
package bit
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type Bit byte
|
type Bit byte
|
||||||
|
|
||||||
func (b *Bit) Flip() {
|
func (b *Bit) Flip() {
|
||||||
|
@ -38,6 +40,33 @@ func (bits *Bits) Bytes() []byte {
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bits Bits) Debits() Debits {
|
||||||
|
var debits = make(Debits, (len(bits)+1)/2)
|
||||||
|
for i := 0; i < len(bits); i += 2 {
|
||||||
|
debits[i/2] = Debit((bits[i] << 1) | (bits[i+1]))
|
||||||
|
}
|
||||||
|
return debits
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bits Bits) Dump() string {
|
||||||
|
var (
|
||||||
|
s string
|
||||||
|
bytes = bits.Bytes()
|
||||||
|
)
|
||||||
|
|
||||||
|
for i, b := range bytes {
|
||||||
|
if i%7 == 0 {
|
||||||
|
if i != 0 {
|
||||||
|
s += "\n"
|
||||||
|
}
|
||||||
|
s += fmt.Sprintf("%08x ", i)
|
||||||
|
}
|
||||||
|
s += fmt.Sprintf("%08b ", b)
|
||||||
|
}
|
||||||
|
s += "\n"
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func (bits Bits) Equal(other Bits) bool {
|
func (bits Bits) Equal(other Bits) bool {
|
||||||
var l = bits.Len()
|
var l = bits.Len()
|
||||||
if l != other.Len() {
|
if l != other.Len() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue