can java read bits?

M

Moerderin

I understand that java 2 can read bytes, and then it can also write a
new file writing just those bytes, but i need java to go farther than
that. i have to write a program that reads bits catagorized into
groups of information then store it in a database using "pointers"

ie

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15| 16|
| Temperature | Thermometer reading in centegrades|therm. name|


thats just an example i made up right now, so how would i have a java
program read bits 1-4 store the info somewhere, then read bits 5-13
and store it somewhere else, and so on and so forth. if anyone has
previously written coding for this type of task or has any wonderful
ideas on how to accomplish it i would be much appreciated. thnx.
 
J

Jean Charbonneau

Moerderin said:
I understand that java 2 can read bytes, and then it can also write a
new file writing just those bytes, but i need java to go farther than
that. i have to write a program that reads bits catagorized into
groups of information then store it in a database using "pointers"

ie

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15| 16|
| Temperature | Thermometer reading in centegrades|therm. name|


thats just an example i made up right now, so how would i have a java
program read bits 1-4 store the info somewhere, then read bits 5-13
and store it somewhere else, and so on and so forth. if anyone has
previously written coding for this type of task or has any wonderful
ideas on how to accomplish it i would be much appreciated. thnx.

you could do such a thing :

If you would want to get the temperature, you could use a mask to get only
the values you need

Here is my view : Exemple with temperature



| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15| 16| -> convert
this binary number into byte
| Temperature | Useless information |

AND ( logical operator )

| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -> convert
this binary number into byte




Then the byte you'd get from that calculation, would only contain the
significative part of the temperature, and you could store it, isn't that
such thing you are trying to accomplish ?
 
R

Roedy Green

I understand that java 2 can read bytes, and then it can also write a
new file writing just those bytes, but i need java to go farther than
that. i have to write a program that reads bits catagorized into
groups of information then store it in a database using "pointers"

Other than a few bit addressable machines, hardware can only deal with
bytes. What you have to do is work with byte or longs then mask.

See http://mindprod.com/jgloss/binary.html
http://mindprod.com/jgloss/masking.html#BITSET
 
H

Hal Rosser

Turning on bits is simple as addition
turning them off is simple as subtraction
some languages use a numer as a series of flagged bits
to turn on the '64-bit' (the 7th ) in a variable - add 64 to it.
to turn on the '8-bit' (the 4th bit) add 8.
all you need to do is realize that the bits relate to powers of two

use bitwise operators to check if specific bits are on.
I have to review and play around with those bitwise operators every time
before I use them.
 
M

Moerderin

you could do such a thing :
If you would want to get the temperature, you could use a mask to get only
the values you need

Here is my view : Exemple with temperature



| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15| 16| -> convert
this binary number into byte
| Temperature | Useless information |

AND ( logical operator )

| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -> convert
this binary number into byte




Then the byte you'd get from that calculation, would only contain the
significative part of the temperature, and you could store it, isn't that
such thing you are trying to accomplish ?


yes thats exactly what i wanted to do, (i'm just learning how to mask
and so forth, thnx)
 
F

Frank

I understand that java 2 can read bytes, and then it can also write a
new file writing just those bytes, but i need java to go farther than
that. i have to write a program that reads bits catagorized into
groups of information then store it in a database using "pointers"

ie

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15| 16|
| Temperature | Thermometer reading in centegrades|therm. name|


thats just an example i made up right now, so how would i have a java
program read bits 1-4 store the info somewhere, then read bits 5-13
and store it somewhere else, and so on and so forth. if anyone has
previously written coding for this type of task or has any wonderful
ideas on how to accomplish it i would be much appreciated. thnx.

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
 
M

Moerderin

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());
}

}

}
 
M

Moerderin

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());
}

}
}
 
J

Joona I Palaste

Hal Rosser said:
Turning on bits is simple as addition

No it's not.
turning them off is simple as subtraction

No it's not.
some languages use a numer as a series of flagged bits
to turn on the '64-bit' (the 7th ) in a variable - add 64 to it.

If the "64-bit" is already set, this will clear it, and instead set
the "128-bit".
to turn on the '8-bit' (the 4th bit) add 8.

If the "8-bit" is already set, this will clear it, and instead set
the "16-bit". If it is already set, this will clear it, and instead
set the "32-bit", and so on.
all you need to do is realize that the bits relate to powers of two
use bitwise operators to check if specific bits are on.

Why not simply use bitwise operators to do the actual setting? To
set a bit, OR (operator |) the number with a number with that bit set
and the others clear. To clear a bit, AND (operator &) the number with
a number with that bit clear and the others set.
 
T

Tris Orendorff

(e-mail address removed) (Moerderin) wrote in

[..]
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);

You are writing "ary" here and not "messageIDBits."


Sincerely,

Tris Orendorff
--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d++ s+:- a+ C+ UL++++ P+ L+ E- W+ N++ o- K++ w+ O+ M !V PS+ PE Y+ PGP t+ !5 X- R- tv--- b++
DI++ D+ G++ e++ h---- r+++ y+++
------END GEEK CODE BLOCK------
 
D

Dave Monroe

I understand that java 2 can read bytes, and then it can also write a
new file writing just those bytes, but i need java to go farther than
that. i have to write a program that reads bits catagorized into
groups of information then store it in a database using "pointers"

ie

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15| 16|
| Temperature | Thermometer reading in centegrades|therm. name|


thats just an example i made up right now, so how would i have a java
program read bits 1-4 store the info somewhere, then read bits 5-13
and store it somewhere else, and so on and so forth. if anyone has
previously written coding for this type of task or has any wonderful
ideas on how to accomplish it i would be much appreciated. thnx.


Nobody seems to have mentioned the BitSet object.
 
D

Dale King

Moerderin said:
I understand that java 2 can read bytes, and then it can also write a
new file writing just those bytes, but i need java to go farther than
that. i have to write a program that reads bits catagorized into
groups of information then store it in a database using "pointers"

ie

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15| 16|
| Temperature | Thermometer reading in centegrades|therm. name|


thats just an example i made up right now, so how would i have a java
program read bits 1-4 store the info somewhere, then read bits 5-13
and store it somewhere else, and so on and so forth. if anyone has
previously written coding for this type of task or has any wonderful
ideas on how to accomplish it i would be much appreciated. thnx.

Here is some code I wrote for writing bits to an output stream. The reverse
process is just as simple:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&[email protected]&rnum=11
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top