Documentation
¶
Overview ¶
Package bit provides functions for bit.
Index ¶
- Variables
- func BitsToBytes(b []Bit, o binary.ByteOrder) []byte
- func GetBitAsByte(b []byte, off Offset, o binary.ByteOrder) (byte, error)
- func GetBitAsByteNotShift(b []byte, off Offset, o binary.ByteOrder) (byte, error)
- func GetBitsAsByte(bytes []byte, off Offset, bitSize uint64, o binary.ByteOrder) (ret []byte, err error)
- func Read(r io.Reader, order binary.ByteOrder, data interface{}) error
- func SetBit(b []byte, off Offset, val Bit, o binary.ByteOrder) error
- func SetBits(bytes []byte, off Offset, setBits []Bit, o binary.ByteOrder) error
- func SetBitsBitEndian(b []byte, off Offset, setBits []Bit, order binary.ByteOrder) error
- func SizeInByte(b []Bit) int
- func Write(w io.Writer, order binary.ByteOrder, input interface{}) error
- type Bit
- func BytesToBits(b []byte, bitSize uint64, o binary.ByteOrder) ([]Bit, error)
- func GetBit(b []byte, off Offset, o binary.ByteOrder) (Bit, error)
- func GetBits(bytes []byte, off Offset, bitSize uint64, o binary.ByteOrder) (ret []Bit, err error)
- func GetBitsBitEndian(b []byte, o Offset, bitSize uint64, order binary.ByteOrder) ([]Bit, error)
- func NewBits(size uint64, v Bit) []Bit
- type Offset
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrOutOfRange = errors.New("out of range")
)
Functions ¶
func BitsToBytes ¶
BitsToBytes converts the unit. bit -> byte.
func GetBitAsByte ¶
GetBitAsByte returns byte (1 or 0). GetBitAsByte reads b at Offset off, returns the bit.
func GetBitAsByteNotShift ¶
GetBitAsByteNotShift reads b at Offset off, returns the bit. Return value is not bit shifted.
Example ¶
b := []byte{0x00, 0x80} /* 1000_0000 0000_0000 in bit */ off := bit.Offset{Byte: 0, Bit: 15} ret, err := bit.GetBitAsByteNotShift(b, off, binary.LittleEndian) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("0x%x\n", ret)
Output: 0x80
func GetBitsAsByte ¶
func GetBitsAsByte(bytes []byte, off Offset, bitSize uint64, o binary.ByteOrder) (ret []byte, err error)
GetBitsAsByte returns byte slice. GetBitsAsByte reads bytes slice from Offset off. Read size is bitSize in bit.
Example ¶
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.GetBitsAsByte(b, off, 4, binary.LittleEndian) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("0x%x\n", ret)
Output: 0x0f
func Read ¶
Read reads structured binary data from i into data. Data must be a pointer to a fixed-size value. Not exported struct field is ignored.
Supports StructTag. `bit:"skip"` : ignore the field. Skip X bits which is the size of the field. It is useful for reserved field. `bit:"-"` : ignore the field. Offset is not changed.
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 ¶
b := []byte{0x00, 0x00} /* 0000_0000 0000_0000 in bit */ off := bit.Offset{Byte: 0, Bit: 15} val := bit.Bit(true) err := bit.SetBit(b, off, val, binary.LittleEndian) 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 ¶
b := []byte{0x00, 0x00} /* 0000_0000 0000_0000 in bit */ off := bit.Offset{Byte: 0, Bit: 8} val := []bit.Bit{false, false, false, true} /* 0000_1000 in bit */ err := bit.SetBits(b, off, val, binary.LittleEndian) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("0x%x\n", b)
Output: 0x0008
func SetBitsBitEndian ¶
SetBitsBitEndian sets bits in b. If order is LittleEndian, it is same as GetBits function. It respect bit order endianness when order is BigEndian.
func SizeInByte ¶
SizeInByte returns size of []Bit slice in byte. e.g. It returns len([]Bit) == 9.
Types ¶
type Bit ¶
type Bit bool
func BytesToBits ¶
BytesToBits returns Bit slices. bitSize is the size of Bit slice.
func GetBit ¶
GetBit returns 1 or 0. GetBit reads b at Offset off, returns the bit.
Example ¶
b := []byte{0x00, 0x80} /* 1000_0000 0000_0000 in bit */ off := bit.Offset{Byte: 0, Bit: 15} ret, err := bit.GetBit(b, off, binary.LittleEndian) if err != nil { fmt.Printf("error:%s\n", err) } fmt.Printf("%t\n", ret)
Output: true
func GetBits ¶
GetBits returns Bit slice. GetBits reads bytes slice from Offset off. Read size is bitSize in bit.
func GetBitsBitEndian ¶
GetBitsBitEndian returns Bit slice. If order is LittleEndian, it is same as GetBits function. It respect bit order endianness when order is BigEndian.
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.