Bit-Wise Operations
Base.:~
— FunctionBase.:&
— Functionx & y
Bitwise and. Implements three-valued logic, returning missing
if one operand is missing
and the other is true
. Add parentheses for function application form: (&)(x, y)
.
Examples
julia> 4 & 10
0
julia> 4 & 12
4
julia> true & missing
missing
julia> false & missing
false
Base.:|
— Functionx | y
Bitwise or. Implements three-valued logic, returning missing
if one operand is missing
and the other is false
.
Examples
julia> 4 | 10
14
julia> 4 | 1
5
julia> true | missing
true
julia> false | missing
missing
Base.xor
— Functionxor(x, y)
⊻(x, y)
Bitwise exclusive or of x
and y
. Implements three-valued logic, returning missing
if one of the arguments is missing
.
The infix operation a ⊻ b
is a synonym for xor(a,b)
, and ⊻
can be typed by tab-completing \xor
or \veebar
in the Julia REPL.
Examples
julia> xor(true, false)
true
julia> xor(true, true)
false
julia> xor(true, missing)
missing
julia> false ⊻ false
false
julia> [true; true; false] .⊻ [true; false; false]
3-element BitVector:
0
1
0
Base.:⊻
— Functionxor(x, y)
⊻(x, y)
Bitwise exclusive or of x
and y
. Implements three-valued logic, returning missing
if one of the arguments is missing
.
The infix operation a ⊻ b
is a synonym for xor(a,b)
, and ⊻
can be typed by tab-completing \xor
or \veebar
in the Julia REPL.
Examples
julia> xor(true, false)
true
julia> xor(true, true)
false
julia> xor(true, missing)
missing
julia> false ⊻ false
false
julia> [true; true; false] .⊻ [true; false; false]
3-element BitVector:
0
1
0
Shift
Base.:<<
— Function<<(x, n)
Left bit shift operator, x << n
. For n >= 0
, the result is x
shifted left by n
bits, filling with 0
s. This is equivalent to x * 2^n
. For n < 0
, this is equivalent to x >> -n
.
Examples
julia> Int8(3) << 2
12
julia> bitstring(Int8(3))
"00000011"
julia> bitstring(Int8(12))
"00001100"
<<(B::BitVector, n) -> BitVector
Left bit shift operator, B << n
. For n >= 0
, the result is B
with elements shifted n
positions backwards, filling with false
values. If n < 0
, elements are shifted forwards. Equivalent to B >> -n
.
Examples
julia> B = BitVector([true, false, true, false, false])
5-element BitVector:
1
0
1
0
0
julia> B << 1
5-element BitVector:
0
1
0
0
0
julia> B << -1
5-element BitVector:
0
1
0
1
0
Base.:>>
— Function>>(x, n)
Right bit shift operator, x >> n
. For n >= 0
, the result is x
shifted right by n
bits, filling with 0
s if x >= 0
, 1
s if x < 0
, preserving the sign of x
. This is equivalent to fld(x, 2^n)
. For n < 0
, this is equivalent to x << -n
.
Examples
julia> Int8(13) >> 2
3
julia> bitstring(Int8(13))
"00001101"
julia> bitstring(Int8(3))
"00000011"
julia> Int8(-14) >> 2
-4
julia> bitstring(Int8(-14))
"11110010"
julia> bitstring(Int8(-4))
"11111100"
>>(B::BitVector, n) -> BitVector
Right bit shift operator, B >> n
. For n >= 0
, the result is B
with elements shifted n
positions forward, filling with false
values. If n < 0
, elements are shifted backwards. Equivalent to B << -n
.
Examples
julia> B = BitVector([true, false, true, false, false])
5-element BitVector:
1
0
1
0
0
julia> B >> 1
5-element BitVector:
0
1
0
1
0
julia> B >> -1
5-element BitVector:
0
1
0
0
0
Base.:>>>
— Function>>>(x, n)
Unsigned right bit shift operator, x >>> n
. For n >= 0
, the result is x
shifted right by n
bits, filling with 0
s. For n < 0
, this is equivalent to x << -n
.
For Unsigned
integer types, this is equivalent to >>
. For Signed
integer types, this is equivalent to signed(unsigned(x) >> n)
.
Examples
julia> Int8(-14) >>> 2
60
julia> bitstring(Int8(-14))
"11110010"
julia> bitstring(Int8(60))
"00111100"
BigInt
s are treated as if having infinite size, so no filling is required and this is equivalent to >>
.
>>>(B::BitVector, n) -> BitVector
Unsigned right bitshift operator, B >>> n
. Equivalent to B >> n
. See >>
for details and examples.