Skip to content

Instantly share code, notes, and snippets.

@davidsonbrsilva
Created May 27, 2019 12:22
Show Gist options
  • Save davidsonbrsilva/70fb14bc667afd649e0c8f5784cb2541 to your computer and use it in GitHub Desktop.
Save davidsonbrsilva/70fb14bc667afd649e0c8f5784cb2541 to your computer and use it in GitHub Desktop.
Método de construção de bytes comprimidos de Hufzip.
string Huff::buildBytes(string bitflow)
{
unsigned char byte = 0;
unsigned int currbit = 0;
std::string output = "";
while (currbit < bitflow.size())
{
// Concatena o byte construído a cada 8 bits lidos.
for (int i = 0; i < BYTE_SIZE; )
{
if (bitflow[currbit] == '1')
{
// Left shift: empurra todos os bits para a esquerda e o bit menos significativo se torna "0".
byte = byte << 1;
// Bitwise inclusive or: realiza uma operação de or inclusivo com "0000 0001".
// O bit menos significativo passará a ser "1".
byte = byte | 1;
}
else if (bitflow[currbit] == '0')
{
// Left shift: empurra todos os bits para a esquerda e o bit menos significativo se torna "0".
byte = byte << 1;
}
++currbit;
++i;
// Se o fluxo de bits gerado terminou e o byte ainda não foi totalmente construído, preenche o restante com zeros.
if (currbit >= bitflow.size())
{
int remaind = 8 - i;
for (int j = 0; j < remaind; ++j)
{
byte = byte << 1;
}
break;
}
}
output += byte;
byte = 0;
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment