diff --git a/base/bitarray.jl b/base/bitarray.jl index 3ec78b56003d2..89d2400273bdc 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -1481,7 +1481,8 @@ 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) @@ -1489,7 +1490,7 @@ function (<<)(B::BitVector, i::Int) return A end -function (>>>)(B::BitVector, i::Int) +function (>>>)(B::BitVector, i::UInt) n = length(B) i == 0 && return copy(B) A = falses(n) @@ -1497,7 +1498,11 @@ function (>>>)(B::BitVector, i::Int) 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 diff --git a/test/bitarray.jl b/test/bitarray.jl index 68e75746c3c26..8ca119fcc30af 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -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))