Skip to content
/ bit Public

go module for basic bit operations on bitfields allowing various input type combinations

License

Notifications You must be signed in to change notification settings

valdotle/bit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub go.mod Go version of a Go module GoDoc reference example GoReportCard example GitHub license Github tag

Module bit comes with a variety of utility functions for use with bitfields.

The module offers functionality for use with go built-in types (string, int and big.Int) as well as aliases for these types to use methods instead.

The module exposes variations of the following functions:

    Has() bool // whether a bitfield includes the flag specified; always only returns a boolean
    Add()      // add a flag to a bitfield (set corresponding bit to 1 / true)
    Flip()     // flip a flag's state in a bitfield (flip corresponding bit)
    Remove()   // remove a flag from a bitfield (set corresponding bit to 0 / false)

Note that all functions' (except for Has()) return value matches the type of the bitfield. This means, that methods defined on an alias type bitfield return a bitfield of that alias type as well.

The combination of import path and function / method suffix determines the data type of the bitfield and flag. This is explained in more detail below. int is assumed as the default / most common bitfield type and thus doesn't need to be explicitly specified. Bitfields of type string and big.Int are intended to be used where integer limitations are exceeded and only then. Since no checks are performed whether a string or bigint passed actually has to be handled as such, all string and bigint related functions always use math/big's big.Int and thus have a significantly slower performance.

The import path determines the type of the bitfield:

    import (
        "github.com/valdotle/bit/field"                    // bitfield of type int - Note how it's not "bit/field/int"
        stringfield "github.com/valdotle/bit/field/string" // bitfield of type string
        "github.com/valdotle/bit/field/big"                // bitfield of type big.Int
    )

    flag := 2
    field.Has(9, flag)           // false

    stringfield.Has("9", flag)   // false

    big.Has(big.NewInt(9), flag) // false

Methods are defined at the parent folder of the corresponding function and are implemented as aliases:

    import (
        "github.com/valdotle/bit"       // methods for bitfield of type alias int
        "github.com/valdotle/bit/field" // functions for bitfield of type int
    )

    flag := 2
    field.Has(9, flag)     // false

    bit.Field(9).Has(flag) // false

Additionally, function and method name suffixes determine the flag's type:

    import stringfield "github.com/valdotle/bit/field/string"

    field := "9"

    stringfield.Has(field, 2)                // flag of type int

    stringfield.HasString(field, "2")        // flag of type string

    stringfield.HasBig(field, big.NewInt(2)) // flag of type big.Int

Note, that int bitfields don't allow checking against flags of type string or big.Int (meaning they don't expose HasString() nor HasBig()) as those are intended to be used where integer limitations are exceeded and as such can't be parsed as integers anymore.

About

go module for basic bit operations on bitfields allowing various input type combinations

Topics

Resources

License

Stars

Watchers

Forks

Languages