Created
December 27, 2019 04:33
-
-
Save r6m/19660cced6af62ad22101d4e43403585 to your computer and use it in GitHub Desktop.
golagn bit mask big int
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bits | |
import "math/big" | |
// Flag is an alias of big.Int | |
type Flag struct { | |
Bits *big.Int | |
} | |
// NewFlag returns a Flag with 0 bits | |
func NewFlag() *Flag { | |
return &Flag{ | |
Bits: big.NewInt(0), | |
} | |
} | |
// NewFlagFromInt returns a Flag with given int64 bits | |
// example: bits.NewFlagFromString(0) | |
func NewFlagFromInt(flags int64) *Flag { | |
return &Flag{ | |
Bits: big.NewInt(flags), | |
} | |
} | |
// NewFlagFromString returns a Flag with given string | |
// example: bits.NewFlagFromString("0") | |
func NewFlagFromString(flags string) *Flag { | |
bits, _ := new(big.Int).SetString(flags, 10) | |
return &Flag{ | |
Bits: bits, | |
} | |
} | |
// Set a flag (sets z = x | y) | |
func (f *Flag) Set(flag int64) { f.Bits = f.Bits.Or(f.Bits, big.NewInt(flag)) } | |
// Clear unsets a flag (sets z = x &^ y) | |
func (f *Flag) Clear(flag int64) { f.Bits = f.Bits.AndNot(f.Bits, big.NewInt(flag)) } | |
// Toggle a flag (sets z = x ^ y) | |
func (f *Flag) Toggle(flag int64) { f.Bits = f.Bits.Xor(f.Bits, big.NewInt(flag)) } | |
// Has check a flag (sets z = x & y) | |
func (f *Flag) Has(flag int64) bool { return f.Bits.And(f.Bits, big.NewInt(flag)).Int64() != 0 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment