bitwise

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2019 License: MIT Imports: 3 Imported by: 0

README

Bitwise

GoDoc

Example

Code
package main

import (
    "fmt"

    "github.com/hekmon/bitwise"
)

const (
    OptionA bitwise.Flags = 1 << iota
    OptionB
    OptionC
)

func main() {
    // Init
    fmt.Println("Initial state")
    var options bitwise.Flags
    fmt.Println("Pool:", options)
    fmt.Println("OptionA:", OptionA)
    fmt.Println("OptionB:", OptionB)
    fmt.Println("OptionC:", OptionC)
    fmt.Println()
    // Activate option B
    fmt.Println("Activate OptionB")
    options.SetFlag(OptionB)
    fmt.Println("Current pool", options)
    fmt.Println("Is OptionB on ?", options.IsFlagSet(OptionB))
    fmt.Println()
    // Toggle option A (from off to on)
    fmt.Println("Toggle OptionA")
    options.ToggleFlag(OptionA)
    fmt.Println("Current pool", options)
    fmt.Println()
    // Remove OptionB
    fmt.Println("Remove OptionB")
    options.UnsetFlag(OptionB)
    fmt.Println("Current pool", options)
    fmt.Println()
    // Something else
    fmt.Println("OptionA + OptionC debug print")
    fmt.Printf("%#v\n", OptionA|OptionC)
}
Output
Initial state
Pool: 0
OptionA: 1
OptionB: 10
OptionC: 100

Activate OptionB
Current pool 10
Is OptionB on ? true

Toggle OptionA
Current pool 11

Remove OptionB
Current pool 1

OptionA + OptionC debug print
0000000000000000000000000000000000000000000000000000000000000101(5)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flags

type Flags uint64

Flags represents a pool of settable flags (64)

func (Flags) Count

func (pool Flags) Count() int

Count returns the number of flags (bits) set in "pool" (Hamming weight)

func (Flags) GoString

func (pool Flags) GoString() string

GoString implements the stdlib GoStringer interface (use "%#v")

func (Flags) IsFlagSet

func (pool Flags) IsFlagSet(flag Flags) bool

IsFlagSet returns true if the flag "flag" is present within "pool" (AND)

func (*Flags) SetFlag

func (pool *Flags) SetFlag(flag Flags)

SetFlag activate "flag" within "pool" (OR)

func (Flags) String

func (pool Flags) String() string

String implements the stdlib Stringer interface

func (*Flags) ToggleFlag

func (pool *Flags) ToggleFlag(flag Flags)

ToggleFlag switch the state of "flag" within "pool" (XOR)

func (*Flags) UnsetFlag

func (pool *Flags) UnsetFlag(flag Flags)

UnsetFlag unset "flag" value within "pool" (AND NOT)

Jump to

Keyboard shortcuts

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