what is bitmask check in Go
tags: learning go programming
content
- im seeing this code:
if segmentedLine.Flags&parser.Elided == parser.Elided {
break
}- type of
segmentedLineisGraphRowLine
type GraphRowLine struct {
// other fields
Flags RowLineFlags
}- and we have
type RowLineFlags int
const (
Revision RowLineFlags = 1 << iota
Highlightable
Elided
)- this involves:
if segmentedLine.Flags&parser.Elided == parser.Elided {
break
}-
in other words, this condition checks:
- does
segmented.Flagscontainparser.Elidedflag?
- does
-
in an more Go idiomatic way:
if segmentedLine.Flags & parser.Elided != 0 {
// do something
}- because every flag only has 1 bit activated,
- after
ANDoperation, the result could only be0orparser.Elided - if it’s
not zero, it meansFlagscontainsElidedflag
- after