Skip to content

Commit

Permalink
add bitstring (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet authored Aug 26, 2019
1 parent dce6fd8 commit 7578d46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/BitIntegers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module BitIntegers

import Base: &, *, +, -, <, <<, <=, ==, >>, >>>, |, ~, AbstractFloat, add_with_overflow,
bswap, checked_abs, count_ones, div, flipsign, isodd, leading_zeros, mod,
mul_with_overflow, ndigits0zpb, promote_rule, rem, sub_with_overflow,
bitstring, bswap, checked_abs, count_ones, div, flipsign, isodd, leading_zeros,
mod, mul_with_overflow, ndigits0zpb, promote_rule, rem, sub_with_overflow,
trailing_zeros, typemax, typemin, unsigned, xor

using Base: add_int, and_int, ashr_int, bswap_int, checked_sadd_int, checked_sdiv_int,
Expand Down Expand Up @@ -421,6 +421,7 @@ end
ndigits0zpb(x::XBS, b::Integer) = ndigits0zpb(unsigned(abs(x)), Int(b))
ndigits0zpb(x::XBU, b::Integer) = ndigits0zpb(x, Int(b))

bitstring(x::XBI) = string(reinterpret(uinttype(typeof(x)), x), pad = 8*sizeof(x), base = 2)

# * rand

Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ end
end
end

@testset "bitstring" begin
@test bitstring(UInt256(3)) == bitstring(Int256(3)) == '0'^254 * "11"
@test bitstring(UInt512(3) << 256) == bitstring(Int512(3) << 256) == '0'^254 * "11" * '0'^256
let (x, y) = rand(UInt128, 2)
u = UInt1024(x) << 512 + UInt1024(y)
v = u + UInt1024(1) << 1023
@test bitstring(u) == bitstring(Int1024(u)) == '0'^384 * bitstring(x) * '0'^384 * bitstring(y)
@test bitstring(v) == bitstring(typemin(Int1024) + Int1024(u)) == '1' * '0'^383 * bitstring(x) * '0'^384 * bitstring(y)
end
end

@testset "floats" begin
for X in XInts
Expand Down

0 comments on commit 7578d46

Please sign in to comment.