Created
October 26, 2019 11:56
-
-
Save rikkix/acbb8e63ab24964b0d7fe76ddd85787c to your computer and use it in GitHub Desktop.
Octal to binary
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() { | |
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