0x4a52466c696e74 1 year ago
parent
commit
ad032802f1
2 changed files with 20 additions and 9 deletions
  1. 17 6
      bufio/bits/tools.go
  2. 3 3
      bufio/bits/z_test.go

+ 17 - 6
bufio/bits/tools.go

@@ -1,10 +1,6 @@
 package bits
 
-import (
-	"fmt"
-)
-
-func SetBit(b byte, flag bool, offset byte) byte {
+func BitSet(b byte, flag bool, offset byte) byte {
 	if flag {
 		b |= 1 << offset
 	} else {
@@ -13,6 +9,21 @@ func SetBit(b byte, flag bool, offset byte) byte {
 	return b
 }
 
+func BitGet(b byte, offset byte) bool {
+	mask := byte(1 << byte(offset))
+	res := b & mask
+	return res > 0
+}
+
 func StringBits(b byte) string {
-	return fmt.Sprintf("%b", b)
+	res := ""
+	for i := 7; i >= 0; i-- {
+		mask := byte(1 << byte(i))
+		if b&mask > 0 {
+			res += "1"
+		} else {
+			res += "0"
+		}
+	}
+	return res
 }

+ 3 - 3
bufio/bits/z_test.go

@@ -4,7 +4,7 @@ import "testing"
 
 func TestBits(t *testing.T) {
 	var b byte
-	b = SetBit(b, true, 1)
-	b = SetBit(b, false, 1)
-	t.Log(StringBits(b))
+	b = BitSet(b, true, 1)
+	b = BitSet(b, true, 5)
+	t.Log(b, StringBits(b))
 }