package main import d285fe0f "math/rand" func main() { d285fe0f.Seed(0x4f7b77c8) const N = 1 << 7 for n := N; n > 0; n-- { var ( in = d285fe0f.Uint64() eval = bitSevenIsSet(in) good = d4159d2e(in) ) if eval != good { panic(nil) } } } func d4159d2e(in uint64) bool { const mask = 1 << 7 return in&mask != 0 } // TITLE // Confused By Operator Precedence // INTRO // Bit 0 is the least significant bit. // We are testing bit 7: Is it 1? // CORRECT // Operators & and << have the same precedence. // They associate from left to right. // The code was testing bit 0. // INCORRECT // Does the shift construct the mask? // EDIT func bitSevenIsSet(in uint64) bool { return in&1<<7 != 0 }