Skip to content

Commit

Permalink
Merge pull request #19760 from JuliaLang/dbeach24-djcb/bugfix-16706-2
Browse files Browse the repository at this point in the history
Allow bitarray to handle shifts by a negative value.
  • Loading branch information
StefanKarpinski authored Jan 2, 2017
2 parents 1c605d2 + f041c62 commit 8f9036a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 8 additions & 3 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1423,23 +1423,28 @@ end

reverse(v::BitVector) = reverse!(copy(v))

function (<<)(B::BitVector, i::Int)

function (<<)(B::BitVector, i::UInt)
n = length(B)
i == 0 && return copy(B)
A = falses(n)
i < n && copy_chunks!(A.chunks, 1, B.chunks, i+1, n-i)
return A
end

function (>>>)(B::BitVector, i::Int)
function (>>>)(B::BitVector, i::UInt)
n = length(B)
i == 0 && return copy(B)
A = falses(n)
i < n && copy_chunks!(A.chunks, i+1, B.chunks, 1, n-i)
return A
end

(>>)(B::BitVector, i::Int) = B >>> i
(>>)(B::BitVector, i::Union{Int, UInt}) = B >>> i

# signed integer version of shift operators with handling of negative values
(<<)(B::BitVector, i::Int) = (i >=0 ? B << unsigned(i) : B >> unsigned(-i))
(>>>)(B::BitVector, i::Int) = (i >=0 ? B >> unsigned(i) : B << unsigned(-i))

"""
rol!(dest::BitVector, src::BitVector, i::Integer) -> BitVector
Expand Down
2 changes: 2 additions & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,8 @@ let b1 = bitrand(v1)
for m = [rand(1:v1)-1, 0, 1, 63, 64, 65, 191, 192, 193, v1-1]
@test isequal(b1 << m, [ b1[m+1:end]; falses(m) ])
@test isequal(b1 >>> m, [ falses(m); b1[1:end-m] ])
@test isequal(b1 << -m, b1 >> m)
@test isequal(b1 >>> -m, b1 << m)
@test isequal(rol(b1, m), [ b1[m+1:end]; b1[1:m] ])
@test isequal(ror(b1, m), [ b1[end-m+1:end]; b1[1:end-m] ])
@test isequal(ror(b1, m), rol(b1, -m))
Expand Down

0 comments on commit 8f9036a

Please sign in to comment.