Results 1 to 4 of 4
  1. #1
    akl
    akl is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default how to remove bits?

    DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
    out.writeInt(300);
    result is 00000000 00000000 00000001 00101100 (32 bits)
    but I need only 100101100 (9 bits)
    How to write in to binary file, highest than 255 values,without unnecessary bits?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to remove bits?

    Files can only be written in multiples of 8 bits (a byte); if your number fits in a byte, write a single byte; the same reasoning appiles for shorts etc ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: how to remove bits?

    The smallest value that you can write is a byte (8 bits on most machines). Which means in your case above, you would have to write out the lower two bytes. To do this you have to do some testing to see which byte, from high to low, has the first non-zero data. Of course, you may have problems if you don't somehow mark the data. For example, if you write out 300 as two bytes and then 298 as two bytes (both a short in this case), how do you know when you read it back in that it wasn't a single int to begin with.

    Perhaps it's not necessary in your app but its something to be aware of.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    akl
    akl is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: how to remove bits?

    I try to write lzw compression. but probably need replace three bytes sequence in to two bytes(short). if sequence repeated.

Similar Threads

  1. confused on bits
    By NoobieCode in forum New To Java
    Replies: 1
    Last Post: 06-12-2013, 08:57 AM
  2. How to restrict my binary output to 8 bits or 4 bits ?
    By Jack_Tauson_Sr in forum New To Java
    Replies: 16
    Last Post: 04-03-2013, 05:59 PM
  3. 48 bits JPEG 2000
    By user_java in forum Java 2D
    Replies: 0
    Last Post: 03-02-2010, 06:19 AM
  4. Image to Binary bits
    By Deva in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •