Created
July 19, 2023 15:11
-
-
Save trappmartin/4841e63d1d1645047563c99d0e02e3b3 to your computer and use it in GitHub Desktop.
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
julia> function decToBin(n) | |
if n > 0 | |
x = decToBin(n ÷ 2) | |
return x === nothing ? [n % 2] : append!(x, n % 2) | |
end | |
end | |
decToBin (generic function with 1 method) | |
julia> decToBin(4) | |
3-element Vector{Int64}: | |
1 | |
0 | |
0 | |
# adding padding | |
julia> function decToBin(n, p) | |
if n > 0 | |
x = decToBin(n ÷ 2, p-1) | |
return x === nothing ? append!(zeros(p-1), n % 2) : append!(x, n % 2) | |
end | |
end | |
decToBin (generic function with 2 methods) | |
julia> decToBin(3, 3) | |
3-element Vector{Float64}: | |
0.0 | |
1.0 | |
1.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment