diff options
Diffstat (limited to 'bitreader.cc')
-rw-r--r-- | bitreader.cc | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/bitreader.cc b/bitreader.cc index dc92223..8b78bb7 100644 --- a/bitreader.cc +++ b/bitreader.cc @@ -18,6 +18,17 @@ #include "bitreader.h" +namespace +{ +inline qint64 signExtend(quint64 x, int n) +{ + if (n < 64 && (x & (1 << (n-1)))) { + x |= -(1LL << n); + } + return x; +} +} + BitReader::BitReader(QIODevice *device) : child(device), buf(0), avail(0) { @@ -38,6 +49,12 @@ quint64 BitReader::readBits(int n) return x; } +qint64 BitReader::readSignedBits(int n) +{ + quint64 x = readBits(n); + return signExtend(x, n); +} + quint64 BitReader::peekBits(int n) { while (n > avail) { @@ -51,6 +68,12 @@ quint64 BitReader::peekBits(int n) return x; } +qint64 BitReader::peekSignedBits(int n) +{ + quint64 x = peekBits(n); + return signExtend(x, n); +} + void BitReader::skipUntilNextByte() { int skip = avail % 8; |