• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading a file by bits

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to find a good way to read and write data one bit at a time. I found the class RandomAccessFile which has methods of writing and reading one byte at a time. But I want to read and write one bit at a time. Even the readBoolean() and writeBoolean() methods are one byte at a time. It seems like a waste of bits to me, because a boolean has only two possible values, so it needs only one bit.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm probably not the best person to answer your question, but I just had a couple thoughts... It seems useless to read one bit at a time. Files are measured in bytes, so for example, a file cannot ever be 3 and a half bytes. It's either going to be 3 or 4 bytes. So it's safe to read a whole byte at a time. Once you've read your byte, you can iterate through each bit, if you need each bit to represent a boolean value for example. I think your best bet is to design your program so that it can save up to 8 different boolean values (or flags, etc.) in a byte, then write that to a file, and vice versa.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

way to read and write data one bit at a time



Why? Most hardware works at least one byte at a time.
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the advice. Actually, what I am trying to do is read and write to a file using my own way of condensing the data. The data is condensed, in order to make the file as small as possible. For example, if a certain value can only be one of eight possible Strings, then I need only three bits to point to one of those eight Strings. I can write a class and methods to get a few bits at a time, but if java has something like that already, it will save me some time.

Which brings me to something else I was wondering about. Suppose I have a byte which I read from a file. How do I get a single bit from that byte? I use the method
byte readByte()
in the RandomAccessFile class and get a byte. Should I just do something like this?
byte mybyte = myfile.readByte();
boolean b = (mybyte < 128);
This is bit operation territory, isn't it? If someone could direct me to a web page which explains all about bit operations, I would be very grateful.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Tysen:
boolean b = (mybyte < 128);


Why not just write "boolean b = true;" ?
All bytes have values between -128 and 127 inclusive, and can therefore never be 128.

Which brings me to something else I was wondering about. Suppose I have a byte which I read from a file. How do I get a single bit from that byte?


You'll have to use bitwise operators indeed. To test if a bit is set, you should create an integer with that only that bit set, then check using the & operator:

Check out Bitwise operators and Common bit manipulation techniques on Wikipedia for more info.
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I guess I misread the API about bytes. In the readByte() method explanation it said 0<= b <= 255 so I thought it meant that bytes were in that range, but then I looked at it again more carefully, and it said the result of this method is signed, and equal to (byte) b.

I looked at the Wikipedia references. Helpful!
Thank you.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds almost like you're writing out some kind of compression scheme?

As was said above the files you write out are measured in bytes so even if your "scheme" compresses some value down to 5 bits of data... when you write it out to disk its going to be one byte or 8 bits.

I guess it does waste a little bit of space but really means that no matter how big of a chunk of data you compress... When you write the bits out to disk the most you will waste is 7 bits... Hardly seems worth worrying about?
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The DEFLATE compression algorithm is used in ZIP files, PNG images and many other places, and it's defined in terms of bits, so it's not a silly idea. It might be educational to look at the source for JZlib, a pure Java implementation of DEFLATE.
 
Hot dog! An advertiser loves us THIS much:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic