help with bitset class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: help with bitset class

  1. #1
    Join Date
    Oct 2005
    Posts
    17

    help with bitset class

    i was looking into the bitset class for java which was suggested to me on an earlier post which i thank to that person for such a helpful suggestion. This seems to be what i may be interested in. I found at least three helpful links that showed me how to create a bitset class which also showed how to look at each bit by using get() to see if it is true for the 1 bit or flase for the 0 bit. This seems to be what i am interested in, but its hard to find information on the bitset class. What i wanted to find was how to open any file as a bitset and read the bits as booleans, such as opening a text file , jpeg file, etc.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Why? Very little is done bit by bit. 99.9999% of processing is at the byte level.

  3. #3
    Join Date
    Oct 2005
    Posts
    17
    well i am interested in that .001%, and if someone could answer my question that will be very helpful. Or if someone could recommend a link or book that will answer my question that would too. Thankyou. Perhaps nspills has a suggestion since he recommended the bitset class.

  4. #4
    Join Date
    Oct 2005
    Posts
    17
    i mean .0001%.

  5. #5
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Do you need an existing class or could you create your own?
    If you read a file as bytes, you can then use the various operators to access individual bits. For example you can AND a byte with a mask to test if a bit is on:

    byte b = ???
    if(b & 0x80 != 0) // test if high order bit is set
    b & 0x40 != 0 // next lower one
    ...
    b & 0x01 != 0 // low order bit

  6. #6
    Join Date
    Oct 2005
    Posts
    17
    nspills suggested the bitset class which will return whatever bit you are referring to in a bitset as a boolean, false for 0 and true for 1. i think this is how it works; you use get(int i) where i is the nth bit in the set. However, if you could explain how i could read every bit in a file one by one that would be helpful. i have seen what you are talking about on other sites but i don't understand the high order or the low order. I am not sure what that means exactly. however, what i want to do is read every bit sequentially. Anywho, if you want to know what i am talking about for the java bitset class, look at this link: http://www.cs.columbia.edu/~nieh/tea...citation7.html
    Just scroll down until you get to the bold heading called The Bitset Class.

  7. #7
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    If you read a file as bytes, you will be reading the bits 8 at a time.
    If the bits in the file are to be numbered from 0 (the first one) to n the last one, then
    you could use some code like the following to return the nth bit. This is off the top of my head so it probably won't compile, but should give you an idea.

    Assign numbers to the bits of a byte: 01234567

    Create masks to test each of these bits
    final byte Byte0 = (byte)0x80;
    ...
    final byte Byte7 = (byte)0x01;

    put these in an array of masks:
    final byte[] Masks = new byte[]{Byte0, ... Byte7};
    This will be indexed to get the mask to be used below


    Read all the bytes of the file into an array of bytes: byte[] allBytes;

    The first eight bits of the file will be in the first byte (0th), the next 8 in the second byte and so on.

    To test a specific bit say: int bitNo = 9;

    First find which byte to test by dividing the bit number by 8. For example the 9th bit would be in the second byte. Remember its 0 based.
    int byteNbr = bitNo / 8; // = 1
    Find which bit to test by using modulo 8: the 9th bit would be bit number 1.
    int bitNbr = bitNo % 8; // = 1

    Now test the bit:

    allBytes[byteNbr] & Masks[bitNbr] != 0

  8. #8
    Join Date
    Oct 2005
    Posts
    17
    i found this code on the net. Supposedly allows you to read a byte bit by bit, but i don't understand how to read it. Would it allow me to read a file as a sequence of bits all the way up to the end of the file:

    public class Bits{

    public static void main(String[] args){
    byte b;
    String bs;

    b = Byte.parseByte(args[0]);
    bs = Integer.toBinaryString(b);
    if (bs.length() < 8){
    bs = "0000000" + bs;
    }
    bs = bs.substring(bs.length() - 8);
    System.out.println(bs);

    System.out.print(b + " = ");
    for (int i = 0; i < 8; ++i){
    if ((b & 0x80) != 0){
    System.out.print("1 ");
    }
    else{
    System.out.print("0 ");
    }
    b <<= 1;
    }
    System.out.println();
    } //end main
    }

  9. #9
    Join Date
    Oct 2005
    Posts
    17
    oh wait i just saw norm's post

  10. #10
    Join Date
    Oct 2005
    Posts
    17
    oh i completely get what you are saying. i just looked at hexadecimal again; you AND the first bit with 0x80, the second with 0x40, the third with 0x20, the fourth with 0x10, the fifth with 0x08 and so on. You load up each byte and determine the sequence. I completely understand, but there's just one thing which is that i am new to programming and don't know how to write such code. Is there a book with example code and detailed explanations or a link with such information. you see it is really hard to find information about this stuff on the net. oh yeah, one more thing; could'nt you read by four bits. Can't you create hexadecimal numbers with four bits. I also remember coming across masking four bits on the net if you declare what you are masking as a integer. Anywho, thank you Norm that was real helpful; the lightbulb really went on.

Similar Threads

  1. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  2. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  3. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 07:53 PM
  4. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 06:19 PM
  5. Calling base class methods
    By GR in forum Java
    Replies: 1
    Last Post: 11-09-2000, 05:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center