Bits.bitMethod
bit(x::Integer, i::Integer)       -> typeof(x)
bit(x::AbstractFloat, i::Integer) -> Integer

Return the bit of x at position i, with value 0 or 1. If x::Integer, the returned bit is of the same type. If x::AbstractFloat is a bits type, the returned bit is a signed integer with the same bitsize as x. See also tstbit.

Examples

julia> bit(0b101, 1)
0x01

julia> bit(0b101, 2)
0x00

julia> bit(-1.0, 64)
1
Bits.bitsMethod
bits(x::Real)

Create an immutable view on the bits of x as a vector of Bool, similar to a BitVector. If x is a BigInt, the vector has length Bits.INF. Currently, no bounds check is performed when indexing into the vector.

Examples

julia> v = bits(Int16(2^8+2^4+2+1))
<00000001 00010011>

julia> permutedims([v[i] for i in 8:-1:1])
1×8 Array{Bool,2}:
false  false  false  true  false  false  true  true

julia> bits(true)
<1>

julia> bits(big(2)^63)
<...0 10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000>

julia> bits(Float32(-7))
<1|10000001|1100000 00000000 00000000>

julia> ans[1:23] # creates a vector of bits with a specific length
<1100000 00000000 00000000>
Bits.bitsizeMethod
bitsize(T::Type) -> Int
bitsize(::T)     -> Int

Return the number of bits that can be held by type T. Only the second method may be defined when the number of bits is a dymanic value, like for BitFloat.

Examples

julia> bitsize(Int32)  == 32        &&
bitsize(true)   == 1         &&
bitsize(big(0)) == Bits.INF  &&
bitsize(1.2)    == 64
true

julia> x = big(1.2); bitsize(x) == 256 + sizeof(x.exp)*8 + 1
true
Bits.low0Function
low0(x, n::Integer=1)
low1(x, n::Integer=1)

Return the position of the nth 0 (for low0) or 1 (for low1) in x.

Examples

julia> low0(0b10101, 2)
4

julia> low1(0b10101, 4) == Bits.NOTFOUND
true
Bits.low1Function
low0(x, n::Integer=1)
low1(x, n::Integer=1)

Return the position of the nth 0 (for low0) or 1 (for low1) in x.

Examples

julia> low0(0b10101, 2)
4

julia> low1(0b10101, 4) == Bits.NOTFOUND
true
Bits.maskMethod
mask(T::Type{<:Integer} := UInt, j::Integer, i::Integer) -> T

Return an integer of type T whose j right-most bits are 0, the following i-j bits are 1, and the remaining bits are 0 (i.e. of the form 0b0...01...10...0 with exactly i-j1s preceded by j0s). When j < 0, the result is not specified. When i < 0, the result is equal to ~mask(T, j), i.e. of the form 1...10...0 with exactly j zeros. NOTE: unstable API, could be changed to mask(j, i-j) instead.

Examples

julia> bits(mask(UInt8, 2, 5))
<00011100>

<...1 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111000>
Bits.maskMethod
mask(T::Type{<:Integer}:=UInt, i::Integer=bitsize(T)) -> T

Return an integer of type T whose i right-most bits are 1, and the others are 0 (i.e. of the form 0b0...01...1 with exactly i1s. When i is not specified, all possible bits are set to 1. When i < 0, the result is not specified. T defaults to UInt.

Examples

julia> mask(3)
0x0000000000000007

0xff

<00000000 11111111 11111111 11111111>
Bits.maskedMethod
masked(x, [j::Integer], i::Integer) -> typeof(x)

Return the result of applying the mask mask(x, [j], i) to x, i.e. x & mask(x, [j], i). If x is a float, apply the mask to the underlying bits.

Examples

julia> masked(0b11110011, 1, 5) === 0b00010010
true

julia> x = rand(); masked(-x, 0, 63) === x
true
Bits.scan0Function
scan0(x, n::Integer=1)
scan1(x, n::Integer=1)

Return the position of the first 0 (for scan0) or 1 (for scan1) after or including n in x.

Examples

julia> scan0(0b10101, 1)
2

julia> scan1(0b10101, 6) == Bits.NOTFOUND
true
Bits.scan1Function
scan0(x, n::Integer=1)
scan1(x, n::Integer=1)

Return the position of the first 0 (for scan0) or 1 (for scan1) after or including n in x.

Examples

julia> scan0(0b10101, 1)
2

julia> scan1(0b10101, 6) == Bits.NOTFOUND
true
Bits.tstbitMethod
tstbit(x::Real, i::Integer) -> Bool

Similar to bit but returns the bit at position i as a Bool.

Examples

julia> tstbit(0b101, 3)
true
Bits.weightMethod
weight(x::Real) -> Int

Hamming weight of x considered as a binary vector. Similarly to count_ones(x), counts the number of 1 in the bit representation of x, but not necessarily at the "bare-metal" level; for example count_ones(big(-1)) errors out, while weight(big(-1)) == Bits.INF, i.e. a BigInt is considered to be an arbitrary large field of bits with twos complement arithmetic.

Examples

julia> weight(123)
6

julia> count(bits(123))
6
Bits.INFConstant

INF::Int indicates the position of the bit at "infinity", for types which can carry an arbitrary number of bits, like BigInt. INF is also used to indicate an arbitrary large number of bits. Currently, Bits.INF == typemax(Int).

Bits.NOTFOUNDConstant

NOTFOUND::Int indicates that no position matches the request, similar to nothing with findnext. Currently, Bits.NOTFOUND == 0.