凤凰求蛊
一种更通用的方法可以让您选择无序位,如下所示:// subset has to go from lowest to highestfunc bits(b uint, subset ...uint) (r uint) { i := uint(0) for _, v := range subset { if b&(1<<v) > 0 { r = r | 1<<uint(i) } i++ } return}func main() { fmt.Println(bits(0x38, 5, 6, 7), "x", 0x38>>5) fmt.Println(bits(0x38, 2, 4, 5)) fmt.Println(bits(0x38, 1, 2, 3), "x", (0x38>>1)&7)}