Skip to content
/ bitwise Public

Forget about which binary operator to use. Human friendly methods are here for you.

License

Notifications You must be signed in to change notification settings

hekmon/bitwise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

Forget about which binary operator to use. Human friendly methods are here for you.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages