Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

shifting and writing bits (bytes) from longs?..tricky!  RSS feed

 
Matthew Ballard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
i'm in the process of implementing the LZW compression.
we hav to used fix length encoding, starting from 10 upto 32 bits per code.
i think i'm right in understanding this:
bufferedwriter, write(), only writes a byte at at time.
now i have to pad my first 256 compressions codes (simply the ascii values) with two 00 to make them the minimum of a 10 bit pattern.

now how do i go about doing this.
i've got a long at the moment (64bit eh?), and i'm shifting it to the left << 54, pushing it up againts the left, i need to do this because other codes coming in use this long as a buffer, then i write the first 8 bits, pushing it again to the left << 8 and continue to write.

i just can't figure out how to get the MSB out of the long into a byte in order to write to a file.
if you don't get this tell me what you don't get and i'll try explain it a little better.. any help apprecitaed.
if there is a better javaranch forum this topic should be in, please tell me.

cheers
 
Ryan McGuire
Ranch Hand
Posts: 1113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matthew Ballard:

i just can't figure out how to get the MSB out of the long into a byte in order to write to a file.
if you don't get this tell me what you don't get and i'll try explain it a little better.. any help apprecitaed.
if there is a better javaranch forum this topic should be in, please tell me.

cheers


Why not just use simple bit shifting?


I suppose you could do some trick with the java.io classes by wrapping a ByteArrayOutputStream in a DataOutputStream, writing the long to DOS and getting the bytes from BAOS:



(I hope that b comes out to 0x77 or 'w' in both cases? )

I haven't bothered to time each way of doing it. My guess would be that the first way is faster, but I'm willing to proven wrong.
 
Matthew Ballard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i havn't run through the first one, but at a glance it looks to do the same as the first eh?
that first example is helpful.
but the problem was more then that.

i could use say a byte[], but a long int is a continues variable and hence easier to shift with. the reason i shift the number to the MSB's is because then after the 10 bits (or 11, 12 ...32) then it buffers the next codes to be written to file.
so effectivly, i want to be able to take the MS 8 Bits from the long and write it to file, WITHOUT loosing any preceeding zeros. and then shift the buffer (long) << 8, having the reamaing 2 bits of the previous code, but caring, cause its all written to the file in sequence, hence the buffer.

its quite a painful tasks.
but i think you were on the right track, thanks!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!