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 / November 2006

Tip: Looking for answers? Try searching our database.

BitSet Class Implementation

Thread view: 
Erick Crouse - 16 Nov 2006 23:27 GMT
Hello Everyone,

I need a question answered concerning the BitSet class. The problem is
that the public methods which manipulate a set of bits requires a
BitSet object as both the caller and argument to the method...

private BitSet A = new BitSet(16),
private BitSet B = new BitSet(16),

// do some manipulations with BitSet... and then...

A.and(B);   // Results in a different A (B is unchanged)
A.xor(B);    // Again results in a different A (B is unchanged)
                   // Interface: public void and( BitSet bits) -
BitSet;

The problem with this is that I prefer both A and B BitSet objects
remain the same while resulting in a new BitSet object say C using an
interface similar to -> public BitSet and( BitSet A, BitSet B) -
BitSet. With the current implementation of BitSet I would need to do
some sort of clone implementation (which seems like overkill) or
instantiate two identical A objects, say A1 and A2, one of which  gets
operated on and then the other remains original for other purposes (
which seems a little awkward having to instantiate identical objects ),
Can anyone suggest what they would do in this situation where the
implementation operates either operand?

Thanks a Million,

EVAC
Daniel Pitts - 17 Nov 2006 00:32 GMT
> Hello Everyone,
>
[quoted text clipped - 26 lines]
>
> EVAC

BitSet does have a clone. There wouldn't be any way to compute a.and(b)
without either mutating a, or cloning a and then mutating the clone.

If its that important to your design, I would create a wrapper class
called ImmutableBitSet which will handle the cloning of new bitsets for
you.
Erick Crouse - 20 Nov 2006 03:41 GMT
Thanks Daniel.

> > Hello Everyone,
> >
[quoted text clipped - 33 lines]
> called ImmutableBitSet which will handle the cloning of new bitsets for
> you.
Daniel Pitts - 20 Nov 2006 18:45 GMT
> Thanks Daniel.

As Andrew Thompson might say, your future lack of top posting will be
thanks enough.

You're welcome,
Daniel.
Mark Rafn - 20 Nov 2006 04:18 GMT
>I need a question answered concerning the BitSet class. The problem is
>that the public methods which manipulate a set of bits requires a
[quoted text clipped - 5 lines]
>A.and(B);   // Results in a different A (B is unchanged)
>A.xor(B);    // Again results in a different A (B is unchanged)

Right.  That's how it works.

>The problem with this is that I prefer both A and B BitSet objects
>remain the same while resulting in a new BitSet object say C using an
>interface similar to -> public BitSet and( BitSet A, BitSet B) -
>BitSet.

something like:
public static BitSet andBitSets(BitSet A, BitSet B) {
   BitSet retval = (BitSet)A.clone();
   retval.and(B);
   return retval;
}

>With the current implementation of BitSet I would need to do
>some sort of clone implementation (which seems like overkill)

BitSet implements Clonable.  You don't need to do anything but call it.

>Can anyone suggest what they would do in this situation where the
>implementation operates either operand?

Use clone() as intended.  This works just fine.  
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>


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.