Skip to content

Commit 05b7020

Browse files
committed
Deprecate ~ in favor of flipbits
1 parent 931dc51 commit 05b7020

37 files changed

+165
-160
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@ Deprecated or removed
937937

938938
* `findin(a, b)` has been deprecated in favor of `find(occursin(b), a)` ([#24673]).
939939

940+
* `~` has been deprecated in favor of `flipbits` ([#25514]).
941+
940942

941943
Command-line option changes
942944
---------------------------
@@ -1187,3 +1189,4 @@ Command-line option changes
11871189
[#25231]: https://github.com/JuliaLang/julia/issues/25231
11881190
[#25365]: https://github.com/JuliaLang/julia/issues/25365
11891191
[#25424]: https://github.com/JuliaLang/julia/issues/25424
1192+
[#25514]: https://github.com/JuliaLang/julia/issues/25514

base/atomics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function atomic_and! end
203203
204204
Atomically bitwise-nand (not-and) `x` with `val`
205205
206-
Performs `x[] = ~(x[] & val)` atomically. Returns the **old** value.
206+
Performs `x[] = flipbits(x[] & val)` atomically. Returns the **old** value.
207207
208208
For further details, see LLVM's `atomicrmw nand` instruction.
209209

base/bitarray.jl

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ IndexStyle(::Type{<:BitArray}) = IndexLinear()
8181

8282
## aux functions ##
8383

84-
const _msk64 = ~UInt64(0)
84+
const _msk64 = flipbits(UInt64(0))
8585
@inline _div64(l) = l >> 6
8686
@inline _mod64(l) = l & 63
8787
@inline _msk_end(l::Integer) = _msk64 >>> _mod64(-l)
@@ -94,7 +94,7 @@ function glue_src_bitchunks(src::Vector{UInt64}, k::Int, ks1::Int, msk_s0::UInt6
9494
@inbounds begin
9595
chunk = ((src[k] & msk_s0) >>> ls0)
9696
if ks1 > k && ls0 > 0
97-
chunk_n = (src[k + 1] & ~msk_s0)
97+
chunk_n = (src[k + 1] & flipbits(msk_s0))
9898
chunk |= (chunk_n << (64 - ls0))
9999
end
100100
end
@@ -117,20 +117,20 @@ function copy_chunks!(dest::Vector{UInt64}, pos_d::Integer, src::Vector{UInt64},
117117

118118
u = _msk64
119119
if delta_kd == 0
120-
msk_d0 = ~(u << ld0) | (u << (ld1+1))
120+
msk_d0 = flipbits(u << ld0) | (u << (ld1 + 1))
121121
else
122-
msk_d0 = ~(u << ld0)
122+
msk_d0 = flipbits(u << ld0)
123123
msk_d1 = (u << (ld1+1))
124124
end
125125
if delta_ks == 0
126-
msk_s0 = (u << ls0) & ~(u << (ls1+1))
126+
msk_s0 = (u << ls0) & flipbits(u << (ls1+1))
127127
else
128128
msk_s0 = (u << ls0)
129129
end
130130

131131
chunk_s0 = glue_src_bitchunks(src, ks0, ks1, msk_s0, ls0)
132132

133-
dest[kd0] = (dest[kd0] & msk_d0) | ((chunk_s0 << ld0) & ~msk_d0)
133+
dest[kd0] = (dest[kd0] & msk_d0) | ((chunk_s0 << ld0) & flipbits(msk_d0))
134134

135135
delta_kd == 0 && return
136136

@@ -152,7 +152,7 @@ function copy_chunks!(dest::Vector{UInt64}, pos_d::Integer, src::Vector{UInt64},
152152

153153
chunk_s = (chunk_s0 >>> (64 - ld0)) | (chunk_s1 << ld0)
154154

155-
dest[kd1] = (dest[kd1] & msk_d1) | (chunk_s & ~msk_d1)
155+
dest[kd1] = (dest[kd1] & msk_d1) | (chunk_s & flipbits(msk_d1))
156156

157157
return
158158
end
@@ -177,24 +177,24 @@ function copy_chunks_rtol!(chunks::Vector{UInt64}, pos_d::Integer, pos_s::Intege
177177
delta_ks = ks1 - ks0
178178

179179
if delta_kd == 0
180-
msk_d0 = ~(u << ld0) | (u << (ld1+1))
180+
msk_d0 = flipbits(u << ld0) | (u << (ld1+1))
181181
else
182-
msk_d0 = ~(u << ld0)
182+
msk_d0 = flipbits(u << ld0)
183183
msk_d1 = (u << (ld1+1))
184184
end
185185
if delta_ks == 0
186-
msk_s0 = (u << ls0) & ~(u << (ls1+1))
186+
msk_s0 = (u << ls0) & flipbits(u << (ls1 + 1))
187187
else
188188
msk_s0 = (u << ls0)
189189
end
190190

191-
chunk_s0 = glue_src_bitchunks(chunks, ks0, ks1, msk_s0, ls0) & ~(u << s)
192-
chunks[kd0] = (chunks[kd0] & msk_d0) | ((chunk_s0 << ld0) & ~msk_d0)
191+
chunk_s0 = glue_src_bitchunks(chunks, ks0, ks1, msk_s0, ls0) & flipbits(u << s)
192+
chunks[kd0] = (chunks[kd0] & msk_d0) | ((chunk_s0 << ld0) & flipbits(msk_d0))
193193

194194
if delta_kd != 0
195195
chunk_s = (chunk_s0 >>> (64 - ld0))
196196

197-
chunks[kd1] = (chunks[kd1] & msk_d1) | (chunk_s & ~msk_d1)
197+
chunks[kd1] = (chunks[kd1] & msk_d1) | (chunk_s & flipbits(msk_d1))
198198
end
199199

200200
left -= s
@@ -212,10 +212,10 @@ function fill_chunks!(Bc::Array{UInt64}, x::Bool, pos::Integer, numbits::Integer
212212

213213
u = _msk64
214214
if k1 == k0
215-
msk0 = (u << l0) & ~(u << (l1+1))
215+
msk0 = (u << l0) & flipbits(u << (l1 + 1))
216216
else
217217
msk0 = (u << l0)
218-
msk1 = ~(u << (l1+1))
218+
msk1 = flipbits(u << (l1 + 1))
219219
end
220220
@inbounds if x
221221
Bc[k0] |= msk0
@@ -224,11 +224,11 @@ function fill_chunks!(Bc::Array{UInt64}, x::Bool, pos::Integer, numbits::Integer
224224
end
225225
k1 > k0 && (Bc[k1] |= msk1)
226226
else
227-
Bc[k0] &= ~msk0
227+
Bc[k0] &= flipbits(msk0)
228228
for k = k0+1:k1-1
229229
Bc[k] = 0
230230
end
231-
k1 > k0 && (Bc[k1] &= ~msk1)
231+
k1 > k0 && (Bc[k1] &= flipbits(msk1))
232232
end
233233
end
234234

@@ -253,10 +253,10 @@ function copy_to_bitarray_chunks!(Bc::Vector{UInt64}, pos_d::Int, C::Array{Bool}
253253

254254
u = _msk64
255255
if delta_kd == 0
256-
msk_d0 = msk_d1 = ~(u << ld0) | (u << (ld1+1))
256+
msk_d0 = msk_d1 = flipbits(u << ld0) | (u << (ld1+1))
257257
lt0 = ld1
258258
else
259-
msk_d0 = ~(u << ld0)
259+
msk_d0 = flipbits(u << ld0)
260260
msk_d1 = (u << (ld1+1))
261261
lt0 = 63
262262
end
@@ -269,7 +269,7 @@ function copy_to_bitarray_chunks!(Bc::Vector{UInt64}, pos_d::Int, C::Array{Bool}
269269
c |= (UInt64(C[ind]) << j)
270270
ind += 1
271271
end
272-
Bc[kd0] = (Bc[kd0] & msk_d0) | (c & ~msk_d0)
272+
Bc[kd0] = (Bc[kd0] & msk_d0) | (c & flipbits(msk_d0))
273273
bind += 1
274274
end
275275

@@ -306,7 +306,7 @@ function copy_to_bitarray_chunks!(Bc::Vector{UInt64}, pos_d::Int, C::Array{Bool}
306306
c |= (UInt64(C[ind]) << j)
307307
ind += 1
308308
end
309-
Bc[kd1] = (Bc[kd1] & msk_d1) | (c & ~msk_d1)
309+
Bc[kd1] = (Bc[kd1] & msk_d1) | (c & flipbits(msk_d1))
310310
end
311311
end
312312

@@ -408,7 +408,7 @@ function copyto!(dest::BitArray, src::BitArray)
408408
destc[nc] = srcc[nc]
409409
else
410410
msk_s = _msk_end(src)
411-
msk_d = ~msk_s
411+
msk_d = flipbits(msk_s)
412412
destc[nc] = (msk_d & destc[nc]) | (msk_s & srcc[nc])
413413
end
414414
end
@@ -634,7 +634,7 @@ end
634634
u = UInt64(1) << i2
635635
@inbounds begin
636636
c = Bc[i1]
637-
Bc[i1] = ifelse(x, c | u, c & ~u)
637+
Bc[i1] = ifelse(x, c | u, c & flipbits(u))
638638
end
639639
end
640640

@@ -676,7 +676,7 @@ function _unsafe_setindex!(B::BitArray, x, I::BitArray)
676676
end
677677
else
678678
for i = 1:length(Bc)
679-
Bc[i] &= ~Ic[i]
679+
Bc[i] &= flipbits(Ic[i])
680680
end
681681
end
682682
return B
@@ -705,7 +705,7 @@ function _unsafe_setindex!(B::BitArray, X::AbstractArray, I::BitArray)
705705
if Imsk & u != 0
706706
lx < c && throw_setindex_mismatch(X, c)
707707
@inbounds x = convert(Bool, X[c])
708-
C = ifelse(x, C | u, C & ~u)
708+
C = ifelse(x, C | u, C & flipbits(u))
709709
c += 1
710710
end
711711
u <<= 1
@@ -879,7 +879,7 @@ function insert!(B::BitVector, i::Integer, item)
879879
end
880880

881881
msk_aft = (_msk64 << j)
882-
msk_bef = ~msk_aft
882+
msk_bef = flipbits(msk_aft)
883883
Bc[k] = (msk_bef & Bc[k]) | ((msk_aft & Bc[k]) << 1)
884884
B[i] = item
885885
B
@@ -889,7 +889,7 @@ function _deleteat!(B::BitVector, i::Integer)
889889
k, j = get_chunks_id(i)
890890

891891
msk_bef = _msk64 >>> (63 - j)
892-
msk_aft = ~msk_bef
892+
msk_aft = flipbits(msk_bef)
893893
msk_bef >>>= 1
894894

895895
Bc = B.chunks
@@ -1088,13 +1088,13 @@ function (-)(B::BitArray)
10881088
end
10891089
broadcast(::typeof(sign), B::BitArray) = copy(B)
10901090

1091-
function broadcast(::typeof(~), B::BitArray)
1091+
function broadcast(::typeof(flipbits), B::BitArray)
10921092
C = similar(B)
10931093
Bc = B.chunks
10941094
if !isempty(Bc)
10951095
Cc = C.chunks
10961096
for i = 1:length(Bc)
1097-
Cc[i] = ~Bc[i]
1097+
Cc[i] = flipbits(Bc[i])
10981098
end
10991099
Cc[end] &= _msk_end(B)
11001100
end
@@ -1104,7 +1104,7 @@ end
11041104
"""
11051105
flipbits!(B::BitArray{N}) -> BitArray{N}
11061106
1107-
Performs a bitwise not operation on `B`. See [`~`](@ref).
1107+
Performs a bitwise not operation on `B`. See [`flipbits`](@ref).
11081108
11091109
# Examples
11101110
```jldoctest
@@ -1123,7 +1123,7 @@ function flipbits!(B::BitArray)
11231123
Bc = B.chunks
11241124
@inbounds if !isempty(Bc)
11251125
for i = 1:length(Bc)
1126-
Bc[i] = ~Bc[i]
1126+
Bc[i] = flipbits(Bc[i])
11271127
end
11281128
Bc[end] &= _msk_end(B)
11291129
end
@@ -1162,7 +1162,7 @@ broadcast(::typeof(&), B::BitArray, x::Bool) = x ? copy(B) : falses(size(B))
11621162
broadcast(::typeof(&), x::Bool, B::BitArray) = broadcast(&, B, x)
11631163
broadcast(::typeof(|), B::BitArray, x::Bool) = x ? trues(size(B)) : copy(B)
11641164
broadcast(::typeof(|), x::Bool, B::BitArray) = broadcast(|, B, x)
1165-
broadcast(::typeof(xor), B::BitArray, x::Bool) = x ? .~B : copy(B)
1165+
broadcast(::typeof(xor), B::BitArray, x::Bool) = x ? flipbits.(B) : copy(B)
11661166
broadcast(::typeof(xor), x::Bool, B::BitArray) = broadcast(xor, B, x)
11671167
for f in (:&, :|, :xor)
11681168
@eval begin
@@ -1471,7 +1471,7 @@ end
14711471

14721472
#findfirst(B::BitArray) = findnext(B, 1) ## defined in array.jl
14731473

1474-
# aux function: same as findnext(~B, start), but performed without temporaries
1474+
# aux function: same as findnext(flipbits(B), start), but performed without temporaries
14751475
function findnextnot(B::BitArray, start::Integer)
14761476
start > 0 || throw(BoundsError(B, start))
14771477
start > length(B) && return 0
@@ -1482,7 +1482,7 @@ function findnextnot(B::BitArray, start::Integer)
14821482

14831483
chunk_start = _div64(start-1)+1
14841484
within_chunk_start = _mod64(start-1)
1485-
mask = ~(_msk64 << within_chunk_start)
1485+
mask = flipbits(_msk64 << within_chunk_start)
14861486

14871487
@inbounds if chunk_start < l
14881488
if Bc[chunk_start] | mask != _msk64
@@ -1557,7 +1557,7 @@ function findprevnot(B::BitArray, start::Integer)
15571557
Bc = B.chunks
15581558

15591559
chunk_start = _div64(start-1)+1
1560-
mask = ~_msk_end(start)
1560+
mask = flipbits(_msk_end(start))
15611561

15621562
@inbounds begin
15631563
if Bc[chunk_start] | mask != _msk64
@@ -1688,24 +1688,24 @@ maximum(B::BitArray) = isempty(B) ? throw(ArgumentError("argument must be non-em
16881688
# arrays since there can be a 64x speedup by working at the level of Int64
16891689
# instead of looping bit-by-bit.
16901690

1691-
map(::Union{typeof(~), typeof(!)}, A::BitArray) = bit_map!(~, similar(A), A)
1691+
map(::Union{typeof(flipbits), typeof(!)}, A::BitArray) = bit_map!(flipbits, similar(A), A)
16921692
map(::typeof(zero), A::BitArray) = fill!(similar(A), false)
16931693
map(::typeof(one), A::BitArray) = fill!(similar(A), true)
16941694
map(::typeof(identity), A::BitArray) = copy(A)
16951695

1696-
map!(::Union{typeof(~), typeof(!)}, dest::BitArray, A::BitArray) = bit_map!(~, dest, A)
1696+
map!(::Union{typeof(flipbits), typeof(!)}, dest::BitArray, A::BitArray) = bit_map!(flipbits, dest, A)
16971697
map!(::typeof(zero), dest::BitArray, A::BitArray) = fill!(dest, false)
16981698
map!(::typeof(one), dest::BitArray, A::BitArray) = fill!(dest, true)
16991699
map!(::typeof(identity), dest::BitArray, A::BitArray) = copyto!(dest, A)
17001700

17011701
for (T, f) in ((:(Union{typeof(&), typeof(*), typeof(min)}), :(&)),
17021702
(:(Union{typeof(|), typeof(max)}), :(|)),
17031703
(:(Union{typeof(xor), typeof(!=)}), :xor),
1704-
(:(Union{typeof(>=), typeof(^)}), :((p, q) -> p | ~q)),
1705-
(:(typeof(<=)), :((p, q) -> ~p | q)),
1706-
(:(typeof(==)), :((p, q) -> ~xor(p, q))),
1707-
(:(typeof(<)), :((p, q) -> ~p & q)),
1708-
(:(typeof(>)), :((p, q) -> p & ~q)))
1704+
(:(Union{typeof(>=), typeof(^)}), :((p, q) -> p | flipbits(q))),
1705+
(:(typeof(<=)), :((p, q) -> flipbits(p) | q)),
1706+
(:(typeof(==)), :((p, q) -> flipbits(xor(p, q)))),
1707+
(:(typeof(<)), :((p, q) -> flipbits(p) & q)),
1708+
(:(typeof(>)), :((p, q) -> p & flipbits(q))))
17091709
@eval map(::$T, A::BitArray, B::BitArray) = bit_map!($f, similar(A), A, B)
17101710
@eval map!(::$T, dest::BitArray, A::BitArray, B::BitArray) = bit_map!($f, dest, A, B)
17111711
end

base/bitset.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ intersect(s1::BitSet, s2::BitSet) =
265265

266266
intersect!(s1::BitSet, s2::BitSet) = _matched_map!(&, s1, s2)
267267

268-
setdiff!(s1::BitSet, s2::BitSet) = _matched_map!((p, q) -> p & ~q, s1, s2)
268+
setdiff!(s1::BitSet, s2::BitSet) = _matched_map!((p, q) -> p & flipbits(q), s1, s2)
269269

270270
symdiff!(s::BitSet, ns) = foldl(int_symdiff!, s, ns)
271271

base/bool.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function !(x::Bool)
3232
return not_int(x)
3333
end
3434

35-
(~)(x::Bool) = !x
35+
bitflip(x::Bool) = !x
3636
(&)(x::Bool, y::Bool) = and_int(x, y)
3737
(|)(x::Bool, y::Bool) = or_int(x, y)
3838

base/checked.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ end
148148
if BrokenUnsignedInt != Union{}
149149
function add_with_overflow(x::T, y::T) where T<:BrokenUnsignedInt
150150
# x + y > typemax(T)
151-
# Note: ~y == -y-1
152-
x + y, x > ~y
151+
# Note: flipbits(y) == -y-1
152+
x + y, x > flipbits(y)
153153
end
154154
end
155155

base/deprecated.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,13 @@ module Operators
292292
for op in [:!, :(!=), :(!==), :%, :&, :*, :+, :-, :/, ://, :<, :<:, :<<, :(<=),
293293
:<|, :(==), :(===), :>, :>:, :(>=), :>>, :>>>, :\, :^, :colon,
294294
:adjoint, :getindex, :hcat, :hvcat, :setindex!, :transpose, :vcat,
295-
:xor, :|, :|>, :~, :×, :÷, :, :, :, :, :, :, :, :, :, :, :,
295+
:xor, :|, :|>, :×, :÷, :, :, :, :, :, :, :, :, :, :, :,
296296
:, :, :, :, :, :]
297297
if isdefined(Base, op)
298298
@eval Base.@deprecate_binding $op Base.$op
299299
end
300300
end
301+
Base.@deprecate ~(x) Base.flipbits(x)
301302
end
302303
export Operators
303304

@@ -2972,6 +2973,7 @@ end
29722973

29732974
@deprecate findin(a, b) find(occursin(b), a)
29742975

2976+
@deprecate ~(x) flipbits(x)
29752977

29762978
# END 0.7 deprecations
29772979

base/exports.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ export
214214
^,
215215
|,
216216
|>,
217-
~,
218217
:,
219218
=>,
220219
,
@@ -286,6 +285,7 @@ export
286285
flipsign,
287286
float,
288287
tryparse,
288+
flipbits,
289289
floor,
290290
fma,
291291
frexp,

0 commit comments

Comments
 (0)