If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

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.

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:

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.

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

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:

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.