Package bitmask ergänzt

test
Klaus Wendel 3 years ago
parent e8d7a62795
commit db43edf5ce

@ -0,0 +1,41 @@
package bitmask
import "unsafe"
// Sets the bit at pos in the integer n. pos = 0 is first.
func SetBit(n int, pos uint) int {
n |= (1 << pos)
return n
}
// Clears the bit at pos in n. pos = 0 is first.
func ClearBit(n int, pos uint) int {
mask := ^(1 << pos)
n &= mask
return n
}
// Check if bit at pos n is set. pos = 0 is first.
func HasBit(n int, pos uint) bool {
val := n & (1 << pos)
return (val > 0)
}
// Sets the bit at pos in the integer n. pos = 0 is first.
func Bit2Pos(n int) (pos uint) {
poss := uint(unsafe.Sizeof(^int(0)) * 8) //Number of Bytes
for p := uint(0); p < poss; p++ {
if n == 1<<p {
return p
}
}
panic("Bit-position not found")
}
// ... and reverse
func Pos2Bit(pos uint) (n int) {
n = 1 << pos
return
}

@ -211,27 +211,3 @@ func ByteCountBinary(b int64) string {
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
/*
// grazeStatusChannel liest einen Wert aus dem StatusChannel, setzt, löscht oder liest ihn. Danach wird der Channel wieder neu geschrieben, damit alle anderen Jobs, die am Channel lauschen, nicht ins Stocken geraten.
func grazeStatusChannel(sc *chan config.SPStype, mode string, offset uint) bool {
cvalue, cstatus := <-*sc
if cstatus {
switch strings.ToLower(mode) {
case "set":
*sc <- uint8(wrap7.SetBit(int(cvalue), offset))
return true // Eigentlich wurscht, was ich hier zurückgebe, aber im Zweifel "true" = gesetzt
case "clear":
*sc <- uint8(wrap7.ClearBit(int(cvalue), offset))
return false // Eigentlich wurscht, was ich hier zurückgebe, aber im Zweifel "false" = nicht gesetzt
default: //case "has":
*sc <- cvalue
return wrap7.HasBit(int(cvalue), offset)
}
} else {
_, fn, line, _ := runtime.Caller(2)
fmt.Printf("Kein statusChannel? Eigentlich hätte der Programmzeiger niemals hier sein dürfen. %s:%d\n", fn, line)
return false //FIXME: Evtl. wäre hier eine panic zu setzen
}
}
*/

Loading…
Cancel
Save