diff --git a/src/longlonguint.jl b/src/longlonguint.jl index ca11cf1..4d08e1c 100644 --- a/src/longlonguint.jl +++ b/src/longlonguint.jl @@ -22,7 +22,7 @@ Base.UInt(x::LongLongUInt{1}) = x.content[1] Base.zero(::Type{LongLongUInt{C}}) where {C} = LongLongUInt{C}(ntuple(_->UInt(0), Val{C}())) Base.zero(::LongLongUInt{C}) where {C} = zero(LongLongUInt{C}) # convert from integers -LongLongUInt{C}(x::T) where {C, T<:Integer} = LongLongUInt{C}(ntuple(i->i==1 ? UInt(x) : zero(UInt), Val{C}())) +LongLongUInt{C}(x::T) where {C, T<:Integer} = LongLongUInt{C}(ntuple(i->i==C ? UInt(x) : zero(UInt), Val{C}())) Base.promote_type(::Type{LongLongUInt{C}}, ::Type{Int}) where {C} = LongLongUInt{C} Base.promote_type(::Type{LongLongUInt{C}}, ::Type{UInt}) where {C} = LongLongUInt{C} function Base.mod(x::LongLongUInt{C}, D::Int) where {C} @@ -51,6 +51,11 @@ function Base.:(<<)(x::LongLongUInt{C}, y::Int) where C end ) end +function readbit(x::LongLongUInt{C}, loc::Int) where {C} + k = (loc-1) ÷ bsizeof(UInt) + return readbit(x.content[C-k], loc - k*bsizeof(UInt)) +end +readbit(x::DitStr{D, N, LongLongUInt{C}}, loc::Int) where {D, N, C} = readbit(x.buf, loc) function indicator(::Type{LongLongUInt{C}}, i::Int) where C k = (i-1) ÷ bsizeof(UInt) LongLongUInt{C}(ntuple(j->j==C-k ? indicator(UInt, i-k*bsizeof(UInt)) : zero(UInt), Val{C}())) diff --git a/test/longlonguint.jl b/test/longlonguint.jl index 1d9ee6a..212ae40 100644 --- a/test/longlonguint.jl +++ b/test/longlonguint.jl @@ -14,6 +14,7 @@ using Test, BitBasis @test ~x == LongLongUInt((~UInt(3), ~UInt(6))) y = LongLongUInt((5, 7)) + @test one(y) == LongLongUInt((0, 1)) @test x & y == LongLongUInt((1, 6)) @test x | y == LongLongUInt((7, 7)) @test x ⊻ y == LongLongUInt((6, 1)) @@ -62,4 +63,18 @@ end @test indicator(LongLongUInt{1}, 64) == LongLongUInt((UInt(1)<<63,)) @test indicator(LongLongUInt{2}, 64) == LongLongUInt((zero(UInt), UInt(1)<<63)) @test indicator(LongLongUInt{2}, 65) == LongLongUInt((1, 0)) +end + +@testset "takebit" begin + x = LongLongUInt((3, 6)) + @test readbit(x, 1) == 0 + @test readbit(x, 2) == 1 + @test readbit(x, 3) == 1 + @test readbit(x, 4) == 0 + + @test readbit(x, 64) == 0 + @test readbit(x, 65) == 1 + @test readbit(x, 66) == 1 + @test readbit(x, 67) == 0 + @test readbit(BitStr{78}(x), 66) == 1 end \ No newline at end of file