44 lines
555 B
C++

#pragma once
#include "Buffer.h"
class BitWriter
{
public:
int k = 0;
Buffer buffer;
char x = 0;
void writeByte(uint8_t v)
{
buffer.add_end(&v, sizeof(v));
}
void writeInt(int v)
{
buffer.add_end((uint8_t *)&v, sizeof(v));
}
void write16(unsigned short int v)
{
buffer.add_end((uint8_t *)&v, sizeof(v));
}
void writeBit(bool b)
{
if (k == 8)
{
writeByte(x);
k = 0;
x = 0;
}
x ^= (-b ^ x) & (1 << k);
k++;
}
void finish()
{
if (k > 0)
writeByte(x);
}
};