Skip to content

Commit

Permalink
Fast implementation of digits(::Integer, base = 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Apr 14, 2019
1 parent 1c88c0e commit a357d95
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,15 @@ function digits!(a::AbstractVector{T}, n::Integer; base::Integer = 10) where T<:
isempty(a) && return a

if base > 0
for i in eachindex(a)
n, d = divrem(n, base)
a[i] = d
if base == 2
for i in eachindex(a)
a[i] = (n >> (i - 1)) & 1
end
else
for i in eachindex(a)
n, d = divrem(n, base)
a[i] = d
end
end
else
# manually peel one loop iteration for type stability
Expand Down

0 comments on commit a357d95

Please sign in to comment.