I am reading a piece of code like this (taken from fsnotify):
type Op uint32
const (
Create Op = 1 << iota
Write
Remove
Rename
Chmod
)
...
func (op Op) String() string {
var buffer bytes.Buffer
if op&Create == Create {
buffer.WriteString("|CREATE")
}
if op&Remove == Remove {
buffer.WriteString("|REMOVE")
}
if op&Write == Write {
buffer.WriteString("|WRITE")
}
if op&Rename == Rename {
buffer.WriteString("|RENAME")
}
if op&Chmod == Chmod {
buffer.WriteString("|CHMOD")
}
if buffer.Len() == 0 {
return ""
}
return buffer.String()[1:]
}
My newbie question is why someone would use a bitwise AND operation like op&Remove == Remove
to actually make a comparison.
Why not just compare the op and (Create|Remove|...) values?