mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-14 19:29:32 +08:00
21 lines
473 B
Go
21 lines
473 B
Go
package bit
|
|
|
|
// Set is used to set a bit in the given integer at a given position to 1.
|
|
func Set(n int, pos uint8) int {
|
|
n |= (1 << pos)
|
|
return n
|
|
}
|
|
|
|
// Clear is used to set a bit in the given integer at a given position to 0.
|
|
func Clear(n int, pos uint8) int {
|
|
mask := ^int(1 << pos)
|
|
n &= mask
|
|
return n
|
|
}
|
|
|
|
// IsSet tests if the bit at the given position is set in the given integer.
|
|
func IsSet(n int, pos uint8) bool {
|
|
val := n & (1 << uint(pos))
|
|
return (val > 0)
|
|
}
|