diff --git a/base/associative.jl b/base/associative.jl index b16521374c643..f09e247b9ec24 100644 --- a/base/associative.jl +++ b/base/associative.jl @@ -252,7 +252,7 @@ const hasha_seed = UInt === UInt64 ? 0x6d35bb51952d5539 : 0x952d5539 function hash(a::Associative, h::UInt) h = hash(hasha_seed, h) for (k,v) in a - h = xor(h, hash(k, hash(v))) + h = h ⊻ hash(k, hash(v)) end return h end diff --git a/base/bitarray.jl b/base/bitarray.jl index 883ef9ce27fe3..8b5315702c851 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -1888,7 +1888,7 @@ map!(f::typeof(max), dest::BitArray, A::BitArray, B::BitArray) = map!(|, dest, A map!(f::typeof(!=), dest::BitArray, A::BitArray, B::BitArray) = map!(xor, dest, A, B) map!(f::Union{typeof(>=), typeof(^)}, dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> p | ~q), dest, A, B) map!(f::typeof(<=), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~p | q), dest, A, B) -map!(f::typeof(==), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~xor(p, q)), dest, A, B) +map!(f::typeof(==), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~(p ⊻ q)), dest, A, B) map!(f::typeof(<), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~p & q), dest, A, B) map!(f::typeof(>), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> p & ~q), dest, A, B) diff --git a/base/broadcast.jl b/base/broadcast.jl index 51cb10545acba..03e2280994dee 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -478,9 +478,9 @@ function broadcast_bitarrays(scalarf, bitf, A::AbstractArray{Bool}, B::AbstractA return F end -biteq(a::UInt64, b::UInt64) = xor(~a, b) +biteq(a::UInt64, b::UInt64) = ~a ⊻ b bitlt(a::UInt64, b::UInt64) = ~a & b -bitneq(a::UInt64, b::UInt64) = xor(a, b) +bitneq(a::UInt64, b::UInt64) = a ⊻ b bitle(a::UInt64, b::UInt64) = ~a | b .==(A::AbstractArray{Bool}, B::AbstractArray{Bool}) = broadcast_bitarrays(==, biteq, A, B) diff --git a/base/char.jl b/base/char.jl index 82c88cdd88a69..79a1b43dec1e3 100644 --- a/base/char.jl +++ b/base/char.jl @@ -33,7 +33,7 @@ in(x::Char, y::Char) = x == y isless(x::Char, y::Char) = UInt32(x) < UInt32(y) const hashchar_seed = 0xd4d64234 -hash(x::Char, h::UInt) = hash_uint64(xor((UInt64(x)+hashchar_seed)<<32, UInt64(h))) +hash(x::Char, h::UInt) = hash_uint64(((UInt64(x)+hashchar_seed)<<32) ⊻ UInt64(h)) -(x::Char, y::Char) = Int(x) - Int(y) -(x::Char, y::Integer) = Char(Int32(x) - Int32(y)) diff --git a/base/combinatorics.jl b/base/combinatorics.jl index 103dc68a89144..fe2f327fb1311 100644 --- a/base/combinatorics.jl +++ b/base/combinatorics.jl @@ -73,7 +73,7 @@ function isperm(A) n = length(A) used = falses(n) for a in A - (0 < a <= n) && (used[a] = xor(used[a], true)) || return false + (0 < a <= n) && (used[a] = used[a] ⊻ true) || return false end true end diff --git a/base/dSFMT.jl b/base/dSFMT.jl index c65c699a5c346..ba70d4aa94e0f 100644 --- a/base/dSFMT.jl +++ b/base/dSFMT.jl @@ -115,19 +115,19 @@ function dsfmt_jump_add!(dest::Vector{UInt64}, src::Vector{UInt64}) while i <= N-diff j = i*2-1 p = j + diff*2 - dest[j] = xor(dest[j], src[p]) - dest[j+1] = xor(dest[j+1], src[p+1]) + dest[j] = dest[j] ⊻ src[p] + dest[j+1] = dest[j+1] ⊻ src[p+1] i += 1 end while i <= N j = i*2-1 p = j + (diff - N)*2 - dest[j] = xor(dest[j], src[p]) - dest[j+1] = xor(dest[j+1], src[p+1]) + dest[j] = dest[j] ⊻ src[p] + dest[j+1] = dest[j+1] ⊻ src[p+1] i += 1 end - dest[N*2+1] = xor(dest[N*2+1], src[N*2+1]) - dest[N*2+2] = xor(dest[N*2+2], src[N*2+2]) + dest[N*2+1] = dest[N*2+1] ⊻ src[N*2+1] + dest[N*2+2] = dest[N*2+2] ⊻ src[N*2+2] return dest end diff --git a/base/deprecated.jl b/base/deprecated.jl index 23c97490f2656..21cb728179ffc 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1022,6 +1022,10 @@ end)) @deprecate ipermutedims(A::AbstractArray,p) permutedims(A, invperm(p)) # 18696 -@deprecate ($) xor +function ($)(x, y) + depwarn("`x \$ y` is deprecated. use `xor(x, y)` or `x ⊻ y` instead.", :$) + xor(x, y) +end +export $ # End deprecations scheduled for 0.6 diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index ad8a4a87a31eb..d88e6df472f7c 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -903,6 +903,7 @@ exp10 """ &(x, y) + ∧(x, y) Bitwise and. @@ -3330,6 +3331,7 @@ Val """ |(x, y) + ∨(x, y) Bitwise or. @@ -3413,6 +3415,7 @@ dawson """ xor(x, y) + ⊻(x, y) Bitwise exclusive or. """ diff --git a/base/exports.jl b/base/exports.jl index b03b20ca7c263..f7040e3bf42da 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -207,9 +207,11 @@ export ≡, ≢, xor, + ⊻, %, ÷, &, + ∧, *, +, -, @@ -249,6 +251,7 @@ export \, ^, |, + ∨, |>, ~, :, diff --git a/base/float.jl b/base/float.jl index 38f4cbefec1ec..3a5480900eaf9 100644 --- a/base/float.jl +++ b/base/float.jl @@ -342,7 +342,7 @@ _default_type(T::Union{Type{Real},Type{AbstractFloat}}) = Float64 ## floating point arithmetic ## -(x::Float64) = box(Float64,neg_float(unbox(Float64,x))) -(x::Float32) = box(Float32,neg_float(unbox(Float32,x))) --(x::Float16) = reinterpret(Float16, xor(reinterpret(UInt16,x), 0x8000)) +-(x::Float16) = reinterpret(Float16, reinterpret(UInt16,x) ⊻ 0x8000) for op in (:+,:-,:*,:/,:\,:^) @eval ($op)(a::Float16, b::Float16) = Float16(($op)(Float32(a), Float32(b))) @@ -512,7 +512,7 @@ const hx_NaN = hx(UInt64(0), NaN, UInt(0 )) hash(x::UInt64, h::UInt) = hx(x, Float64(x), h) hash(x::Int64, h::UInt) = hx(reinterpret(UInt64,abs(x)), Float64(x), h) -hash(x::Float64, h::UInt) = isnan(x) ? xor(hx_NaN, h) : hx(box(UInt64,fptoui(unbox(Float64,abs(x)))), x, h) +hash(x::Float64, h::UInt) = isnan(x) ? (hx_NaN ⊻ h) : hx(box(UInt64,fptoui(unbox(Float64,abs(x)))), x, h) hash(x::Union{Bool,Int8,UInt8,Int16,UInt16,Int32,UInt32}, h::UInt) = hash(Int64(x), h) hash(x::Float32, h::UInt) = hash(Float64(x), h) @@ -558,7 +558,7 @@ function nextfloat(f::Union{Float16,Float32,Float64}, d::Integer) fu = fumax else du = da % U - if xor(fneg, dneg) + if fneg ⊻ dneg if du > fu fu = min(fumax, du - fu) fneg = !fneg diff --git a/base/hashing.jl b/base/hashing.jl index 0cda93a3f5ecd..8e83b21d8ecf3 100644 --- a/base/hashing.jl +++ b/base/hashing.jl @@ -14,11 +14,11 @@ hash(x::ANY, h::UInt) = 3*object_id(x) - h function hash_64_64(n::UInt64) local a::UInt64 = n a = ~a + a << 21 - a = xor(a, a >> 24) + a = a ⊻ a >> 24 a = a + a << 3 + a << 8 - a = xor(a, a >> 14) + a = a ⊻ a >> 14 a = a + a << 2 + a << 4 - a = xor(a, a >> 28) + a = a ⊻ a >> 28 a = a + a << 31 return a end @@ -26,22 +26,22 @@ end function hash_64_32(n::UInt64) local a::UInt64 = n a = ~a + a << 18 - a = xor(a, a >> 31) + a = a ⊻ a >> 31 a = a * 21 - a = xor(a, a >> 11) + a = a ⊻ a >> 11 a = a + a << 6 - a = xor(a, a >> 22) + a = a ⊻ a >> 22 return a % UInt32 end function hash_32_32(n::UInt32) local a::UInt32 = n a = a + 0x7ed55d16 + a << 12 - a = xor(a, 0xc761c23c, a >> 19) + a = a ⊻ 0xc761c23c ⊻ a >> 19 a = a + 0x165667b1 + a << 5 - a = a + xor(0xd3a2646c, a << 9) + a = a + 0xd3a2646c ⊻ a << 9 a = a + 0xfd7046c5 + a << 3 - a = xor(a, 0xb55a4f09, a >> 16) + a = a ⊻ 0xb55a4f09 ⊻ a >> 16 return a end diff --git a/base/hashing2.jl b/base/hashing2.jl index 35ba3a4590934..cc9eb315c7f17 100644 --- a/base/hashing2.jl +++ b/base/hashing2.jl @@ -3,11 +3,11 @@ ## efficient value-based hashing of integers ## function hash_integer(n::Integer, h::UInt) - h = xor(hash_uint(xor(n % UInt, h)), h) + h = hash_uint((n % UInt) ⊻ h) ⊻ h n = abs(n) n >>>= sizeof(UInt) << 3 while n != 0 - h = xor(hash_uint(xor(n % UInt, h)), h) + h = hash_uint((n % UInt) ⊻ h) ⊻ h n >>>= sizeof(UInt) << 3 end return h @@ -18,9 +18,9 @@ function hash_integer(n::BigInt, h::UInt) s == 0 && return hash_integer(0, h) p = convert(Ptr{UInt}, n.d) b = unsafe_load(p) - h = xor(hash_uint(xor(ifelse(s < 0, -b, b), h)), h) + h = hash_uint(ifelse(s < 0, -b, b) ⊻ h) ⊻ h for k = 2:abs(s) - h = xor(hash_uint(xor(unsafe_load(p, k), h)), h) + h = hash_uint(unsafe_load(p, k) ⊻ h) ⊻ h end return h end diff --git a/base/int.jl b/base/int.jl index 3d069c8fb81ef..3ee0b62e4b59f 100644 --- a/base/int.jl +++ b/base/int.jl @@ -76,7 +76,7 @@ flipsign(x::Signed, y::Float32) = flipsign(x, reinterpret(Int32,y)) flipsign(x::Signed, y::Float64) = flipsign(x, reinterpret(Int64,y)) flipsign(x::Signed, y::Real) = flipsign(x, -oftype(x,signbit(y))) -copysign(x::Signed, y::Signed) = flipsign(x, xor(x,y)) +copysign(x::Signed, y::Signed) = flipsign(x, x ⊻ y) copysign(x::Signed, y::Float16) = copysign(x, reinterpret(Int16,y)) copysign(x::Signed, y::Float32) = copysign(x, reinterpret(Int32,y)) copysign(x::Signed, y::Float64) = copysign(x, reinterpret(Int64,y)) @@ -149,7 +149,7 @@ rem{T<:BitUnsigned64}(x::T, y::T) = box(T,checked_urem_int(unbox(T,x),unbox(T,y) fld{T<:Unsigned}(x::T, y::T) = div(x,y) function fld{T<:Integer}(x::T, y::T) d = div(x,y) - d - (signbit(xor(x,y)) & (d*y!=x)) + d - (signbit(x ⊻ y) & (d*y!=x)) end # cld(x,y) = div(x,y) + ((x>0) == (y>0) && rem(x,y) != 0 ? 1 : 0) diff --git a/base/intset.jl b/base/intset.jl index 6cd1243600c11..189cbed9c9058 100644 --- a/base/intset.jl +++ b/base/intset.jl @@ -140,7 +140,7 @@ function symdiff!(s::IntSet, n::Integer) elseif n < 0 throw(ArgumentError("IntSet elements cannot be negative")) end - s.bits[n>>5 + 1] = xor(s.bits[n>>5 + 1], UInt32(1)<<(n&31)) + s.bits[n>>5 + 1] = s.bits[n>>5 + 1] ⊻ UInt32(1)<<(n&31) return s end @@ -282,14 +282,14 @@ function symdiff!(s::IntSet, s2::IntSet) end lim = length(s2.bits) for n = 1:lim - s.bits[n] = xor(s.bits[n], s2.bits[n]) + s.bits[n] = s.bits[n] ⊻ s2.bits[n] end if s2.fill1s for n=lim+1:length(s.bits) s.bits[n] = ~s.bits[n] end end - s.fill1s = xor(s.fill1s, s2.fill1s) + s.fill1s = s.fill1s ⊻ s2.fill1s s end diff --git a/base/latex_symbols.jl b/base/latex_symbols.jl index 86e88a950b4b2..ffa122464446e 100644 --- a/base/latex_symbols.jl +++ b/base/latex_symbols.jl @@ -83,6 +83,7 @@ const latex_symbols = Dict( "\\pppprime" => "⁗", "\\backpprime" => "‶", "\\backppprime" => "‷", + "\\xor" => "⊻", # Superscripts "\\^0" => "⁰", diff --git a/base/math.jl b/base/math.jl index 52efcb5489142..d4012525b9d9b 100644 --- a/base/math.jl +++ b/base/math.jl @@ -393,10 +393,10 @@ function significand{T<:AbstractFloat}(x::T) if xe == 0 # x is subnormal x == 0 && return x xs = xu & sign_mask(T) - xu = xor(xu, xs) + xu = xu ⊻ xs m = leading_zeros(xu)-exponent_bits(T) xu <<= m - xu = xor(xu, xs) + xu = xu ⊻ xs elseif xe == exponent_mask(T) # NaN or Inf return x end @@ -417,10 +417,10 @@ function frexp{T<:AbstractFloat}(x::T) if xe == 0 # x is subnormal x == 0 && return x, 0 xs = xu & sign_mask(T) - xu = xor(xu, xs) + xu = xu ⊻ xs m = leading_zeros(xu)-exponent_bits(T) xu <<= m - xu = xor(xu, xs) + xu = xu ⊻ xs k = 1-m elseif xe == exponent_mask(T) # NaN or Inf return x, 0 diff --git a/base/operators.jl b/base/operators.jl index fb0fd5f5d5328..4fda1f4d2f7e2 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -268,6 +268,10 @@ identity(x) = x (|)(x::Integer) = x xor(x::Integer) = x +const ⊻ = xor +const ∧ = & +const ∨ = | + # foldl for argument lists. expand recursively up to a point, then # switch to a loop. this allows small cases like `a+b+c+d` to be inlined # efficiently, without a major slowdown for `+(x...)` when `x` is big. @@ -1040,6 +1044,9 @@ export ∪, √, ∛, + ⊻, + ∧, + ∨, colon, hcat, vcat, @@ -1053,6 +1060,6 @@ import ..this_module: !, !=, xor, %, .%, ÷, .÷, &, *, +, -, .!=, .+, .-, .*, . .>=, .\, .^, /, //, <, <:, <<, <=, ==, >, >=, >>, .>>, .<<, >>>, <|, |>, \, ^, |, ~, !==, ===, >:, colon, hcat, vcat, hvcat, getindex, setindex!, transpose, ctranspose, - ≥, ≤, ≠, .≥, .≤, .≠, ⋅, ×, ∈, ∉, ∋, ∌, ⊆, ⊈, ⊊, ∩, ∪, √, ∛ + ≥, ≤, ≠, .≥, .≤, .≠, ⋅, ×, ∈, ∉, ∋, ∌, ⊆, ⊈, ⊊, ∩, ∪, √, ∛, ⊻, ∧, ∨ end diff --git a/base/random.jl b/base/random.jl index e8a91237450ad..7e81e67226729 100644 --- a/base/random.jl +++ b/base/random.jl @@ -447,7 +447,7 @@ function rand!{T<:Union{Float16, Float32}}(r::MersenneTwister, A::Array{T}, ::Ty A128 = unsafe_wrap(Array, convert(Ptr{UInt128}, pointer(A)), n128) @inbounds for i in 1:n128 u = A128[i] - u = xor(u, u << 26) + u = u ⊻ u << 26 # at this point, the 64 low bits of u, "k" being the k-th bit of A128[i] and "+" the bit xor, are: # [..., 58+32,..., 53+27, 52+26, ..., 33+7, 32+6, ..., 27+1, 26, ..., 1] # the bits needing to be random are @@ -488,17 +488,17 @@ function rand!(r::MersenneTwister, A::Array{UInt128}, n::Int=length(A)) i = 0 @inbounds while n-i >= 5 u = A[i+=1] - A[n] = xor(A[n], u << 48) - A[n-1] = xor(A[n-1], u << 36) - A[n-2] = xor(A[n-2], u << 24) - A[n-3] = xor(A[n-3], u << 12) + A[n] = A[n] ⊻ u << 48 + A[n-1] = A[n-1] ⊻ u << 36 + A[n-2] = A[n-2] ⊻ u << 24 + A[n-3] = A[n-3] ⊻ u << 12 n -= 4 end end if n > 0 u = rand_ui2x52_raw(r) for i = 1:n - @inbounds A[i] = xor(A[i], u << 12*i) + @inbounds A[i] = A[i] ⊻ u << 12*i end end A diff --git a/base/set.jl b/base/set.jl index a47a76460a0a4..f91f050f5e64e 100644 --- a/base/set.jl +++ b/base/set.jl @@ -189,7 +189,7 @@ const hashs_seed = UInt === UInt64 ? 0x852ada37cfe8e0ce : 0xcfe8e0ce function hash(s::Set, h::UInt) h = hash(hashs_seed, h) for x in s - h = xor(h, hash(x)) + h = h ⊻ hash(x) end return h end diff --git a/doc/manual/mathematical-operations.rst b/doc/manual/mathematical-operations.rst index 4e72568e2bf00..3a09b1eb34f6e 100644 --- a/doc/manual/mathematical-operations.rst +++ b/doc/manual/mathematical-operations.rst @@ -68,17 +68,17 @@ The following `bitwise operators `_ are supported on all primitive integer types: -=========== ========================================================================= -Expression Name -=========== ========================================================================= -``~x`` bitwise not -``x & y`` bitwise and -``x | y`` bitwise or -``xor(x, y)`` bitwise xor (exclusive or) -``x >>> y`` `logical shift `_ right -``x >> y`` `arithmetic shift `_ right -``x << y`` logical/arithmetic shift left -=========== ========================================================================= +=========== ========================================================================= +Expression Name +=========== ========================================================================= +``~x`` bitwise not +``x & y`` bitwise and +``x | y`` bitwise or +``x ⊻ y`` bitwise xor (exclusive or) +``x >>> y`` `logical shift `_ right +``x >> y`` `arithmetic shift `_ right +``x << y`` logical/arithmetic shift left +=========== ========================================================================= Here are some examples with bitwise operators: @@ -93,6 +93,9 @@ Here are some examples with bitwise operators: julia> 123 | 234 251 + julia> 123 ⊻ 234 + 145 + julia> xor(123, 234) 145 diff --git a/test/bitarray.jl b/test/bitarray.jl index 9d3e50bd2a61a..69c4f37f164f2 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -1087,7 +1087,7 @@ q[[1,3]] = true @test map(&, p, q) == map((x,y)->x&y, p, q) == p & q @test map(|, p, q) == map((x,y)->x|y, p, q) == p | q -@test map(xor, p, q) == map((x,y)->xor(x,y), p, q) == xor(p, q) +@test map(xor, p, q) == map((x,y)->x⊻y, p, q) == p ⊻ q @test map(^, p, q) == map((x,y)->x^y, p, q) == p .^ q @test map(*, p, q) == map((x,y)->x*y, p, q) == p .* q @@ -1112,7 +1112,7 @@ r = falses(4) @test map!(&, r, p, q) == map!((x,y)->x&y, r, p, q) == p & q == r @test map!(|, r, p, q) == map!((x,y)->x|y, r, p, q) == p | q == r -@test map!(xor, r, p, q) == map!((x,y)->xor(x,y), r, p, q) == xor(p, q) == r +@test map!(xor, r, p, q) == map!((x,y)->x⊻y, r, p, q) == p ⊻ q == r @test map!(^, r, p, q) == map!((x,y)->x^y, r, p, q) == p .^ q == r @test map!(*, r, p, q) == map!((x,y)->x*y, r, p, q) == p .* q == r @@ -1134,14 +1134,14 @@ for l=[0,1,63,64,65,127,128,129,255,256,257,6399,6400,6401] @test map(identity, p) == p @test map(&, p, q) == p & q @test map(|, p, q) == p | q - @test map(xor, p, q) == xor(p, q) + @test map(⊻, p, q) == p ⊻ q == map(xor, p, q) == xor(p, q) r = BitVector(l) @test map!(~, r, p) == ~p == r @test map!(identity, r, p) == p == r @test map!(~, r) == ~p == r @test map!(&, r, p, q) == p & q == r @test map!(|, r, p, q) == p | q == r - @test map!(xor, r, p, q) == xor(p, q) == r + @test map!(⊻, r, p, q) == p ⊻ q == map!(xor, r, p, q) == xor(p, q) == r end ## Filter ## diff --git a/test/numbers.jl b/test/numbers.jl index 5981923907b1a..9e0a4cc1984c6 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -20,12 +20,24 @@ const ≣ = isequal # convenient for comparing NaNs @test true & false == false @test false & true == false @test true & true == true +@test false ∧ false == false +@test true ∧ false == false +@test false ∧ true == false +@test true ∧ true == true @test false | false == false @test true | false == true @test false | true == true @test true | true == true - +@test false ∨ false == false +@test true ∨ false == true +@test false ∨ true == true +@test true ∨ true == true + +@test false ⊻ false == false +@test true ⊻ false == true +@test false ⊻ true == true +@test true ⊻ true == false @test xor(false, false) == false @test xor(true, false) == true @test xor(false, true) == true diff --git a/test/operators.jl b/test/operators.jl index ed303814bec88..69366044500d4 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -35,6 +35,9 @@ p = 1=>:foo @test (|)(2) == 2 @test xor(2) == 2 +@test (⊻)(2) == 2 +@test (∨)(2) == 2 +@test (∧)(2) == 2 # @test ctranspose('a') == 'a' # (c)transpose of Chars no longer supported