-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitInputStream.cpp
More file actions
55 lines (43 loc) · 1.27 KB
/
BitInputStream.cpp
File metadata and controls
55 lines (43 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
@author Rajdeep Konwar (A53225609)
@date Nov 10 2017
@section OVERVIEW
Implementation of class BitInputStream's member functions.
@section ASSIGNMENT NUMBER
PA3
*/
#include "HCTree.h"
#include "BitInputStream.h"
/** Input params: None
* Return param: None
* Description : Fill the buffer with a new byte from istream.
*
* Get a byte from istream to bitwise buffer and set no. of bits to zero
*/
void BitInputStream::fill() {
buff = in.get(); //! Read one byte from istream to bitwise buffer
//! Handle end of file
if( in.eof() ) {
nbits = -1; //! Special return case for eof
return;
}
nbits = 0; //! No bits have been read from bitwise buffer
}
/** Input params: None
* Return param: unsigned int bit
* Description : Read the next bit from the buffer.
*/
unsigned int BitInputStream::readBit() {
//! Fill bitwise buffer if there are no more unread bits
if( nbits == 8 )
fill();
//! Special case to return when hit eof
if( nbits == -1 )
return 2;
//! Get the next unread bit (least significant bit!) from the bitwise buffer
unsigned int nextBit = ((buff & (1 << nbits)) >> nbits);
//! Increment the number of bits read from the bitwise buffer
nbits++;
//! Return the bit just read
return nextBit;
}