Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bitarray to handle shifts by a negative value. #19760

Merged
merged 1 commit into from
Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1481,23 +1481,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 @@ -1069,6 +1069,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