Skip to content

Instantly share code, notes, and snippets.

@trappmartin
Created July 19, 2023 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trappmartin/4841e63d1d1645047563c99d0e02e3b3 to your computer and use it in GitHub Desktop.
Save trappmartin/4841e63d1d1645047563c99d0e02e3b3 to your computer and use it in GitHub Desktop.
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