Skip to content

Instantly share code, notes, and snippets.

@ArseniySavin
Created November 20, 2023 13:03
Show Gist options
  • Save ArseniySavin/bcf10a7ee0470636067fcc03d980f047 to your computer and use it in GitHub Desktop.
Save ArseniySavin/bcf10a7ee0470636067fcc03d980f047 to your computer and use it in GitHub Desktop.
Bitmask example
package main
import "fmt"
func main() {
//arr := []int{1, 2, 3, 4}
arr := []string{"A", "B", "C", "D"}
n := len(arr)
for mask := 0; mask < (1 << n); mask++ {
fmt.Println("n =", n, "(1 << n) = ", (1 << n), " mask = ", mask)
for i := 0; i < n; i++ {
a := 1 << i
b := mask & a
fmt.Print(" i = ", i, " a = ", (1 << i), " b = ", (mask & a))
if b == 0 {
fmt.Print(" arr[i] = ", arr[i])
}
fmt.Print("\n")
}
fmt.Println("")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment