can java read bits?

Discussion in 'Java' started by Moerderin, Jul 9, 2004.

  1. Moerderin

    Moerderin Guest

    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.
    Moerderin, Jul 9, 2004
    #1
    1. Advertising

  2. "Moerderin" <> wrote in message
    news:...
    > 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 ?
    Jean Charbonneau, Jul 9, 2004
    #2
    1. Advertising

  3. Michael Borgwardt, Jul 9, 2004
    #3
  4. Moerderin

    Roedy Green Guest

    On 9 Jul 2004 10:06:03 -0700, (Moerderin) wrote or
    quoted :

    >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



    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 9, 2004
    #4
  5. Moerderin

    Hal Rosser Guest

    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.


    "Moerderin" <> wrote in message
    news:...
    > 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.



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.716 / Virus Database: 472 - Release Date: 7/5/2004
    Hal Rosser, Jul 10, 2004
    #5
  6. Moerderin

    Moerderin Guest

    > 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)
    Moerderin, Jul 10, 2004
    #6
  7. Moerderin

    Roedy Green Guest

    Roedy Green, Jul 10, 2004
    #7
  8. Moerderin

    Frank Guest

    On 9 Jul 2004 10:06:03 -0700, Moerderin <> wrote:

    > 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
    Frank, Jul 10, 2004
    #8
  9. Moerderin

    Moerderin Guest

    thnx, but why wont my mask work?

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

    }

    }
    Moerderin, Jul 13, 2004
    #9
  10. Moerderin

    Moerderin Guest

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

    }
    }
    Moerderin, Jul 13, 2004
    #10
  11. Hal Rosser <> scribbled the following:
    > 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.

    > I have to review and play around with those bitwise operators every time
    > before I use them.


    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "A bicycle cannot stand up by itself because it's two-tyred."
    - Sky Text
    Joona I Palaste, Jul 14, 2004
    #11
  12. Re: thnx, but why wont my mask work?

    (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------
    Tris Orendorff, Jul 14, 2004
    #12
  13. Moerderin

    Roedy Green Guest

    On 14 Jul 2004 07:52:18 GMT, Joona I Palaste <>
    wrote or quoted :

    >> turning them off is simple as subtraction

    >
    >No it's not.


    Only if you never use the bit 31. Other wise you use & | >>> <<.
    See http://mindprod.com/jgloss/masking.html

    Even then, subtraction to remove a bit is not as swift as &. With
    subtraction you must first test a bit before you can remove it. With &
    you can do it one step.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 14, 2004
    #13
  14. Moerderin

    Dave Monroe Guest

    (Moerderin) wrote in message news:<>...
    > 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.
    Dave Monroe, Jul 15, 2004
    #14
  15. Moerderin

    Roedy Green Guest

    Roedy Green, Jul 15, 2004
    #15
  16. Moerderin

    Dale King Guest

    "Moerderin" <> wrote in message
    news:...
    > 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&selm=&rnum=11

    --
    Dale King
    Dale King, Jul 26, 2004
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GGG
    Replies:
    10
    Views:
    12,500
    Donar
    Jul 6, 2006
  2. sarmin kho
    Replies:
    2
    Views:
    810
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Umesh

    Can we replace 8 bits by 2 bits?

    Umesh, Jan 5, 2007, in forum: C Programming
    Replies:
    22
    Views:
    621
    Randy Howard
    Jan 10, 2007
  4. Umesh
    Replies:
    23
    Views:
    679
    Randy Howard
    Jan 10, 2007
  5. Zhi
    Replies:
    2
    Views:
    1,115
    Mike Treseler
    Oct 9, 2007
Loading...

Share This Page