You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35-10Lines changed: 35 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,32 +2,57 @@
2
2
3
3
Basic, pure Ruby bit field. Pretty fast (for what it is) and memory efficient. Works well for Bloom filters (the reason I wrote it).
4
4
5
-
Written in 2007 and not updated since then, just bringing it on to GitHub by user request. It used to be called Bitfield and was hosted at http://snippets.dzone.com/posts/show/4234 .. I will review the code and bring the docs up to date in due course.
5
+
Written in 2007 and not updated since then, just bringing it on to GitHub by user request. It used to be called BitField and was hosted at http://snippets.dzone.com/posts/show/4234 .. I will review the code and bring the docs up to date in due course.
6
6
7
7
## Installation
8
8
9
-
gem install bitarray
9
+
```ruby
10
+
gem install bitarray
11
+
```
10
12
11
13
## Examples
12
14
13
15
To use:
14
16
15
-
require 'bitarray'
17
+
```ruby
18
+
require'bitarray'
19
+
```
16
20
17
-
Create a bit field 1000 bits wide:
21
+
Create a bit array 1000 bits wide:
18
22
19
-
ba = BitArray.new(1000)
23
+
```ruby
24
+
ba =BitArray.new(1000)
25
+
```
20
26
21
27
Setting and reading bits:
22
28
23
-
ba[100] = 1
24
-
ba[100] .. => 1
25
-
ba[100] = 0
29
+
```ruby
30
+
ba[100] =1
31
+
ba[100]
32
+
#=> 1
33
+
34
+
ba[100] =0
35
+
ba[100]
36
+
#=> 0
37
+
```
26
38
27
39
More:
28
40
29
-
ba.to_s = "10101000101010101" (example)
30
-
ba.total_set .. => 10 (example - 10 bits are set to "1")
41
+
```ruby
42
+
ba =BitArray.new(20)
43
+
[1,3,5,9,11,13,15].each { |i| ba[i] =1 }
44
+
ba.to_s
45
+
#=> "01010100010101010000"
46
+
ba.total_set
47
+
#=> 7
48
+
```
49
+
50
+
## History
51
+
- v5 (added support for flags being on by default, instead of off)
52
+
- v4 (fixed bug where setting 0 bits to 0 caused a set to 1)
53
+
- v3 (supports dynamic bitwidths for array elements.. now doing 32 bit widths default)
54
+
- v2 (now uses 1 << y, rather than 2 ** y .. it's 21.8 times faster!)
0 commit comments