| | |
Storing things as a byte
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 1,568
Reputation:
Solved Threads: 196
I'm having trouble with storing bits as a byte. I've read probably 30 different sources about this already. I'm doing Huffman Compression. Lets say I read in the character "c" and I am supposed to output "1010". How would I go about doing that? I know how to write bytes to an output stream, my problem is generating the bytes in the first place. My question is very similar to a question I found elsewhere on the web that received this response:
"Don't do that. Use the bit manipulation operations to create an integer - you need to store the accumulated bits, and the number of bits you've written to it. When you have 8 bits, write those out as a byte, and shift the stored value right by 8 and reduce the count by 8. That way you can write irregular numbers of bits, and don't even have to convert numbers and strings. "
I understand the need for 'waiting until you have 8 bits' to write the byte. However, what would a method look like that recursively changed the bits 0000 to 1111, one at a time? The problem is - if we have 0000, in order to change the second bit to a 1, don't I have to do
0000 | 0010
So this causes issues if I don't know what bit I'm currently "on", and just want to recursively "add" a series of bits and create a byte out of it. What I want to do looks more like String concatenation, except I want to do it for a byte, and I want to do it a lot more efficiently than can be done w/ Strings.
So, for example, if this was possible, this would be what I'd want to do:
byte current = empty;
for (int i = 0; i < 8, i++)
current += 1;
and have current come out as 11111111
"Don't do that. Use the bit manipulation operations to create an integer - you need to store the accumulated bits, and the number of bits you've written to it. When you have 8 bits, write those out as a byte, and shift the stored value right by 8 and reduce the count by 8. That way you can write irregular numbers of bits, and don't even have to convert numbers and strings. "
I understand the need for 'waiting until you have 8 bits' to write the byte. However, what would a method look like that recursively changed the bits 0000 to 1111, one at a time? The problem is - if we have 0000, in order to change the second bit to a 1, don't I have to do
0000 | 0010
So this causes issues if I don't know what bit I'm currently "on", and just want to recursively "add" a series of bits and create a byte out of it. What I want to do looks more like String concatenation, except I want to do it for a byte, and I want to do it a lot more efficiently than can be done w/ Strings.
So, for example, if this was possible, this would be what I'd want to do:
byte current = empty;
for (int i = 0; i < 8, i++)
current += 1;
and have current come out as 11111111
Last edited by BestJewSinceJC; Dec 7th, 2008 at 1:47 pm.
•
•
Join Date: Sep 2008
Posts: 1,568
Reputation:
Solved Threads: 196
Actually, wait lol. After all this time spent trying to figure this out, I stopped thinking about it and I think it just came to me. If I do
byte current = 00000000;
current = current | 1;
that will make current = 00000001, right?
Then, the next time I wanted to add a bit to the front, I could shift the current bits over 1 and do another OR. No?
byte current = 00000000;
current = current | 1;
that will make current = 00000001, right?
Then, the next time I wanted to add a bit to the front, I could shift the current bits over 1 and do another OR. No?
![]() |
Similar Threads
- Vista login pass forgotten (Windows Vista and Windows 7)
- A few questions about binary i/o.. (C++)
- php.ini confusion (PHP)
- Help for comparing strings and copying strings please. (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: Action Even
- Next Thread: Text modifier program
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking bidirectional binary binarytree birt bluetooth chat chatprogramusingobjects class client code columns component database derby design eclipse encryption error errors expand fractal game givemetehcodez graphics gui guidancer homework html ide if_statement image inheritance integer intellij interface j2me java javadesktopapplications javaprojects jlabel jme jni jpanel jtextfield julia linux list map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source problem program programming project property recursion reference ria scanner search server set sms sort sourcelabs splash sql sqlite static stop string support swing testautomation threads tree ui unicode validation windows






