Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Problem with BitSet.length()...

Thread view: 
Mothra - 16 Aug 2007 12:22 GMT
... is that it does not return the length of the bit set.  For example, if
I create a new 8-bit set:

BitSet eightBits = new BitSet(8);

Running eightBits.length() returns zero, rather than eight.

The problem is that I want to be able to convert the bit set back to an
integer at some point.  I thought the easiest way would be to iterate
backwards over the bit set thus:

private static int bitsToInt(BitSet bits) {
       int myInt = 0;
       int binColumn = 1;

       // loop over the bitset backwards, adding up the true values
       for (int i=(bits.length()-1); i>=0; i--) {
           myInt += (booleanToInt(bits.get(i)) * binColumn);
           binColumn = (binColumn * 2);
       }
       
       return myInt;
   }

But because BitSet.length only returns the length from the highest
bit that's set to "true", this doesn't work for any binary numbers that
start with zeros in the full bit set (e.g. an 8-bit set of '00111000').

I suppose my method could take in a 'length' parameter that I coul define,
but isn't there a neater way of getting the true length of a BitSet?
Joshua Cranmer - 16 Aug 2007 22:32 GMT
> Running eightBits.length() returns zero, rather than eight.

Logically speaking, if length() returns the perceived size and not the
actual size, then there would be a function that returns the actual size
if it might be pertinent. This function would also have a name similar
to "length", like "capacity" or "size". Capacity is used for both
ArrayList and String{Builder,Buffer}, for example. Searching the
Javadocs for BitSet reveals this (in the function summary table):

size() - Returns the number of bits of space actually in use by this
BitSet to represent bit values.

> private static int bitsToInt(BitSet bits) {

What if BitSet.size() > 32?

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.