bit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package bit provides functions for bit.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBit

func GetBit(b []byte, off Offset) (byte, error)

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

func GetBitNotShift(b []byte, off Offset) (byte, error)

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

func GetBits(bytes []byte, off Offset, bitSize uint64) (ret []byte, err error)

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

func SetBit(b []byte, off Offset, val byte) 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
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

func SetBits(bytes []byte, off Offset, bitSize uint64, setBits []byte) error

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

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

func (off Offset) OffsetInBit() uint64

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

func (Offset) SubOffset

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

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

Jump to

Keyboard shortcuts

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