Bitmask

You can express a field by using a bitmask pattern. In this pattern, we combine uint8 values. The bitwise operators are sufficient for implementing this pattern, but sometimes it's semantically nice to define methods on top of the simple binary "word".

type Bit uint8

func (u Bit) Or(v Bit) Bit {
    return u | v
}

func (u Bit) Xand(v Bit) Bit {
    return u &^ v
}

func (u Bit) Xor(v Bit) Bit {
    return u ^ v
}

func (u Bit) And(v Bit) Bit {
    return u & v
}

func (u Bit) Is(v Bit) bool {
    return u == v
}

func (u Bit) IsNot(v Bit) bool {
    return u != v
}

func (u Bit) Has(v Bit) Bit {
    return u.And(v).IsNot(0)
}

Orthogonal values come from creating an enumeration with the shift operator.

Last updated