bit

package module
v2.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package bit provides functions for bit.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrOutOfRange = errors.New("out of range")
)

Functions

func BitsToBytes

func BitsToBytes(b []Bit, o binary.ByteOrder) []byte

BitsToBytes converts the unit. bit -> byte.

func GetBitAsByte

func GetBitAsByte(b []byte, off Offset, o binary.ByteOrder) (byte, error)

GetBitAsByte returns byte (1 or 0). GetBitAsByte reads b at Offset off, returns the bit.

func GetBitAsByteNotShift

func GetBitAsByteNotShift(b []byte, off Offset, o binary.ByteOrder) (byte, error)

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

func Read(r io.Reader, order binary.ByteOrder, data interface{}) error

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

func SetBit(b []byte, off Offset, val Bit, o binary.ByteOrder) error

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

func SetBits(bytes []byte, off Offset, setBits []Bit, o binary.ByteOrder) error

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

func SetBitsBitEndian(b []byte, off Offset, setBits []Bit, order binary.ByteOrder) error

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

func SizeInByte(b []Bit) int

SizeInByte returns size of []Bit slice in byte. e.g. It returns len([]Bit) == 9.

func Write

func Write(w io.Writer, order binary.ByteOrder, input interface{}) error

Write writes structured binary data from input into w.

Types

type Bit

type Bit bool

func BytesToBits

func BytesToBits(b []byte, bitSize uint64, o binary.ByteOrder) ([]Bit, error)

BytesToBits returns Bit slices. bitSize is the size of Bit slice.

func GetBit

func GetBit(b []byte, off Offset, o binary.ByteOrder) (Bit, error)

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

func GetBits(bytes []byte, off Offset, bitSize uint64, o binary.ByteOrder) (ret []Bit, err error)

GetBits returns Bit slice. GetBits reads bytes slice from Offset off. Read size is bitSize in bit.

func GetBitsBitEndian

func GetBitsBitEndian(b []byte, o Offset, bitSize uint64, order binary.ByteOrder) ([]Bit, error)

GetBitsBitEndian returns Bit slice. If order is LittleEndian, it is same as GetBits function. It respect bit order endianness when order is BigEndian.

func NewBits

func NewBits(size uint64, v Bit) []Bit

NewBits generates slice of Bit.

func (Bit) String

func (b Bit) String() string

type Offset

type Offset struct {
	Byte uint64 /* Offset in byte. */
	Bit  uint64 /* Offset in bit.  */
}

Offset represents offset to access bits in byte slices.

func (Offset) AddOffset

func (off Offset) AddOffset(diff Offset) (Offset, error)

AddOffset adds diff and returns new Offset.

func (Offset) Bits

func (off Offset) Bits() uint64

Bits returns offset in bit. e.g. Offset{Byte:3, Bit:2} -> 26.

func (Offset) Compare

func (off Offset) Compare(b Offset) int

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) String

func (o Offset) String() string

func (Offset) SubOffset

func (off Offset) SubOffset(diff Offset) (Offset, error)

SubOffset subs diff and returns new Offset. diff must be larger then off.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL