Created
November 20, 2023 13:03
-
-
Save ArseniySavin/bcf10a7ee0470636067fcc03d980f047 to your computer and use it in GitHub Desktop.
Bitmask example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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