Hi,All

I'm now working in project to compress a files.

And I used Huffman encoding to get new code for each character in the site.

but now I can't complete my project.

How can I use bitwise operation to put all those new codes into the compression file.

and Also how can return it to its original characters.

I'm really need a help in this as i searched on Google and can't got any thing

any help will be useful to me.

Thanks,

The basic idea is that you combine your bit sets until you have 8 bits you can write to the file.

In semi-pseudo code:

uint bitsUsed=0;
uint64_t combinedSet=0;
[...]
const uint64_t bitSetToAdd=1234;
const uint bitSetLength=12;
combinedSet|=bitSetToAdd<<bitsUsed;
bitsUsed+=bitSetLength;
while (bitsUsed>=8)
{
  writeByteToFile(combinedSet&255);
  bitsUsed-=8;
  combinedSet>>=8;
}

Don't forget to "flush" any remaining bits that didn't add up to one byte when you're done writing.

Reading is basically the reverse of that; but less convenient - you'll have to read it bit by bit, since you don't know beforehand how long the Huffman sequence will be.

please, can you explain your semi-pseudo code.
As I couldn't understood it.

Well, suppose you have three bitsets (your Huffman sequences) with values 2, 5 and 4 which are 2, 3 and 4 bits in length, respectively.
In binary these are:
10
101
0100

The code would now string them together using bitwise operations as follows:
0100 101 10

As soon as there are 8 bits, they're written to the file - so we have:
0 10010110
The right 8 bits are written to the file and removed, which leaves us with
0

If there's nothing else to write to the file, seven filler bits must be added, so that a full byte is reached.

your explanation isn't clear

still have the same problem :(:(

Well, it looks like you're having some reading about bitwise operations to do.
So you should do that first.

I already readied about bitwise operation but all I need to understand your pseudo code step by step.

Then you should specify which step you don't understand.

I will use this algorithm for compression.

buffer ← 0
bufferLength ← 0
for each byte in the file:
    buffer ← buffer << code[byte].length
    buffer ← buffer | code[byte].code
    bufferLength ← bufferLength + code[byte].length
   while (bufferLength >= 8):
      file.write(buffer >>> (bufferLength-8))
      buffer ← buffer % (1 << (bufferLength-8))
      bufferLength ← bufferLength – 8
if (bufferLength > 0):
   file.write(buffer)
   file.write(bufferLength)
else
   file.write(8)

so How can I decompress.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.