Skip to content

Commit 9090a15

Browse files
committed
Fixup
1 parent 00e4a56 commit 9090a15

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/bit-manipulation/bit-manipulation.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const BitUnsigned = Union{UInt8, UInt16, UInt32, UInt64}
22

33
@inline reversebits(x::Unsigned, ::BitsPerSymbol{8}) = bswap(x)
4+
@inline reversebits(x::Unsigned, ::BitsPerSymbol{1}) = bitreverse(x)
45

56
@inline function reversebits(x::T, ::BitsPerSymbol{2}) where T <: BitUnsigned
67
mask = 0x3333333333333333 % T
@@ -23,15 +24,18 @@ end
2324

2425
@inline reversebits(x::UInt32, ::BitsPerSymbol{32}) = x
2526
@inline function reversebits(x::UInt64, ::BitsPerSymbol{32})
26-
mask = 0x00000000FFFFFFF
27+
mask = 0x00000000FFFFFFFF
2728
x = ((x >> 32) & mask) | ((x & mask) << 32)
2829
reversebits(x, BitsPerSymbol{64}())
2930
end
3031

3132
@inline reversebits(x::UInt64, ::BitsPerSymbol{64}) = x
3233

3334
# Generic method for large integers, or odd-sized integers
34-
@inline function reversebits(x::Unsigned, b::BitsPerSymbol)
35+
@inline function reversebits(x::Unsigned, b::BitsPerSymbol{B}) where B
36+
if B > 8 * sizeof(x)
37+
throw(MethodError(reversebits, (x, b)))
38+
end
3539
if sizeof(x) < 8
3640
# If smaller than UInt64, we convert to UInt64, reverse, and then
3741
# shift down and truncate.

test/bit-manipulation.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@testset "Bit manipulation" begin
2+
function test_bit_reverse(u, bps)
3+
rv = BioSequences.reversebits(u, BioSequences.BitsPerSymbol{bps}())
4+
ustr = string(u; base=2, pad=8 * sizeof(u))
5+
rvs = join(reverse!(collect(Iterators.partition(ustr, bps))))
6+
@test rv == parse(typeof(u), rvs; base=2)
7+
end
8+
9+
# Test UInt128 with various BitsPerSymbol values (powers of two: 1, 2, 4, 8, 16, 32, 64)
10+
for bps in [1, 2, 4, 8, 16, 32, 64]
11+
# Edge cases
12+
test_bit_reverse(UInt128(0), bps)
13+
test_bit_reverse(typemax(UInt128), bps)
14+
15+
# Small values
16+
test_bit_reverse(UInt128(1), bps)
17+
test_bit_reverse(UInt128(0xff), bps)
18+
test_bit_reverse(UInt128(0xffff), bps)
19+
20+
# # Medium values
21+
test_bit_reverse(UInt128(0x123456789abcdef0), bps)
22+
test_bit_reverse(UInt128(0xfedcba9876543210), bps)
23+
24+
# # Large values
25+
test_bit_reverse(UInt128(0xb1d318f6d8b882f1ee180f1bcd8f8727), bps)
26+
test_bit_reverse(UInt128(0xffffffffffffffff0000000000000000), bps)
27+
test_bit_reverse(UInt128(0x0000000000000000ffffffffffffffff), bps)
28+
test_bit_reverse(UInt128(0xaaaaaaaaaaaaaaaa5555555555555555), bps)
29+
test_bit_reverse(UInt128(0x5555555555555555aaaaaaaaaaaaaaaa), bps)
30+
31+
# # Random-looking patterns
32+
test_bit_reverse(UInt128(0xdeadbeefcafebabe0123456789abcdef), bps)
33+
test_bit_reverse(UInt128(0x13579bdf02468ace8642fdb975310eca), bps)
34+
end
35+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ using YAML
1515

1616
# Test utils not dependent on BioSymbols
1717
include("utils.jl")
18+
include("bit-manipulation.jl")
1819

1920
@testset "Alphabet" begin
2021
include("alphabet.jl")

0 commit comments

Comments
 (0)