Skip to content

Instantly share code, notes, and snippets.

@rikkix
Created October 26, 2019 11:56
Show Gist options
  • Save rikkix/acbb8e63ab24964b0d7fe76ddd85787c to your computer and use it in GitHub Desktop.
Save rikkix/acbb8e63ab24964b0d7fe76ddd85787c to your computer and use it in GitHub Desktop.
Octal to binary
package main
import "fmt"
func main() {
fmt.Println(f(1)) // [1 0 0 0 0 0 0 0 0 0]
fmt.Println(f(7)) // [1 1 1 0 0 0 0 0 0 0]
fmt.Println(f(8)) // [0 0 0 1 0 0 0 0 0 0]
fmt.Println(f(255)) // [1 1 1 1 1 1 1 1 0 0]
fmt.Println(f(1023)) // [1 1 1 1 1 1 1 1 1 1]
}
func f(val uint) []int {
result := make([]int, 10)
for i := 0; i < 10; i++ {
r := val & (1 << uint(i))
if r == 0 {
result[i] = 0
continue
}
result[i] = 1
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment