@@ -26,31 +26,52 @@ oldValue := ba.ChkClr(5) // returns the value at position 5 before clearing it
2626v := bit.One
2727ba.Swap (5 , &v) // swaps the value at position 5 with v
2828
29- b1 := bitarray.FromStr (" 110110010" )
30- b2 := bitarray.FromStr (" 0011011" )
31- bitarray.SwapRange (&b1, &b2, 3 ) // swap bits starting at position 3
32- fmt.Println (" b1: " , &b1, " b2: " , &b2) // b1 = "110101110", b2 = "0011100"
33-
34-
3529ba.SetAll () // sets all the bits
3630ba.ClrAll () // clears all the bits
3731```
3832
33+ ## Range Operations
34+ There are two procedures ` CopyRange ` and ` SwapRange ` to help work with a range of bits. A` Range ` represents
35+ a span over a certain number of bits starting at a specific position.
36+ ### CopyRange
37+ ``` go
38+ b1 := FromStr (" 11001001" )
39+ b2 := FromStr (" 00101110" )
40+ // copy 3 bits starting at position 3 from `b2` into `b1` starting at position 1
41+ bitarray.CopyRange (b1.Range (1 , 3 ), b2.Range (3 , 3 ))
42+ fmt.Println (" b1: " , &b1) // b1 = "10111001"
43+ ```
44+ ` CopyRange ` copies number of bits equal to that of the smaller range.
45+
46+ ### SwapRange
47+ ``` go
48+ b1 := FromStr (" 11001001" )
49+ b2 := FromStr (" 00101110" )
50+ // swap 3 bits starting at position 3 from `b2` with `b1` starting at position 1
51+ bitarray.SwapRange (b1.Range (1 , 3 ), b2.Range (3 , 3 ))
52+ fmt.Println (" b1: " , b1, " b2: " , b2) // b1 = "10111001" b2 = "00110010"
53+ ```
54+ ` SwapRange ` swaps number of bits equal to that of the smaller range.
55+
3956## Tests and Benchmarks
4057Tests and benchmarks can be found in ba_test.go.
4158```
4259goos: windows
4360goarch: amd64
4461pkg: github.com/c2akula/bitarray
45- BenchmarkBitArray/chk-8 20200155 311 ns/op 0 B/op 0 allocs/op
46- BenchmarkBitArray/put-8 1000000000 1.41 ns/op 0 B/op 0 allocs/op
47- BenchmarkBitArray/set-8 1000000000 1.31 ns/op 0 B/op 0 allocs/op
48- BenchmarkBitArray/tgl-8 1000000000 1.42 ns/op 0 B/op 0 allocs/op
49- BenchmarkBitArray/swap-range,_best-case-8 632504266 9.50 ns/op 0 B/op 0 allocs/op
50- BenchmarkBitArray/swap-range,_worst-case-8 33005805 185 ns/op 0 B/op 0 allocs/op
51- BenchmarkBitArray/copy-8 1000000000 5.00 ns/op 0 B/op 0 allocs/op
52- BenchmarkBitArray_Cnt/cnt_-_chk-8 13548182 442 ns/op 0 B/op 0 allocs/op
53- BenchmarkBitArray_Cnt/cnt_-_bits-8 988570477 6.06 ns/op 0 B/op 0 allocs/op
62+
63+ BenchmarkNew-8 40094756 28.1 ns/op 32 B/op 1 allocs/op
64+ BenchmarkBitArray/chk-8 3957813 297 ns/op 0 B/op 0 allocs/op
65+ BenchmarkBitArray/put-8 853075514 1.41 ns/op 0 B/op 0 allocs/op
66+ BenchmarkBitArray/set-8 846415746 1.43 ns/op 0 B/op 0 allocs/op
67+ BenchmarkBitArray/tgl-8 847111244 1.45 ns/op 0 B/op 0 allocs/op
68+ BenchmarkBitArray/swap-range,_best-case-8 80213366 14.9 ns/op 0 B/op 0 allocs/op
69+ BenchmarkBitArray/swap-range,_worst-case-8 6011475 184 ns/op 0 B/op 0 allocs/op
70+ BenchmarkBitArray/copy-8 251309268 4.76 ns/op 0 B/op 0 allocs/op
71+ BenchmarkBitArray_Cnt/cnt_-_bits-8 191233108 6.28 ns/op 0 B/op 0 allocs/op
72+ BenchmarkCopyRange/worst_case_-_unaligned_copy-8 705885 1703 ns/op 0 B/op 0 allocs/op
73+ BenchmarkCopyRange/best_case_-_aligned_copy-8 122504306 9.54 ns/op 0 B/op 0 allocs/op
74+
5475```
5576
5677# Issues
0 commit comments