Documentation ¶
Overview ¶
Package bit provides functions for bit.
Index ¶
- func GetBit(b []byte, off Offset) (byte, error)
- func GetBitNotShift(b []byte, off Offset) (byte, error)
- func GetBits(bytes []byte, off Offset, bitSize uint64) (ret []byte, err error)
- func SetBit(b []byte, off Offset, val byte) error
- func SetBits(bytes []byte, off Offset, bitSize uint64, setBits []byte) error
- type Offset
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetBit ¶
GetBit returns 1 or 0. GetBit reads b at Offset off, returns the bit.
Example ¶
package main import ( "fmt" "github.com/nokute78/go-bit/pkg/bit" ) func main() { b := []byte{0x00, 0x80} /* 1000_0000 0000_0000 in bit */ off := bit.Offset{Byte: 0, Bit: 15} ret, err := bit.GetBit(b, off) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("0x%x\n", ret) }
Output: 0x1
func GetBitNotShift ¶
GetBitNotShift reads b at Offset off, returns the bit. Return value is not bit shifted.
Example ¶
package main import ( "fmt" "github.com/nokute78/go-bit/pkg/bit" ) func main() { b := []byte{0x00, 0x80} /* 1000_0000 0000_0000 in bit */ off := bit.Offset{Byte: 0, Bit: 15} ret, err := bit.GetBitNotShift(b, off) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("0x%x\n", ret) }
Output: 0x80
func GetBits ¶
GetBits returns byte slice. GetBits reads bytes slice from Offset off. Read size is bitSize in bit.
Example ¶
package main import ( "fmt" "github.com/nokute78/go-bit/pkg/bit" ) func main() { b := []byte{0x78} /* 0111_1000 in bit */ /* try to get 4bits(1111b) from 0111_1000 */ off := bit.Offset{Byte: 0, Bit: 3} ret, err := bit.GetBits(b, off, 4) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("0x%x\n", ret) }
Output: 0x0f
func SetBit ¶
SetBit sets bit on b at off. Bit is 0 if val == 0, 1 if val > 0. SetBit returns error if error occurred.
Example ¶
package main import ( "fmt" "github.com/nokute78/go-bit/pkg/bit" ) func main() { b := []byte{0x00, 0x00} /* 0000_0000 0000_0000 in bit */ off := bit.Offset{Byte: 0, Bit: 15} val := byte(0x01) err := bit.SetBit(b, off, val) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("0x%x\n", b) }
Output: 0x0080
func SetBits ¶
SetBits sets bits on bytes at off. The length to set is bitSize. SetBits returns error if error occurred.
Example ¶
package main import ( "fmt" "github.com/nokute78/go-bit/pkg/bit" ) func main() { b := []byte{0x00, 0x00} /* 0000_0000 0000_0000 in bit */ off := bit.Offset{Byte: 0, Bit: 8} val := []byte{0x08} /* 0000_1000 in bit */ err := bit.SetBits(b, off, 4, val) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("0x%x\n", b) }
Output: 0x0008
Types ¶
type Offset ¶
Offset represents offset to access bits in byte slices.
func (Offset) Compare ¶
Compare returns an integer comparing two Offsets. The result will be 0 if off==b, -1 if off < b, and +1 if off > b.
func (*Offset) Normalize ¶
func (off *Offset) Normalize()
Normalize updates off.Byte if off.Bit >= 8. e.g. Offset{Byte: 3, Bit: 53} -> Offset{Byte: 9, Bit: 5}
func (Offset) OffsetInBit ¶
OffsetInBit returns offset in bit. e.g. Offset{Byte:3, Bit:2} -> 26.