Skip to content

Commit

Permalink
The minus operation for long integer
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Jul 17, 2024
1 parent 9d57781 commit 0f904c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/longlonguint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,24 @@ function _sadd(x::NTuple{C,UInt}, y::NTuple{C,UInt}, c::Bool) where {C}
return (_sadd(x[1:C-1], y[1:C-1], c1)..., v1)
end
end
function Base.:(-)(x::LongLongUInt{C}, y::LongLongUInt{C}) where {C}
return LongLongUInt(_ssub(x.content, y.content, false))
end
function _ssub(x::NTuple{1,UInt}, y::NTuple{1,UInt}, c::Bool)
return (x[1] - y[1] - c,)
end
function _ssub(x::NTuple{C,UInt}, y::NTuple{C,UInt}, c::Bool) where {C}
v1, c1 = Base.sub_with_overflow(x[C], y[C])
if c
v2, c2 = Base.sub_with_overflow(v1, c)
c = c1 || c2
return (_ssub(x[1:C-1], y[1:C-1], c)..., v2)
else
return (_ssub(x[1:C-1], y[1:C-1], c1)..., v1)
end
end
Base.count_ones(x::LongLongUInt) = sum(count_ones, x.content)
Base.bitstring(x::LongLongUInt) = join(bitstring.(x.content), "")

function longinttype(n::Int, D::Int)
N = ceil(Int, n * log2(D))
Expand Down
9 changes: 9 additions & 0 deletions test/longlonguint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ using Test, BitBasis
@test Int(x) === 3
@test bsizeof(x) == 64
@test BitBasis.nint(x) == 1
@test bitstring(x) == "0000000000000000000000000000000000000000000000000000000000000011"

x = LongLongUInt((3, 6))
@test bitstring(x) == "00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000110"
@test zero(x) == LongLongUInt((0, 0))
@test x << 1 == LongLongUInt((6, 12))
@test x >> 1 == LongLongUInt((UInt(1), UInt(3) + UInt(1)<<63))
Expand All @@ -19,6 +21,8 @@ using Test, BitBasis
@test x | y == LongLongUInt((7, 7))
@test x y == LongLongUInt((6, 1))
@test x + y == LongLongUInt((8, 13))
z = LongLongUInt((5, 4))
@test z - x == LongLongUInt((UInt64(1), typemax(UInt64)-1))

# add with overflow
z = LongLongUInt((UInt(17), typemax(UInt)-1))
Expand Down Expand Up @@ -77,4 +81,9 @@ end
@test readbit(x, 66) == 1
@test readbit(x, 67) == 0
@test readbit(BitStr{78}(x), 66) == 1
end

@testset "bmask" begin
@test bmask(LongLongUInt{1}, 1:1) == LongLongUInt((UInt64(1),))
@test bmask(LongLongUInt{2}, 1:65) == LongLongUInt((UInt64(1), typemax(UInt64)))
end

0 comments on commit 0f904c2

Please sign in to comment.