bitset

package module
v0.0.0-...-6754784 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2017 License: BSD-3-Clause Imports: 4 Imported by: 1

README

License BSD Go Report Card GoDoc Build Status

bitset

This package implements a bitset in Go. By a bitset, I mean a structure that encodes true/false values as 1/0 inside bytes. Eight boolean values should be accomodated by one byte using this structure.

To allow for large bitsets, the constructor asks for a number of bits you wish to contain, and then constructs a []byte with size suitable to hold this number.

Values are encoded in a little endian fashion: in a single bytes, bits would be indexes as 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0. Callers need not concern themselves with this implementation detail: getting, setting and unsetting bits is done through functions.

There are three main functions:

SetBitN(n) which sets the bit at position n to 1 (true)

UnsetBitN(n) which sets the bit at position n to 0 (false)

GetBitN(n) which gets the bit as position n and returns it as a bool (1 = true, 0 = false)

For those wishing to visualize the layout of bits, a convenience function called DumpBitSet is provided.

Documentation

Overview

Package bitset implements getting and setting of true/false values within bytes.

Index

Constants

View Source
const (
	WIDTH = 8
)

According to the Go spec, byte = uint8, so we set our byte width to 8 here

Variables

This section is empty.

Functions

This section is empty.

Types

type BitSet

type BitSet struct {
	// contains filtered or unexported fields
}

BitSet is simply an array of bytes large enough to contain the number of bits the user requests. Values are encoded in a little endian fashion: in a single bytes, bits would be indexes as 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0. Callers need not concern themselves with this implementation detail: getting, setting and unsetting bits is done through functions.

func New

func New(n uint32) *BitSet

New is an alias for NewBitSet.

func NewBitSet

func NewBitSet(n uint32) *BitSet

NewBitSet will create a new BitSet that can accomodate n bits.

func (*BitSet) DumpBitSet

func (bs *BitSet) DumpBitSet() string

DumpBitSet will return a string form of the BitSet.

func (*BitSet) GetBitN

func (bs *BitSet) GetBitN(n int) (bool, error)

GetBitN will read bit n as either true (1) or false (0). Error if n is not indexable in this BitSet.

func (*BitSet) SetBitN

func (bs *BitSet) SetBitN(n int) error

SetBitN will set bit n to be 1. Error if n is not indexable in this BitSet.

func (*BitSet) Size

func (bs *BitSet) Size() int

Size returns the size of the BitSet.

func (*BitSet) String

func (bs *BitSet) String() string

String satisifes the stringer interface.

func (*BitSet) UnsetBitN

func (bs *BitSet) UnsetBitN(n int) error

UnsetBitN will set bit n to be 0. Error if n is not indexable in this BitSet.

Jump to

Keyboard shortcuts

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