?
Solved

Storing bits and then writing them to a file

Posted on 2005-03-28
12
Medium Priority
?
207 Views
Last Modified: 2010-03-31
Hi again,

I am implementing the Huffman Compression algorithm. This requires the program to store an appendable length of bits. This length then has to be written to a file.

I have been looking for data structures to store lengths of bits, but without much success. Also for the best way to then write these to a file.

Any help would be much appreciated. Thank you in advance.

Andy
0
Comment
Question by:acrxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 400 total points
ID: 13647791
Check: java.util.BitSet
You can call toString() and save it to file as a String. (or you can serialize the object if you don't care about the format).
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13648297
bitSet.set(int index, boolean value) will expand the bit set as much as needed.
You might want to take a look at http://www.hta-bi.bfh.ch/~hew/java/software/Huffman/ which also uses BitSet
0
 
LVL 6

Assisted Solution

by:CodingExperts
CodingExperts earned 800 total points
ID: 13649403
// This routine sets bit n in b.
byte BitSet(byte b, int n)
{
      b|=1<<(n-1);
      return(b);
}

// Convert a dual number representation in String format to an array of bytes.
void String2Bytes(String s, byte[] barray)
{
      int i,j,l=s.length()/8;
      for (j=0;j<l;j++) {
            for (i=0;i<8;i++) {      
                  if (s.charAt(i+(j*8))=='1') barray[j]=BitSet(barray[j],8-i);
            }
      }
      l=s.length()%8;
      for (i=0;i<l;i++) {
              if (s.charAt(i+(j*8))=='1') barray[j]=BitSet(barray[j],8-i);
      }
}

-CE
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Expert Comment

by:Webstorm
ID: 13652418
Hi acrxx,

you can use this :


int tmp=0, // store written bits until we get a whole byte
     nbit=8; // number of bits that can be stored in tmp

/*
    bits : contains your bit values (high bits first)
    nbits : number of bits to be written
*/
void writeBits(OutputStream out,int bits, int nbits)
{
    while (nbits>0)
    {
        int n=(nbits>nbit)?nbit:nbits;

        nbits-=n;
        nbit-=n;
        tmp|=(  (bits>>>nbits)&( (1<<n)-1 )  )<<nbit;

        if (nbit==0) // no more bits can be stored in tmp
        {
            out.write(tmp);
            tmp=0;
            nbit=8;
        }
    }
}

// call this method just before closing your output stream
void flushBits(OutputStream out)
{
        if (nbit<0) out.write(tmp);
}
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 13652438
>>void flushBits(OutputStream out)
>>{
>>        if (nbit<0) out.write(tmp);

should be :

void flushBits(OutputStream out)
{
        if (nbit<8) out.write(tmp);
0
 

Author Comment

by:acrxx
ID: 13653112
Does this mean, I can store the bitstream in int form, and the equivalent is then written to the file in bit form?

when:
int bits = 10001111

is then stored in the file as 10001111 in bits

Is that correct?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13653215
No, integer has 4 bytes and therfore can represent 4 * 8 bits.
You can display it in the form of 101.. (binary form) by doing Integer.toBinaryString(bits).
But again, you are limited to 32 bits and to manipulate it you will have to use the binary operations (|, <, > ~, &, ..)
BitSet doesn't have the size limitation.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 13653473
>> Is that correct?
You can use my code to put each code when you generate them.
One Huffman code never exceed 16 bits (or less).

0
 
LVL 13

Accepted Solution

by:
Webstorm earned 800 total points
ID: 13653523
You can directly write the bits to a file (use a FileOutputStream for the "out" parameter)
Or, if you want to get a byte array, use a ByteArrayOutputStream.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13655403
>> One Huffman code never exceed 16 bits
Why is that? I don't think there is a limit to the number of bits that can be used by Huffman code (that is very much depends on the amount of symbols you  are trying to encode).
0
 

Author Comment

by:acrxx
ID: 13669988
Thanks to all for relieving me of the headache I was having over this :)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 13671148
:-)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question