June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,6 +1,9 @@
Implement some operations on [[wp:Variable-length quantity|variable-length quantities]], at least including conversions from a normal number in the language to the binary representation of the variable-length quantity for that number, and ''vice versa''. Any variants are acceptable.
'''Task :''' With above operations,
;Task:
With above operations,
*convert these two numbers 0x200000 (2097152 in decimal) and 0x1fffff (2097151 in decimal) into sequences of octets (an eight-bit byte);
*display these sequences of octets;
*convert these sequences of octets back to numbers, and check that they are equal to original numbers.
<br><br>

View file

@ -1,34 +1,31 @@
type VLQ
q::Array{Uint8,1}
mutable struct VLQ
quant::Vector{UInt8}
end
function VLQ{T<:Integer}(n::T)
q = uint8(digits(n, 128))
for i in 2:length(q)
q[i] |= 0x80
end
VLQ(reverse(q))
function VLQ(n::T) where T <: Integer
quant = UInt8.(digits(n, 128))
@inbounds for i in 2:length(quant) quant[i] |= 0x80 end
VLQ(reverse(quant))
end
function Base.uint(vlq::VLQ)
q = reverse(vlq.q)
n = shift!(q)
p = one(Uint64)
for i in q
import Base.UInt64
function Base.UInt64(vlq::VLQ)
quant = reverse(vlq.quant)
n = shift!(quant)
p = one(UInt64)
for i in quant
p *= 0x80
n += p*(i&0x7f)
n += p * ( i & 0x7f)
end
return n
end
test = [0x00200000, 0x001fffff, 0x00000000, 0x0000007f,
0x00000080, 0x00002000, 0x00003fff, 0x00004000,
0x08000000, 0x0fffffff]
const test = [0x00200000, 0x001fffff, 0x00000000, 0x0000007f,
0x00000080, 0x00002000, 0x00003fff, 0x00004000,
0x08000000, 0x0fffffff]
for i in test
q = VLQ(i)
j = uint(q)
print(@sprintf " 0x%x => " i)
print(@sprintf "[%s]" join(["0x"*hex(r, 2) for r in q.q], ", "))
println(@sprintf " => 0x%x" j)
vlq = VLQ(i)
j = UInt(vlq)
@printf "0x%-8x => [%-25s] => 0x%x\n" i join(("0x" * hex(r, 2) for r in vlq.quant), ", ") j
end