Writes n bits starting with the least significant bit. They are written one byte at a time so endianness is of no concern.
| 38 | // Writes n bits starting with the least significant bit. They are |
| 39 | // written one byte at a time so endianness is of no concern. |
| 40 | void Write(uint8_t bits, uint8_t n) |
| 41 | { |
| 42 | if (n == 0) |
| 43 | return; |
| 44 | |
| 45 | bits = bits & bits::GetFullMask(n); |
| 46 | |
| 47 | ASSERT_LESS_OR_EQUAL(n, CHAR_BIT, ()); |
| 48 | uint32_t bufferedBits = m_bitsWritten % CHAR_BIT; |
| 49 | m_bitsWritten += n; |
| 50 | if (n + bufferedBits > CHAR_BIT) |
| 51 | { |
| 52 | uint8_t b = (bits << bufferedBits) | m_buf; |
| 53 | m_writer.Write(&b, 1); |
| 54 | m_buf = bits >> (CHAR_BIT - bufferedBits); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | if (bufferedBits > 0) |
| 59 | { |
| 60 | bits = (bits << bufferedBits) | m_buf; |
| 61 | n += bufferedBits; |
| 62 | } |
| 63 | if (n == CHAR_BIT) |
| 64 | { |
| 65 | m_writer.Write(&bits, 1); |
| 66 | bits = 0; |
| 67 | } |
| 68 | m_buf = bits; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #define WRITE_BYTE() \ |
| 73 | { \ |