Revised BitSet

1,164 views
Skip to first unread message

Will Fitzgerald

unread,
May 20, 2011, 12:52:24 PM5/20/11
to golan...@googlegroups.com
I've revamped the BitSet package.


It now takes a stronger view of these as a 'set of (boolean) bits', so some of the methods for treating these as strings of bits are gone. (Better to use the big package, especially with recent revisions, for that).

BitSets will now grow dynamically to the largest set bit.

Many of the methods return *BitSet, so chaining is possible.

The SetBit and Bit methods were changed to Set and Test. ClearBit is now Clear, and the method to clear all bits is ClearAll

The underlying word size was changed to 32 bits. This was slightly faster in my benchmarks than 64 bit words. I'd be glad to know if (a) people think this was a mistake, and (b) if there is a way to adjust the word size based on the underlying architecture.

As always, comments are appreciated. I'm sure there are some remaining infelicities.

Will

Will Fitzgerald

unread,
Mar 11, 2013, 9:42:18 PM3/11/13
to golan...@googlegroups.com
After a long hiatus, I've updated this BitSet implementation to use 1.x style formatting and installation.

Dean Schulze

unread,
Sep 20, 2013, 9:15:02 PM9/20/13
to golan...@googlegroups.com
Will,

Thanks for bitset.  You mention that I could also use math/big, but I don't see the same functionality there.

Where would I find similar functionality in math/big?

Thanks.

Carlos Castillo

unread,
Sep 22, 2013, 8:12:09 AM9/22/13
to golan...@googlegroups.com
With fixed size integers, like uint16, and the bitwise operators (&&, ||, ^, as well as bit-shifts <<, >>) you can implement a finite (in this case 16) element bit set.

By using a math/big.Int, you can create an arbitrarily large bit set, the type even provides methods to get and set individual bits (no need for shifts), as well as the required .And (Intersection), .Or (Union), and .AndNot (Difference) methods.

Here is a sample type built using big.Ints: http://play.golang.org/p/NpHns5EBnQ 

simon place

unread,
Sep 22, 2013, 2:58:28 PM9/22/13
to golan...@googlegroups.com
i guess this should be;

func (b *BitSet) Clear(bit uint) *BitSet {
b.bits.SetBit(&b.bits, int(bit), 0)
return b
}

rather than;

func (b *BitSet) Clear(bit uint) *BitSet {
b.bits.SetBit(&b.bits, int(bit), 1)
return b

Carlos Castillo

unread,
Sep 22, 2013, 8:35:55 PM9/22/13
to simon place, golang-nuts
My arch-nemeses Copy and Paste strike again!


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/5i3l0CXDiBg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Carlos Castillo

Kamil Kisiel

unread,
Sep 26, 2013, 2:29:04 PM9/26/13
to golan...@googlegroups.com
I was bored the other day so I had a stab at implementing a package that does this:


As always, comments & PR's are welcome.

simon place

unread,
Sep 30, 2013, 8:08:49 PM9/30/13
to golan...@googlegroups.com
this is bit set, not any set, so shouldn't it be called that?


Reply all
Reply to author
Forward
0 new messages