ref #35 bitwise pack/unpack for integers
This commit is contained in:
1 parent
987b80e44d
commit
d969993997
3 files changed
+27
-4
No files matched your search
@@ -47,6 +47,10 @@ abstract class BitInput {
|
||||
return getBitsOrNull(count) ?: throw IllegalStateException("Unexpected end of stream")
|
||||
}
|
||||
|
||||
fun getBit(): Int {
|
||||
return getBitOrNull() ?: throw IllegalStateException("Unexpected end of stream")
|
||||
}
|
||||
|
||||
fun unpackUnsigned(): ULong {
|
||||
val tetrades = getBits(4).toInt()
|
||||
var result = 0UL
|
||||
@@ -57,5 +61,11 @@ abstract class BitInput {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun unpackSigned(): Long {
|
||||
val isNegative = getBit()
|
||||
val value = unpackUnsigned().toLong()
|
||||
return if( isNegative == 1) -value else value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ abstract class BitOutput {
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
fun putSigned(value: Long) {
|
||||
fun packSigned(value: Long) {
|
||||
if( value < 0 ) {
|
||||
putBit(1)
|
||||
packUnsigned((-value).toULong())
|
||||
|
||||
Reference in new issue
Block a user