In terms of reading and storing, binary information is managed by bytes.
But, in your example, you're dealing with 16 bits at a time, which is
easily managed by a single short primitive.
So, you can load this information in as:
DataInput input=new DataInputStream( your_input_source );
short encoded=input.readShort(); //just one way to get a short
int temp=encoded >>> 12; //highest 4 bits
int reading=(encoded & 0x0FF8) >> 3;
int therm_name=encoded & 0x0007;
which allow you to treat each value by itself as something a bit more
natural...
recombining:
short combine(int temp, int reading, int therm_name) {
//only using assert to make sure params are in range
assert (temp & 0x000F) == temp;
assert (reading & 0x01FF) == reading;
assert (therm_name & 0x0007) == therm_name;
return (short) ( (temp<<12) | (reading<<3) | therm_name );
}
Hope this helps to shed some light on your question!
Frank
Thank you Frank, and everyone else. You are helping me understand this
a great deal. first of all I feel like an idiot, after looking at
other posts and so forth I realize I am supposed to label my bits like
this :
| 15| 14| 13| 12| 11| 10| 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Well, with all of your guys' help that you gave me I tried writing
code to do pretty much exactly what I need, but the mask isn't
working. here is the exact situation of what I have:
| 15| 14| 13| 12| 11| 10| 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
-----------------------------------------------------------------
| Word Count |
word 1
-----------------------------------------------------------------
| 0 | 1 | Message_ID | unused |
word 2
-----------------------------------------------------------------
| stuff I don't care about here |
word 3
-----------------------------------------------------------------|
more stuff I don't care about here | word 4
-----------------------------------------------------------------
My goal in writing this code was to extract the Message_ID and put it
in its own file. to do that I wrote code to skip the first word
(which is two bytes) and read the second word after shifting the
Message_ID to the far right (at least thats what I thought the coding
would do
Again, thank you for all your guys' help
well here is the short java file that I wrote, its not long so please
look at it and tell me why it wont mask and shift the Message_ID and
write it into a new file
import java.io.*;
public class Mask {
public static void main(String[] arguments) {
try {
// create object to be read
File bits = new File("c:/test.txt");
FileInputStream file = new FileInputStream(bits);
//skip the first two bytes
file.skip(2);
//read the next two bytes
byte[] ary = new byte[2];
//read file that contains bits
file.read(ary);
//create a mask
int i = 0;
int messageIDBits = (i >> 6) & 0xFF;
//create object to write the bits which were read
File txt = new File("c:/test1.txt");
FileOutputStream messageID = new FileOutputStream(txt);
//write the bits containing the Message_ID
messageID.write(ary);
//close the files
file.close();
messageID.close();
} catch (Exception e) {
System.out.println("Error -- " + e.toString());
}
}
}