gf2 2.1.0

Working in bit-space a.k.a. GF(2)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
//! [`BitStore`] is the core trait implemented by bit-arrays, bit-vectors, and bit-slices.
use crate::{
    BitSlice,
    BitVector,
    Bits,
    SetBits,
    UnsetBits,
    Unsigned,
    Words,
    rng,
};

// Standard library imports.
use std::{
    fmt::Write,
    ops::{
        Bound,
        RangeBounds,
    },
};

#[doc = include_str!("../docs/store.md")]
pub trait BitStore<Word: Unsigned>: Sized {
    /// Required method that should return the number of *bit elements* in the store.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::zeros(10);
    /// assert_eq!(v.len(), 10);
    /// ```
    fn len(&self) -> usize;

    /// Required method that should return a pointer to the real words underlying the bit-store as an [`Unsigned`]
    /// slice.
    fn store(&self) -> &[Word];

    /// Required method that should return a pointer to the real words underlying the bit-store as an [`Unsigned`]
    /// mutable slice.
    fn store_mut(&mut self) -> &mut [Word];

    /// Required method that should return the number of bits from the least significant bit of `self.store()[0]` to the
    /// first bit in the store. This will be zero for bit-vectors and sets, but generally non-zero for bit-slices.
    fn offset(&self) -> u32;

    /// Required method that should return the _fewest_ number of [`Unsigned`] words needed to store the bits in the
    /// store. The return value will always be identical to `Word::words_needed(self.len())`.
    ///
    /// # Note
    /// This may not be the same as the number of words in the underlying vector of `Unsigned` words. <br>
    /// In particular, slices need not be aligned to the word boundaries of the underlying store so this method may
    /// return one fewer words than the underlying vector of `Unsigned` words.
    /// The return value will always be identical to `Word::words_needed(self.len())` so we could compute `words()` on
    /// the fly but all concrete bit-store types cache this value, which has a measurable performance impact.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector<u8> = BitVector::zeros(10);
    /// assert_eq!(v.words(), 2);
    /// ```
    fn words(&self) -> usize;

    /// Required method that should return the "word" `i` from the store.
    ///
    /// # Note
    /// This method is used to access the bits in the store _as if_ they were perfectly aligned with the word
    /// boundaries. It should appear a bit `0` in the store is located at the least significant bit of `word(0)`.
    /// This is trivial for the `BitVector` and `BitArray` types, but bit-slices typically need to synthesise
    /// appropriate "words" on demand from a couple of the "real" words that back the owning bit-vector or
    /// bit-array.
    ///
    /// For example, if the store has 18 elements, then `word(0)` should cover bit elements 0 through 7, `word(1)` the
    /// bit elements 8 through 15, and `word(2)` the bit elements 16 and 17 and the rest of the bits in that word
    /// should be zeros.
    ///
    /// The final word may not be fully occupied but the method must guarantee that unused bits are set to 0.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::zeros(10);
    /// assert_eq!(v.word(0), 0);
    /// ```
    fn word(&self, i: usize) -> Word;

    /// Required method that should set "word"  `i` in the store to the specified `value`.
    ///
    /// # Note
    /// This method is used to mutate the bits in the store as if they were perfectly aligned with the word boundaries.
    /// It should _look as if_ bit `0` in the store is located at the least significant bit of `word(0)`.
    /// This is trivial for the `BitVector` and `BitArray` types, but bit-slices typically need to synthesise
    /// appropriate "words" on demand from a couple of the "real" words that back the owning bit-vector or
    /// bit-array.
    ///
    /// For example, if the store has 18 elements, then `set_word(0,v)` should set bit elements 0 through 7,
    /// `set_word(1,v)` the bit elements 8 through 15, and `set_word(2,v)` the bit elements 16 and 17 and leave the
    /// rest of the bits in that word at zero
    ///
    /// The method must ensure that inaccessible bits in the underlying store are not changed by this call.
    ///
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(10);
    /// v.set_word(0, 0);
    /// assert_eq!(v.to_string(), "0000000000");
    /// ```
    fn set_word(&mut self, i: usize, value: Word);

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to access individual bits in the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns `true` if element `i` in the store is set to `1`.
    ///
    /// # Panics
    /// In debug mode, panics if `i` is out of bounds for the object.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(10);
    /// assert_eq!(v.get(0), true);
    /// assert_eq!(v.get(1), false);
    /// assert_eq!(v.get(8), true);
    /// assert_eq!(v.get(9), false);
    /// ```
    #[inline]
    fn get(&self, i: usize) -> bool {
        debug_assert!(i < self.len(), "index {} is out of bounds for bit-length {}", i, self.len());
        let (word_index, mask) = Word::index_and_mask(i);
        self.word(word_index) & mask != Word::ZERO
    }

    /// Returns `true` if the first bit in the store is set.
    ///
    /// # Panics
    /// In debug mode, panics if the type is empty.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::ones(10);
    /// assert!(v.first() == true);
    /// let v: BitVector = BitVector::zeros(10);
    /// assert!(v.first() == false);
    /// ```
    #[inline]
    fn first(&self) -> bool {
        debug_assert!(!self.is_empty(), "The store is empty");
        self.get(0)
    }

    /// Returns `true` if the last bit in the store is set.
    ///
    /// # Panics
    /// In debug mode, panics if the type is empty.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::ones(10);
    /// assert!(v.last() == true);
    /// let v: BitVector = BitVector::zeros(10);
    /// assert!(v.last() == false);
    /// ```
    #[inline]
    fn last(&self) -> bool {
        debug_assert!(!self.is_empty(), "The store is empty");
        self.get(self.len() - 1)
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to change individual bits in the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Sets the bit at index `i` to the boolean `val` and returns a *reference* to the store.
    ///
    /// # Panics
    /// In debug mode, panics if `i` is out of bounds for the store for chaining.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(10);
    /// v.set(0, true);
    /// assert_eq!(v.to_string(), "1000000000");
    /// v.set(1, true);
    /// assert_eq!(v.to_string(), "1100000000");
    /// v.set(1, false);
    /// assert_eq!(v.to_string(), "1000000000");
    /// ```
    #[inline]
    fn set(&mut self, i: usize, val: bool) -> &mut Self {
        debug_assert!(i < self.len(), "index {i} is out of bounds for a store of length {}", self.len());
        let (word_index, mask) = Word::index_and_mask(i);
        let word = self.word(word_index);
        let current_value = (word & mask) != Word::ZERO;
        if current_value != val {
            self.set_word(word_index, word ^ mask);
        }
        self
    }

    /// Flips the bit at index `i` and returns a *reference* to the store for chaining.
    ///
    /// # Panics
    /// In debug mode, panics if `i` is out of bounds for the store for chaining.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::ones(10);
    /// v.flip(0);
    /// assert_eq!(v.to_string(), "0111111111");
    /// v.flip(1);
    /// assert_eq!(v.to_string(), "0011111111");
    /// v.flip(9);
    /// assert_eq!(v.to_string(), "0011111110");
    /// ```
    #[inline]
    fn flip(&mut self, i: usize) -> &mut Self {
        debug_assert!(i < self.len(), "index {i} is out of bounds for a store of length {}", self.len());
        let (word_index, mask) = Word::index_and_mask(i);
        self.set_word(word_index, self.word(word_index) ^ mask);
        self
    }

    /// Swaps the bits at indices `i0` and `i1` and returns a *reference* to the store for chaining.
    ///
    /// # Panics
    /// In debug mode, panics if either of the indices is out of bounds for the store for chaining.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(10);
    /// v.set(0, true);
    /// assert_eq!(v.to_string(), "1000000000");
    /// v.swap(0, 1);
    /// assert_eq!(v.to_string(), "0100000000");
    /// v.swap(0, 1);
    /// assert_eq!(v.to_string(), "1000000000");
    /// v.swap(0, 9);
    /// assert_eq!(v.to_string(), "0000000001");
    /// v.swap(0, 9);
    /// assert_eq!(v.to_string(), "1000000000");
    /// ```
    #[inline]
    fn swap(&mut self, i0: usize, i1: usize) -> &mut Self {
        debug_assert!(i0 < self.len(), "index {i0} is out of bounds for a store of length {}", self.len());
        debug_assert!(i1 < self.len(), "index {i1} is out of bounds for a store of length {}", self.len());
        if i0 != i1 {
            let (word0, mask0) = Word::index_and_mask(i0);
            let (word1, mask1) = Word::index_and_mask(i1);
            let val0 = (self.word(word0) & mask0) != Word::ZERO;
            let val1 = (self.word(word1) & mask1) != Word::ZERO;
            if val0 != val1 {
                if word0 == word1 {
                    // Both bits are in the same word
                    self.set_word(word0, self.word(word0) ^ mask0 ^ mask1);
                }
                else {
                    // Bits are in different words
                    self.set_word(word0, self.word(word0) ^ mask0);
                    self.set_word(word1, self.word(word1) ^ mask1);
                }
            }
        }
        self
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to query the overall state of the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns `true` if the store is empty.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::new();
    /// assert!(v.is_empty());
    /// v.push(true);
    /// assert!(!v.is_empty());
    /// ```
    #[inline]
    fn is_empty(&self) -> bool { self.len() == 0 }

    /// Returns `true` if at least one bit is set to `1` in the store.
    ///
    /// # Note
    /// Empty types are considered to have no set bits.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(12);
    /// assert!(v.any() == false);
    /// v.set_all(true);
    /// assert!(v.any() == true);
    /// ```
    #[inline]
    fn any(&self) -> bool {
        // If the type is empty, it trivially has no set bits.
        // NOTE: Formally, the "logical connective" for any() is `OR` with the identity `false`.
        for i in 0..self.words() {
            if self.word(i) != Word::ZERO {
                return true;
            }
        }
        false
    }

    /// Returns `true` if all of the bits are set to `1` in the store.
    ///
    /// # Note
    /// Empty types are considered to have all set bits.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(12);
    /// assert!(v.all() == false);
    /// v.set(1, true);
    /// assert!(v.all() == false);
    /// v.set_all(true);
    /// assert!(v.all() == true);
    /// ```
    fn all(&self) -> bool {
        // NOTE: Formally, the "logical connective" for all() is `AND` with the identity `TRUE`.
        if self.is_empty() {
            return true;
        }

        // Check the fully occupied words ...
        for i in 0..self.words() - 1 {
            if self.word(i) != Word::MAX {
                return false;
            }
        }

        // Handle the last word which may not be fully occupied.
        #[allow(clippy::cast_possible_truncation)]
        let unused_bits = (Word::UBITS - self.len() % Word::UBITS) as u32;
        let last_word_max = Word::MAX.unbounded_shr(unused_bits);
        if self.word(self.words() - 1) != last_word_max {
            return false;
        }
        true
    }

    /// Returns `true` if none of the bits are set to `1` in the store.
    ///
    /// # Note
    /// Empty types are considered to have no unset bits.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(12);
    /// assert!(v.none() == true);
    /// v.set(1, true);
    /// assert!(v.none() == false);
    /// ```
    #[inline]
    fn none(&self) -> bool {
        // If the bit-vector is empty, it trivially has no unset bits.
        // NOTE: Formally, the "logical connective" for none() is `AND` with the identity `TRUE`.
        !self.any()
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to change all bits in the store in one call.
    // ----------------------------------------------------------------------------------------------------------------

    /// Sets all bits in the store to the boolean value `v` and returns a *reference* to the store for chaining.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(10);
    /// v.set_all(true);
    /// assert_eq!(v.to_string(), "1111111111");
    /// ```
    fn set_all(&mut self, v: bool) -> &mut Self {
        let value = if v { Word::MAX } else { Word::ZERO };
        for word_index in 0..self.words() {
            self.set_word(word_index, value);
        }
        self
    }

    /// Flips all bits in the store and returns a *reference* to it for chaining.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::ones(10);
    /// v.flip_all();
    /// assert_eq!(v.to_string(), "0000000000");
    /// ```
    fn flip_all(&mut self) -> &mut Self {
        for word_index in 0..self.words() {
            self.set_word(word_index, !self.word(word_index));
        }
        self
    }

    /// Returns a new bit-vector that is the result of flipping all the bits in this bit-store.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let u: gf2::BitVector = gf2::BitVector::ones(3);
    /// let v = u.flipped();
    /// assert_eq!(u.to_binary_string(), "111");
    /// assert_eq!(v.to_binary_string(), "000");
    /// ```
    fn flipped(&self) -> BitVector<Word> {
        let mut result: BitVector<Word> = BitVector::from_store(self);
        result.flip_all();
        result
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to copy bits into the store from other sources.
    // ----------------------------------------------------------------------------------------------------------------

    /// Copies the bits from an unsigned `src` value.
    ///
    /// # Notes:
    /// 1. The size of the store *must* match the number of bits in the source type.
    /// 2. We allow *any* unsigned word source, e.g. copying a single `u64` into a `BitVector<u8>` of size 64.
    /// 3. The least-significant bit of the source becomes the bit at index 0 in the store.
    ///
    /// # Example
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(16);
    /// let src: u16 = 0b1010101010101010;
    /// v.copy_unsigned(src);
    /// assert_eq!(v.to_string(), "0101010101010101");
    /// ```
    fn copy_unsigned<Src>(&mut self, src: Src) -> &mut Self
    where Src: Unsigned + TryInto<Word> {
        assert_eq!(
            self.len(),
            Src::UBITS,
            "Bit-length mismatch: store has {} bits but source has {} bits",
            self.len(),
            Src::UBITS
        );

        // Try to convert `src` into a `Word` if possible.
        if let Ok(word) = src.try_into() {
            // Conversion succeeded: The `src` was converted to a `Word` without loss of information. Use it.
            self.set_word(0, word);
        }
        else {
            // The `src` word is too big to fit into a `Word` so nibble bits from it one `Word` at a time.
            let num_words = Src::UBITS / Word::UBITS;
            let mut word: Word;
            let mut src = src;
            for word_index in 0..num_words {
                // Extract the next `Word` from `src`. This works because `Word` is smaller than `Src`.
                unsafe { word = std::mem::transmute_copy(&src) };

                // Store the extracted `Word`.
                self.set_word(word_index, word);

                // Shift `src` down to get the next `Word` into position.
                src >>= Word::BITS;
            }
        }
        self
    }

    /// Copies the bits from an iterator of unsigned values.
    ///
    /// # Notes:
    /// 1. The size of the store *must* match the total number of bits in the iterator, i.e. `n * Src::UBITS`.
    /// 2. We allow *any* unsigned word source, e.g. copying `u8` values into a `BitVector<u16>` of size 16.
    /// 3. The least-significant bit of the first source word becomes bit index 0 in the store; subsequent sources fill
    ///    consecutive ranges of `Src::UBITS` bits.
    /// 4. The iterator must report an exact length (i.e. implement `ExactSizeIterator`).
    ///
    /// # Example
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(16);
    /// let src = [0b1010_1010_u8, 0b1100_1100_u8];
    /// v.copy_unsigneds(src);
    /// assert_eq!(v.to_string(), "0101010100110011");
    /// ```
    fn copy_unsigneds<Src, SrcIter>(&mut self, src_iter: SrcIter) -> &mut Self
    where
        Src: Unsigned + TryInto<Word>,
        SrcIter: IntoIterator<Item = Src>,
        <SrcIter as IntoIterator>::IntoIter: ExactSizeIterator,
    {
        let iter = src_iter.into_iter();
        let iter_bits = iter.len() * Src::UBITS;
        assert_eq!(
            self.len(),
            iter_bits,
            "Bit-length mismatch: store has {} bits but the iterator provides {} bits",
            self.len(),
            iter_bits
        );

        let mut offset = 0usize;
        for src in iter {
            let end = offset + Src::UBITS;
            self.slice_mut(offset..end).copy_unsigned(src);
            offset = end;
        }
        self
    }

    /// Fills this bit-store with the bits from _any_ other bit-store `src` of the same length.
    ///
    /// # Note
    /// This is one of the few methods in the library that _doesn't_ require the two stores to have the same underlying
    /// `Unsigned` word type for their storage -- i.e., the `Word` type for `self` may differ from the `SrcWord` type
    /// for the bit-store `src`. You can use it to convert between different `Word` type stores (e.g., from
    /// `BitVector<u32>` to `BitVector<u8>`) as long as the sizes match.
    ///
    /// # Panics
    /// This method panics if the number of elements in this store and the `src` do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let src: BitVector =
    ///     BitVector::from_string("010101010101010101010101010101010101010101010101010101010101").unwrap();
    /// let mut dst: BitVector = BitVector::zeros(src.len());
    /// dst.copy_store(&src);
    /// assert_eq!(dst.to_string(), src.to_string());
    /// let src: BitVector<u8> = BitVector::from_string("1011001110001111").unwrap();
    /// let mut dst: BitVector<u32> = BitVector::zeros(src.len());
    /// dst.copy_store(&src);
    /// assert_eq!(dst.to_string(), src.to_string());
    /// let src: BitVector<u16> = BitVector::from_string("101100111000111110110011100011111011001110001111").unwrap();
    /// let mut dst: BitVector<u8> = BitVector::zeros(src.len());
    /// dst.copy_store(&src);
    /// assert_eq!(dst.to_string(), src.to_string());
    /// ```
    fn copy_store<SrcWord, SrcStore>(&mut self, src: &SrcStore) -> &mut Self
    where
        SrcWord: Unsigned,
        SrcStore: BitStore<SrcWord>,
    {
        assert_eq!(self.len(), src.len(), "Number of bits mismatch {} != {}", self.len(), src.len());

        // Edge case.
        if self.is_empty() {
            return self;
        }

        // Fast path: source and destination words have the same bit-width so we can copy a word at a time.
        if Word::UBITS == SrcWord::UBITS {
            for i in 0..self.words() {
                let src_word = src.word(i);
                let dst_word: Word = unsafe { std::mem::transmute_copy(&src_word) };
                self.set_word(i, dst_word);
            }
            return self;
        }

        // General case: source and destination words have different bit-widths.
        // We need to nibble bits from the source as needed.
        #[allow(clippy::cast_possible_truncation)]
        for dst_word_index in 0..self.words() {
            // Determine the bit-range in the overall store covered by this destination word.
            let start_bit = dst_word_index * Word::UBITS;
            let end_bit = (start_bit + Word::UBITS).min(self.len());
            let bits_needed = end_bit - start_bit;

            // Nibble the required bits from the source store to create the destination word `dst_word`.
            let mut dst_word = Word::ZERO;
            let mut remaining = bits_needed;
            let mut src_bit = start_bit;
            while remaining > 0 {
                // Determine the source word and offset within that word for the current source bit.
                let src_word_index = src_bit / SrcWord::UBITS;
                let src_offset = src_bit % SrcWord::UBITS;
                let src_word = src.word(src_word_index);

                // Determine how many bits we can grab from the current source word.
                let available = (SrcWord::UBITS - src_offset).min(remaining);

                // Grab the required bits from the current source word.
                let mask = SrcWord::with_set_bits(0..available as u32);
                let chunk = (src_word >> (src_offset as u32)) & mask;
                let chunk_as_word: Word = match Word::try_from(chunk.as_u128()) {
                    Ok(val) => val,
                    Err(_) => unreachable!("Oops --- chunk should always fit into destination word!"),
                };

                // Place the chunk into the destination word.
                let dst_offset = src_bit - start_bit;
                dst_word |= chunk_as_word << (dst_offset as u32);

                // Update counters.
                remaining -= available;
                src_bit += available;
            }

            // Store the constructed destination word into the appropriate location.
            self.set_word(dst_word_index, dst_word);
        }
        self
    }

    /// Fills a bit-store by calling a function `f` for each bit index.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(10);
    /// v.copy_fn(|i| i % 2 == 0);
    /// assert_eq!(v.len(), 10);
    /// assert_eq!(v.to_string(), "1010101010");
    /// ```
    fn copy_fn(&mut self, f: impl Fn(usize) -> bool) -> &mut Self {
        self.set_all(false);
        for i in 0..self.len() {
            if f(i) {
                self.set(i, true);
            }
        }
        self
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to fill the store with random bits.
    // ----------------------------------------------------------------------------------------------------------------

    /// Fills the store with random bits where each bit is set with probability `p`, and the RNG is seeded to `seed`.
    /// A seed of `0` indicates we should randomly seed the RNG.
    ///
    /// # Note
    /// Probability `p` should be in the range `[0, 1]`. If `p` is outside this range, the function will return a
    /// bit-vector with all elements set or unset as appropriate.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(10);
    /// v.fill_random_biased_seeded(1.2, 42); // All bits set
    /// assert_eq!(v.count_ones(), 10);
    /// let mut u: BitVector = BitVector::zeros(10);
    /// u.fill_random_biased_seeded(0.5, 42); // Using same seed for u and v.
    /// v.fill_random_biased_seeded(0.5, 42);
    /// assert_eq!(u, v);
    /// ```
    fn fill_random_biased_seeded(&mut self, p: f64, seed: u64) -> &mut Self {
        // Note: Need `LazyLock` to make `TWO_POWER_64` `static` as `powi` is not `const`.
        static TWO_POWER_64: std::sync::LazyLock<f64> = std::sync::LazyLock::new(|| 2.0_f64.powi(64));

        if p <= 0.0 {
            return self.set_all(false);
        }
        if p >= 1.0 {
            return self.set_all(true);
        }

        // If given a non-zero seed we need to save and restore the old seed.
        let old_seed = rng::seed();
        if seed != 0 {
            rng::set_seed(seed);
        }

        // Scale p by 2^64 to remove floating point arithmetic from the main loop below.
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let scaled_p = (*TWO_POWER_64 * p) as u64;

        // Start with all zeros and set each bit with probability `p`.
        self.set_all(false);
        for i in 0..self.len() {
            if rng::u64() < scaled_p {
                self.set(i, true);
            }
        }

        // Restore the old RNG seed.
        if seed != 0 {
            rng::set_seed(old_seed);
        }

        self
    }

    /// Fills the store with random bits where each bit is set with probability `p`, and the RNG is seeded using the
    /// system clock.
    ///
    /// # Note
    /// Probability `p` should be in the range `[0, 1]`. If `p` is outside this range, the function will return a
    /// bit-vector with all elements set or unset as appropriate.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(10);
    /// v.fill_random_biased(0.5);
    /// assert_eq!(v.len(), 10);
    /// v.fill_random_biased(1.2); // All bits set
    /// assert_eq!(v.count_ones(), 10);
    /// ```
    fn fill_random_biased(&mut self, p: f64) -> &mut Self { self.fill_random_biased_seeded(p, 0) }

    /// Fills the store with random bits where each bit is set with probability `0.5`, and the RNG is seeded to `seed`.
    /// A seed of `0` indicates we should randomly seed the RNG.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(10);
    /// let mut u: BitVector = BitVector::zeros(10);
    /// u.fill_random_seeded(42); // Using same seed for u and v.
    /// v.fill_random_seeded(42);
    /// assert_eq!(u, v);
    /// ```
    fn fill_random_seeded(&mut self, seed: u64) -> &mut Self { self.fill_random_biased_seeded(0.5, seed) }

    /// Fills the store with random bits where each bit is set with probability `0.5`, and the RNG is seeded using the
    /// clock.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut u: BitVector = BitVector::zeros(10);
    /// u.fill_random();
    /// assert_eq!(u.len(), 10);
    /// ```
    fn fill_random(&mut self) -> &mut Self { self.fill_random_biased_seeded(0.5, 0) }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to count the number of set and unset bits in the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns the number of set bits in the store.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(12);
    /// assert_eq!(v.count_ones(), 0);
    /// v.set_all(true);
    /// assert_eq!(v.count_ones(), 12);
    /// ```
    fn count_ones(&self) -> usize {
        let mut count = 0;
        for i in 0..self.words() {
            count += self.word(i).count_ones() as usize;
        }
        count
    }

    /// Returns the number of unset bits in the store.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(12);
    /// assert_eq!(v.count_zeros(), 12);
    /// v.set(1, true);
    /// assert_eq!(v.count_zeros(), 11);
    /// v.set_all(true);
    /// assert_eq!(v.count_zeros(), 0);
    /// ```
    fn count_zeros(&self) -> usize { self.len() - self.count_ones() }

    /// Returns the number of leading zeros in the store.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(37);
    /// assert_eq!(v.leading_zeros(), 37);
    /// v.set(27, true);
    /// assert_eq!(v.leading_zeros(), 27);
    /// let v: BitVector = BitVector::ones(10);
    /// assert_eq!(v.leading_zeros(), 0, "v = {v} so expected leading zeros to be 0");
    /// let v: BitVector = BitVector::unit(3, 41);
    /// assert_eq!(v.leading_zeros(), 3);
    /// ```
    fn leading_zeros(&self) -> usize {
        // Note: Even if the last word is partially occupied, we know any unused bits are zeros.
        for i in 0..self.words() {
            let word = self.word(i);
            if word != Word::ZERO {
                return i * Word::UBITS + word.trailing_zeros() as usize;
            }
        }
        // Note: This handles the case where the bit-vector or slice is all zeros.
        self.len()
    }

    /// Returns the number of trailing zeros in the store.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(27);
    /// assert_eq!(v.trailing_zeros(), 27);
    /// v.set(0, true);
    /// assert_eq!(v.trailing_zeros(), 26);
    /// ```
    fn trailing_zeros(&self) -> usize {
        if self.is_empty() {
            return 0;
        }
        // The last occupied word may have some unused bits that we need to subtract.
        let last_word = self.words() - 1;
        let unused_bits = Word::UBITS - (self.len() % Word::UBITS);
        for i in (0..=last_word).rev() {
            if self.word(i) != Word::ZERO {
                return (last_word - i) * Word::UBITS + self.word(i).leading_zeros() as usize - unused_bits;
            }
        }
        self.len()
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to find set bits in the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns the index of the first *set* bit in the store or `None` if no bits are set.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector<u8> = BitVector::zeros(37);
    /// assert_eq!(v.first_set(), None);
    /// v.set(2, true);
    /// assert_eq!(v.first_set(), Some(2));
    /// v.set(2, false);
    /// assert_eq!(v.first_set(), None);
    /// v.set(27, true);
    /// assert_eq!(v.first_set(), Some(27));
    /// v.clear();
    /// assert_eq!(v.first_set(), None);
    /// ```
    fn first_set(&self) -> Option<usize> {
        // Iterate forward looking for a word with a set bit and use the lowest of those ...
        // Remember that any unused bits in the final word are guaranteed to be unset.
        for i in 0..self.words() {
            let word = self.word(i);
            if let Some(loc) = word.lowest_set_bit() {
                return Some(i * Word::UBITS + loc as usize);
            }
        }
        None
    }

    /// Returns the index of the last *set* bit in the store or `None` if no bits are set.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(37);
    /// assert_eq!(v.last_set(), None);
    /// v.set(2, true);
    /// assert_eq!(v.last_set(), Some(2));
    /// v.set(2, false);
    /// assert_eq!(v.last_set(), None);
    /// v.set(27, true);
    /// assert_eq!(v.last_set(), Some(27));
    /// v.clear();
    /// assert_eq!(v.last_set(), None);
    /// ```
    fn last_set(&self) -> Option<usize> {
        // Iterate backwards looking for a word with a set bit and use the highest of those ...
        // Remember that any unused bits in the final word are guaranteed to be unset.
        for i in (0..self.words()).rev() {
            let word = self.word(i);
            if let Some(loc) = word.highest_set_bit() {
                return Some(i * Word::UBITS + loc as usize);
            }
        }
        None
    }

    /// Returns the index of the next set bit after `index` in the store or `None` if no more set bits exist.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(37);
    /// assert_eq!(v.next_set(0), None);
    /// v.set(2, true);
    /// v.set(27, true);
    /// assert_eq!(v.next_set(0), Some(2));
    /// assert_eq!(v.next_set(2), Some(27));
    /// assert_eq!(v.next_set(27), None);
    /// ```
    fn next_set(&self, index: usize) -> Option<usize> {
        // Start our search at index + 1.
        let index = index + 1;

        // Perhaps we are off the end? (This also handles the case of an empty type).
        if index >= self.len() {
            return None;
        }

        // Where is that starting index located in the word store?
        let (word_index, bit) = Word::index_and_offset(index);

        // Iterate forward looking for a word with a new set bit and use the lowest one ...
        for i in word_index..self.words() {
            let mut word = self.word(i);
            if i == word_index {
                // First word -- turn all the bits before our starting bit into zeros so we don't see them as set.
                word.reset_bits(0..bit);
            }
            if let Some(loc) = word.lowest_set_bit() {
                return Some(i * Word::UBITS + loc as usize);
            }
        }
        None
    }

    /// Returns the index of the previous set bit before `index` in the store or `None` if no previous set bits exist.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::zeros(37);
    /// assert_eq!(v.previous_set(0), None);
    /// v.set(2, true);
    /// v.set(27, true);
    /// assert_eq!(v.previous_set(37), Some(27));
    /// assert_eq!(v.previous_set(27), Some(2));
    /// assert_eq!(v.previous_set(2), None);
    /// ```
    fn previous_set(&self, index: usize) -> Option<usize> {
        // Edge case: If the type is empty or we are at the start, there are no previous set bits.
        if self.is_empty() || index == 0 {
            return None;
        }

        // Silently fix large indices and also adjust the index down a slot.
        let index = if index >= self.len() { self.len() - 1 } else { index - 1 };

        // Where is that starting index located in the word store?
        let (word_index, bit) = Word::index_and_offset(index);

        // Iterate backwards looking for a word with a *new* set bit and use the highest of those ...
        for i in (0..=word_index).rev() {
            let mut word = self.word(i);
            if i == word_index {
                // First word -- turn all higher bits after our starting bit into zeros so we don't see them as set.
                word.reset_bits(bit + 1..);
            }
            if let Some(loc) = word.highest_set_bit() {
                return Some(i * Word::UBITS + loc as usize);
            }
        }
        None
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to find unset bits in the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns the index of the first unset bit in the store or `None` if all bits are set.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector<u8> = BitVector::ones(39);
    /// assert_eq!(v.first_unset(), None);
    /// v.set(2, false);
    /// assert_eq!(v.first_unset(), Some(2));
    /// v.set(2, true);
    /// assert_eq!(v.first_unset(), None);
    /// v.set(27, false);
    /// assert_eq!(v.first_unset(), Some(27));
    /// v.clear();
    /// assert_eq!(v.first_unset(), None);
    /// ```
    fn first_unset(&self) -> Option<usize> {
        // Iterate forward looking for a word with an unset bit and use the lowest of those ...
        for i in 0..self.words() {
            let mut word = self.word(i);
            if i == self.words() - 1 {
                // Final word may have some unused zero bits that we need to replace with ones.
                let last_occupied_bit = Word::bit_offset(self.len() - 1);
                word.set_bits(last_occupied_bit + 1..);
            }
            if let Some(loc) = word.lowest_unset_bit() {
                return Some(i * Word::UBITS + loc as usize);
            }
        }
        None
    }

    /// Returns the index of the last unset bit in the store or `None` if all bits are set.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::ones(37);
    /// assert_eq!(v.last_unset(), None);
    /// v.set(2, false);
    /// assert_eq!(v.last_unset(), Some(2));
    /// v.set(2, true);
    /// assert_eq!(v.last_unset(), None);
    /// v.set(27, false);
    /// assert_eq!(v.last_unset(), Some(27));
    /// v.clear();
    /// assert_eq!(v.last_unset(), None);
    /// ```
    fn last_unset(&self) -> Option<usize> {
        // Iterate backwards looking for a word with an unset bit and use the highest of those ...
        for i in (0..self.words()).rev() {
            let mut word = self.word(i);
            if i == self.words() - 1 {
                // Final word may have some unused zero bits that we need to replace with ones.
                let last_occupied_bit = Word::bit_offset(self.len() - 1);
                word.set_bits(last_occupied_bit + 1..);
            }
            if let Some(loc) = word.highest_unset_bit() {
                return Some(i * Word::UBITS + loc as usize);
            }
        }
        None
    }

    /// Returns the index of the next unset bit after `index` in the store or `None` if no more set bits exist.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector<u8> = BitVector::ones(37);
    /// assert_eq!(v.next_unset(0), None);
    /// v.set(2, false);
    /// v.set(27, false);
    /// assert_eq!(v.next_unset(0), Some(2));
    /// assert_eq!(v.next_unset(2), Some(27));
    /// assert_eq!(v.next_unset(27), None);
    /// ```
    fn next_unset(&self, index: usize) -> Option<usize> {
        // Start our search at index + 1.
        let index = index + 1;

        // Perhaps we are off the end of the type (also handles the case of an empty type).
        if index >= self.len() {
            return None;
        }

        // Where is that starting index in the word store?
        let (word_index, bit) = Word::index_and_offset(index);

        // Iterate forward looking for a word with a new unset bit and use the lowest of those ...
        for i in word_index..self.words() {
            let mut word = self.word(i);
            if i == word_index {
                // Current word -- turn all the bits before our starting bit into ones so we don't see them as unset.
                word.set_bits(0..bit);
            }
            if i == self.words() - 1 {
                // Final word may have some unused zero bits that we need to replace with ones.
                let last_occupied_bit = Word::bit_offset(self.len() - 1);
                word.set_bits(last_occupied_bit + 1..);
            }
            if let Some(loc) = word.lowest_unset_bit() {
                return Some(i * Word::UBITS + loc as usize);
            }
        }
        None
    }

    /// Returns the index of the previous unset bit before `index` in the store or `None` if no more set bits exist.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector<u8> = BitVector::ones(37);
    /// assert_eq!(v.previous_unset(0), None);
    /// v.set(2, false);
    /// v.set(27, false);
    /// assert_eq!(v.previous_unset(37), Some(27));
    /// assert_eq!(v.previous_unset(27), Some(2));
    /// assert_eq!(v.previous_unset(2), None);
    /// ```
    fn previous_unset(&self, index: usize) -> Option<usize> {
        // Edge case: If the type is empty or we are at the start, there are no previous set bits.
        if self.is_empty() || index == 0 {
            return None;
        }

        // Silently fix large indices and also adjust the index down a slot.
        let index = if index >= self.len() { self.len() - 1 } else { index - 1 };

        // Where is that starting index in the word store?
        let (word_index, bit) = Word::index_and_offset(index);

        // Iterate backwards looking for a word with a new *unset* bit and use the highest of those ...
        for i in (0..=word_index).rev() {
            let mut word = self.word(i);
            if i == word_index {
                // Current word -- turn all higher bits after our starting bit into ones so we don't see them as unset.
                word.set_bits(bit + 1..);
            }
            if i == self.words() - 1 {
                // Final word may have some unused high order zero bits that we need to replace with ones.
                let last_occupied_bit = Word::bit_offset(self.len() - 1);
                word.set_bits(last_occupied_bit + 1..);
            }
            if let Some(loc) = word.highest_unset_bit() {
                return Some(i * Word::UBITS + loc as usize);
            }
        }
        None
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to create iterators over the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns an iterator over all the bits in the bit-store with the associated type `bool`.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::ones(10);
    /// v.set(5, false);
    /// let bits: Vec<bool> = v.bits().collect();
    /// assert_eq!(bits, vec![true, true, true, true, true, false, true, true, true, true]);
    /// ```
    #[inline]
    fn bits(&self) -> Bits<'_, Self, Word> { Bits::new(self) }

    /// Returns an iterator over all the bits in the bit-store that are set to `true`. The associated type is `usize`.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// type BV = BitVector<u8>;
    /// let v = BV::ones(10);
    /// let set_indices: Vec<usize> = v.set_bits().collect();
    /// assert_eq!(set_indices, vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    /// ```
    #[inline]
    fn set_bits(&self) -> SetBits<'_, Self, Word> { SetBits::new(self) }

    /// Returns an iterator over all the bits in the bit-store that are set to `false`. The associated type is `usize`.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// type BV = BitVector<u8>;
    /// let v = BV::zeros(10);
    /// let unset_indices: Vec<usize> = v.unset_bits().collect();
    /// assert_eq!(unset_indices, vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    /// ```
    #[inline]
    fn unset_bits(&self) -> UnsetBits<'_, Self, Word> { UnsetBits::new(self) }

    /// Returns an iterator over the "words" in the bit-store with some [`Unsigned`] associated type.
    ///
    /// # Note
    /// This *behaves as if* the bits were copied into a vector of `Unsigned` words starting at bit 0 of word 0.
    /// The iterator returns the words from that vector in order.
    ///
    /// The final `Unsigned` word may not be fully occupied but any unused bits will be zeros.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector<u8> = BitVector::ones(10);
    /// let words: Vec<u8> = v.store_words().collect();
    /// assert_eq!(words, vec![0b1111_1111_u8, 0b0000_0011_u8]);
    /// ```
    #[inline]
    fn store_words(&self) -> Words<'_, Self, Word> { Words::new(self) }

    /// Returns a copy of the words underlying this bit-store.
    ///
    /// # Note
    /// The last word in the vector may not be fully occupied but unused slots will be all zeros.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector<u8> = BitVector::ones(10);
    /// let words = v.to_words();
    /// assert_eq!(words, vec!(255, 3));
    /// ```
    #[inline]
    fn to_words(&self) -> Vec<Word> { self.store_words().collect() }

    /// Fills the provided destination vector with the words underlying this bit-store.
    ///
    /// # Note
    /// The destination is cleared before writing. The last word in the vector may not be fully occupied but unused
    /// slots will be all zeros.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector<u8> = BitVector::ones(10);
    /// let mut words: Vec<u8> = Vec::new();
    /// v.to_words_into(&mut words);
    /// assert_eq!(words, vec![255, 3]);
    /// ```
    #[inline]
    fn to_words_into(&self, dst: &mut Vec<Word>) {
        dst.clear();
        dst.extend(self.store_words());
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to create slices of the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns a [`BitSlice`] of this store for the bits in the half-open range `[range.start, range.end)`.
    ///
    /// # Panics
    /// This method panics if `self` is empty or if the range is not valid.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(10);
    /// let s1 = v.slice(1..5);
    /// assert_eq!(s1.to_string(), "0101");
    /// ```
    fn slice<R: RangeBounds<usize>>(&self, range: R) -> BitSlice<'_, Word> {
        let (start, end) = self.start_and_end_for(range);
        let offset = self.offset() as usize;
        BitSlice::new(self.store(), start + offset, end + offset)
    }

    /// Returns a mutable [`BitSlice`] of this store for the bits in the half-open range `[range.start, range.end)`.
    ///
    /// # Panics
    /// This method panics if `self` is empty or if the range is not valid.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector = BitVector::alternating(10);
    /// let mut slice = v.slice_mut(1..5);
    /// assert_eq!(slice.to_string(), "0101");
    /// ```
    fn slice_mut<R: RangeBounds<usize>>(&mut self, range: R) -> BitSlice<'_, Word> {
        let (start, end) = self.start_and_end_for(range);
        let offset = self.offset() as usize;
        BitSlice::new_mut(self.store_mut(), start + offset, end + offset)
    }

    /// Helper method: Consumes a range and returns the corresponding `start` and `end` as a pair of `usize`s.
    ///
    /// The bits of interest are in the half-open interval `[start, end)`
    ///
    /// # Panics
    /// Panics if the vector of `words` is empty or the `range` is invalid. <br>
    /// The `range` cannot extend beyond the last bit of the vector of `words`.
    fn start_and_end_for<R: RangeBounds<usize>>(&self, range: R) -> (usize, usize) {
        assert!(!self.is_empty(), "cannot create a bit-slice from an empty bit-vector");

        let start = match range.start_bound() {
            Bound::Included(start) => *start,
            Bound::Excluded(start) => *start + 1,
            Bound::Unbounded => 0,
        };

        let end = match range.end_bound() {
            Bound::Included(end) => *end + 1,
            Bound::Excluded(end) => *end,
            Bound::Unbounded => self.len(),
        };

        // Check that the range is non-empty and does not extend beyond the end of the vector of words.
        assert!(start < end, "bit range [{start}, {end}) is invalid");
        assert!(end <= self.len(), "bit range extends beyond the end of the type");

        (start, end)
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods for sub-vector extraction.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns a *clone* of the elements in the half-open range `[begin, end)` as a new bit-vector.
    ///
    /// # Panics
    /// This method panics if the range is not valid.
    ///
    /// # Example
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(10);
    /// let mut s = v.sub(1..5);
    /// assert_eq!(s.to_string(), "0101");
    /// s.set_all(true);
    /// assert_eq!(s.to_string(), "1111");
    /// assert_eq!(v.to_string(), "1010101010");
    /// ```
    fn sub<R: RangeBounds<usize>>(&self, range: R) -> BitVector<Word> { BitVector::from_store(&self.slice(range)) }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods that split the store into two parts.
    // ----------------------------------------------------------------------------------------------------------------

    /// Splits a bit-store into two parts at the given index.
    // The parts are stored in the bit-vectors `left` and `right`.
    ///
    /// On return, `left` contains the bits from the start of the bit-store up to but not including `at` and `right`
    /// contains the bits from `at` to the end of the bit-store. The `self` bit-store is not modified.
    ///
    /// # Note
    /// This allows one to reuse the `left` and `right` outputs without having to allocate new bit-vectors.
    /// This is useful when implementing iterative algorithms that need to split a bit-store into two parts repeatedly.
    ///
    /// # Panics
    /// This method panics if the split point is beyond the end of the bit-store.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(10);
    /// let mut left: BitVector = BitVector::new();
    /// let mut right: BitVector = BitVector::new();
    /// v.split_at_into(5, &mut left, &mut right);
    /// assert_eq!(left.to_string(), "10101");
    /// assert_eq!(right.to_string(), "01010");
    /// ```
    fn split_at_into(&self, at: usize, left: &mut BitVector<Word>, right: &mut BitVector<Word>) {
        assert!(at <= self.len(), "split point {at} is beyond the end of the bit-vector");
        left.clear();
        right.clear();
        left.append_store(&self.slice(0..at));
        right.append_store(&self.slice(at..self.len()));
    }

    /// Splits a bit-store into two parts at the given index. The parts are returned as a pair of new bit-vectors.
    ///
    /// On return, `left` contains the bits from the start of the bit-store up to but not including `at` and `right`
    /// contains the bits from `at` to the end of the bit-store. The `self` bit-store is not modified.
    ///
    /// # Panics
    /// This method panics if the split point is beyond the end of the bit-store.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(10);
    /// let (left, right) = v.split_at(5);
    /// assert_eq!(left.to_string(), "10101");
    /// assert_eq!(right.to_string(), "01010");
    /// ```
    fn split_at(&self, at: usize) -> (BitVector<Word>, BitVector<Word>) {
        let mut left = BitVector::new();
        let mut right = BitVector::new();
        self.split_at_into(at, &mut left, &mut right);
        (left, right)
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods tto return copies of the store interleaved with zeros.
    // ----------------------------------------------------------------------------------------------------------------

    /// Riffle this bit-vector into another bit-vector `dst` with the bits in the original vector interleaved with
    /// zeros.
    ///
    /// If this bit-vector in binary is `abcde` then the destination bit-vector will have the elements `a0b0c0d0e`.
    /// Note there is no last `0` bit in the destination bit-vector here.
    ///
    /// # Note
    /// This method is useful in various repeated squaring algorithms where we want to re-use the same `dst` bit-vector
    /// for each iteration.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v: BitVector<u8> = BitVector::ones(10);
    /// let mut dst: BitVector<u8> = BitVector::zeros(10);
    /// v.riffled_into(&mut dst);
    /// assert_eq!(dst.to_string(), "1010101010101010101");
    /// ```
    fn riffled_into(&self, dst: &mut BitVector<Word>) {
        // Not a lot to do if the bit-vector is empty or has only one bit. With two bits `ab` we return `a0b`.
        let ln = self.len();
        if ln < 2 {
            dst.resize(ln);
            dst.copy_store(self);
            return;
        }

        // Make sure `dst` is large enough to hold the riffled bits (a bit too big but we fix that below).
        dst.resize(2 * ln);
        let dst_words = dst.words();

        // Riffle each word in the store into two adjacent words in `dst`.
        for i in 0..self.words() {
            let (lo, hi) = self.word(i).riffle();
            dst.set_word(2 * i, lo);

            // Note that `hi` may be completely superfluous ...
            if 2 * i + 1 < dst_words {
                dst.set_word(2 * i + 1, hi);
            }
        }

        // If this bit-store was say `abcde` then `dst` will now be `a0b0c0d0e0`. Pop the last 0.
        dst.pop();
    }

    /// Returns a new bit-vector that is the result of riffling the bits in this bit-store with zeros.
    ///
    /// If this bit-store in binary is say `abcde` then the output bit-vector will have the elements `a0b0c0d0e`.
    /// Note there is no last `0` bit in the output bit-vector here.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector<u8> = BitVector::ones(10);
    /// let dst = v.riffled();
    /// assert_eq!(dst.to_string(), "1010101010101010101");
    /// ```
    #[must_use]
    fn riffled(&self) -> BitVector<Word> {
        let mut dst = BitVector::new();
        self.riffled_into(&mut dst);
        dst
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods for vector-vector dot products and convolutions.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns the scalar dot product of this bit-store and another bit-store.
    ///
    /// For any pair of vector-like types, the dot product is the "sum" of the element-wise products of the two
    /// operands. In GF(2) the sum is modulo 2 and, by convention, the scalar output is a boolean value.
    /// ```text
    /// u * v = \sum_i u[i] v[i]
    /// ```
    /// where the sum is over all the indices so the two operands must have the same length.
    ///
    /// # Note
    /// - We have also implemented the `Mul` trait to overload the `*` operator to return the same thing.
    /// - See the `BitMatrix` documentation for matrix-vector, vector-matrix, and matrix-matrix multiplication.
    ///
    /// # Panics
    /// In debug mode, panics if the lengths of `self` and `rhs` do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v1: BitVector = BitVector::alternating(10);
    /// let v2: BitVector = BitVector::alternating(10) >> 1;
    /// assert_eq!(v1.dot(&v1), true);
    /// assert_eq!(v1.dot(&v2), false);
    /// ```
    fn dot<Rhs: BitStore<Word>>(&self, rhs: &Rhs) -> bool {
        debug_assert_eq!(self.len(), rhs.len(), "Length mismatch {} != {}", self.len(), rhs.len());
        let mut sum = Word::ZERO;
        for i in 0..self.words() {
            sum ^= self.word(i) & rhs.word(i);
        }
        sum.count_ones() % 2 == 1
    }

    /// Returns the convolution of this bit-store and another bit-store as a new bit-vector.
    ///
    /// The *convolution* of any two vector-like objects `u` & `v` is defined as:
    ///
    /// ```text
    ///   (u * v)[k] = \sum_j u[j] v[k-j+1]
    /// ```
    ///
    /// where the sum is taken over all `j` such that the indices in the formula are valid.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let lhs: BitVector = BitVector::ones(3);
    /// let rhs: BitVector = BitVector::ones(2);
    /// let result = lhs.convolved_with(&rhs);
    /// assert_eq!(result.to_string(), "1001");
    /// ```
    fn convolved_with<Rhs: BitStore<Word>>(&self, rhs: &Rhs) -> BitVector<Word> {
        // Edge case: if either vector is empty then the convolution is empty.
        if self.is_empty() || rhs.is_empty() {
            return BitVector::new();
        }

        // Generally the result will have length `self.len() + other.len() - 1` (could be all zeros).
        let mut result = BitVector::zeros(self.len() + rhs.len() - 1);

        // If either vector is all zeros then the convolution is all zeros.
        if self.none() || rhs.first_set().is_none() {
            return result;
        }

        // Only need to consider words in `rhs` up to and including the one holding its final set bit.
        // We have already checked that `rhs` is not all zeros so we know there is a last set bit!
        let rhs_words_end = Word::word_index(rhs.last_set().unwrap()) + 1;

        // Initialize `result` by copying the live words from `rhs`
        for i in 0..rhs_words_end {
            result.set_word(i, rhs.word(i));
        }

        // Work backwards from our last set bit (which we know exists as we checked `self` is not all zeros).
        for i in (0..self.last_set().unwrap()).rev() {
            let mut prev = Word::ZERO;
            for j in 0..result.words() {
                let left = prev >> (Word::UBITS - 1);
                prev = result.word(j);
                result.set_word(j, prev << 1_u32 | left);
            }
            if self.get(i) {
                for j in 0..rhs_words_end {
                    result.set_word(j, result.word(j) ^ rhs.word(j));
                }
            }
        }
        result
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated string representation methods.
    // ----------------------------------------------------------------------------------------------------------------

    /// Returns the "binary" string representation of the bits in the bit-store.
    ///
    /// The output is a string of 0's and 1's without any spaces, commas, or other formatting.
    ///
    /// # Note
    /// The output is in *vector-order* e.g. `v_0v_1v_2v_3...` where `v_0` is the first element in the vector/slice.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(10);
    /// assert_eq!(v.to_binary_string(), "1010101010");
    /// ```
    fn to_binary_string(&self) -> String { self.to_custom_binary_string("", "", "") }

    /// Returns the "pretty" string representation of the bits in the bit-store.
    ///
    /// The output is a string of 0's and 1's with spaces between each bit, and the whole thing enclosed in square
    /// brackets.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(10);
    /// assert_eq!(v.to_pretty_string(), "[1 0 1 0 1 0 1 0 1 0]");
    /// let v: BitVector = BitVector::new();
    /// assert_eq!(v.to_pretty_string(), "[]");
    /// ```
    fn to_pretty_string(&self) -> String { self.to_custom_binary_string(" ", "[", "]") }

    /// Returns a customised "binary" string representation of the bits in the bit-store.
    ///
    /// The elements are output as 0's and 1's with a custom `separator` between them.
    /// You can also provide custom `left` and `right` delimiters for the whole vector or slice.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(10);
    /// assert_eq!(v.to_custom_binary_string(" ", "[", "]"), "[1 0 1 0 1 0 1 0 1 0]");
    fn to_custom_binary_string(&self, separator: &str, left: &str, right: &str) -> String {
        // Edge case: No elements in the type, return the empty string.
        if self.is_empty() {
            return format!("{left}{right}");
        }

        // Start with the pure binary string representation.
        // We preallocate space for that string -- generally this is a little more than strictly necessary.
        let n_words = self.words();
        let mut binary_string = String::with_capacity(n_words * Word::UBITS);

        // Reverse each word to vector-order and get its binary string representation.
        for i in 0..n_words {
            let word = self.word(i).reverse_bits();
            write!(binary_string, "{:0width$b}", word, width = Word::UBITS).unwrap();
        }

        // The last block may not be fully occupied and padded with spurious zeros so we truncate to the correct length.
        binary_string.truncate(self.len());

        // If we were given a custom separator, add it between the elements ...
        let str = if separator.is_empty() {
            binary_string
        }
        else {
            binary_string.chars().fold(String::new(), |mut acc, c| {
                if !acc.is_empty() {
                    acc.push_str(separator);
                }
                acc.push(c);
                acc
            })
        };

        // Add any custom left and right delimiters ...
        let mut result = String::with_capacity(str.len() + left.len() + right.len());
        write!(result, "{left}{str}{right}").unwrap();
        result
    }

    /// Returns the "hex" string representation of the bits in the bit-store.
    ///
    /// The output is a string of hex characters without any spaces, commas, or other formatting. <br>
    /// The string may have a two character *suffix* of the form ".base" where `base` is one of 2, 4 or 8. <br>
    /// All hex characters encode 4 bits: "0X0" -> `0b0000`, "0X1" -> `0b0001`, ..., "0XF" -> `0b1111`. <br>
    /// The three possible ".base" suffixes allow for bit-vectors whose length is not a multiple of 4. <br>
    /// Empty bit-vectors are represented as the empty string.
    ///
    /// - `0X1`   is the hex representation of the bit-vector `0001` => length 4.
    /// - `0X1.8` is the hex representation of the bit-vector `001`  => length 3.
    /// - `0X1.4` is the hex representation of the bit-vector `01`   => length 2.
    /// - `0X1.2` is the hex representation of the bit-vector `1`    => length 1.
    ///
    /// # Note
    /// The output is in *vector-order*.
    /// If "h0" is the first hex digit in the output string, you can print it as four binary digits `v_0v_1v_2v_3`. <br>
    /// For example, if h0 = "A" which is `1010` in binary, then v = 1010.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::new();
    /// assert_eq!(v.to_hex_string(), "");
    /// let v: BitVector = BitVector::ones(4);
    /// assert_eq!(v.to_hex_string(), "F");
    /// let v: BitVector = BitVector::ones(5);
    /// assert_eq!(v.to_hex_string(), "F1.2");
    /// let v: BitVector = BitVector::alternating(8);
    /// assert_eq!(v.to_hex_string(), "AA");
    /// ```
    fn to_hex_string(&self) -> String {
        // Edge case: No bits in the type, return the empty string.
        if self.is_empty() {
            return String::new();
        }

        // The number of digits in the output string. Generally hexadecimal but the last may be to a lower base.
        let len = self.len();
        let digits = len.div_ceil(4);

        // Preallocate space allowing for a possible lower base on the last digit such as "_2".
        let mut result = String::with_capacity(digits + 2);

        // The number of hex digits per word.
        let hex_digits_per_word = Word::UBITS / 4;

        // Reverse each word to vector-order and get its hex string rep (fully padded with zeros to the left).
        for i in 0..self.words() {
            let word = self.word(i).reverse_bits();
            write!(result, "{word:0hex_digits_per_word$X}").unwrap();
        }

        // Last word may not be fully occupied and padded with spurious zeros so we truncate the output string.
        result.truncate(digits);

        // Every four elements in the bit-vector is encoded by a single hex digit but `len` may not be a multiple of 4.
        let k = len % 4;
        if k != 0 {
            // That last hex digit should really be encoded to a lower base -- 2, 4 or 8.
            // We compute the number represented by the trailing `k` elements in the bit-vector.
            let mut num = 0;
            for i in 0..k {
                if self.get(len - 1 - i) {
                    num |= 1 << i;
                }
            }

            // Convert that number to hex & use it to *replace* the last hex digit in our `result` string.
            let result_len = result.len();
            result.truncate(result_len - 1);
            write!(result, "{num:X}").unwrap();

            // Append the appropriate base to the output string so that the last digit can be interpreted properly.
            write!(result, ".{}", 1 << k).unwrap();
        }
        result
    }

    /// Returns a multi-line string describing the bit-store in some detail.
    ///
    /// # Note
    /// This method is useful for debugging and testing but you should not rely on the output format which may change.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v: BitVector = BitVector::alternating(20);
    /// println!("{}", v.describe());
    /// ```
    fn describe(&self) -> String {
        let mut result = String::new();
        writeln!(result, "binary format:         {}", self.to_binary_string()).unwrap();
        writeln!(result, "hex format:            {}", self.to_hex_string()).unwrap();
        writeln!(result, "number of bits:        {}", self.len()).unwrap();
        writeln!(result, "number of set bits:    {}", self.count_ones()).unwrap();
        writeln!(result, "number of unset bits:  {}", self.count_zeros()).unwrap();
        writeln!(result, "bits per word:         {}", Word::UBITS).unwrap();
        writeln!(result, "word count:            {}", self.words()).unwrap();
        writeln!(result, "words in hex:         [").unwrap();
        for i in 0..self.words() {
            writeln!(result, "  {:0width$X}", self.word(i), width = Word::UBITS / 4).unwrap();
        }
        writeln!(result, "]").unwrap();
        result
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to perform bit-shifts of the store.
    // ----------------------------------------------------------------------------------------------------------------

    /// Shifts all bits in the bit-store to the left by `shift` places.
    ///
    /// Shifting is in *vector-order* so if `v = [v0,v1,v2,v3]` then `v.left_shift(1)` is `[v1,v2,v3,0]`.
    /// New bits entering from the right are set to zero.
    ///
    /// # Note
    /// - Left shifting in vector-order is the same as right shifting in bit-order.
    /// - Only accessible bits are affected by the shift.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut bv: gf2::BitVector = gf2::BitVector::ones(10);
    /// bv.left_shift(3);
    /// assert_eq!(bv.to_string(), "1111111000");
    /// ```
    fn left_shift(&mut self, shift: usize) {
        // Edge cases where there is nothing to do.
        if shift == 0 || self.len() == 0 {
            return;
        }

        // Perhaps we have shifted all the contents out and we are left with all zeros.
        if shift >= self.len() {
            self.set_all(false);
            return;
        }

        // For larger shifts, we can efficiently shift by whole words first.
        let word_shift = shift / Word::UBITS;
        let end_word = self.words() - word_shift;

        // Do the whole word shifts first, pushing in zero words to fill the empty slots.
        let mut shift = shift;
        if word_shift > 0 {
            // Shift the words.
            for i in 0..end_word {
                self.set_word(i, self.word(i + word_shift));
            }

            // Fill in the high order words with zeros.
            for i in end_word..self.words() {
                self.set_word(i, Word::ZERO);
            }

            // How many bits are left to shift?
            shift -= word_shift * Word::UBITS;
        }

        // Perhaps there are some partial word shifts left to do.
        if shift != 0 {
            // Do the "interior" words where the shift moves bits from one word to the next.
            if end_word > 0 {
                let shift_complement = Word::UBITS - shift;
                for i in 0..end_word - 1 {
                    let lo = self.word(i) >> shift;
                    let hi = self.word(i + 1) << shift_complement;
                    self.set_word(i, lo | hi);
                }
            }

            // Do the last word.
            let value = self.word(end_word - 1);
            self.set_word(end_word - 1, value >> shift);
        }
    }

    /// Returns a new bit-vector that is the result of left-shifting this bit-store by `shift` places.
    ///
    /// Shifting is in *vector-order* so if `v = [v0,v1,v2,v3]` then `v.left_shifted(1)` returns `[v1,v2,v3,0]`.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v1: gf2::BitVector = gf2::BitVector::ones(10);
    /// let v2 = v1.left_shifted(3);
    /// assert_eq!(v1.to_string(), "1111111111");
    /// assert_eq!(v2.to_string(), "1111111000");
    /// let s2 = v2.slice(0..7);
    /// assert_eq!(s2.to_string(), "1111111");
    /// let v3 = s2.left_shifted(3);
    /// assert_eq!(v3.to_string(), "1111000");
    /// assert_eq!(s2.to_string(), "1111111");
    /// ```
    fn left_shifted(&self, shift: usize) -> BitVector<Word> {
        let mut result: BitVector<Word> = BitVector::from_store(self);
        result.left_shift(shift);
        result
    }

    /// Shifts all bits in the bit-store to the right by `shift` places.
    ///
    /// Shifting is in *vector-order* so if `v = [v0,v1,v2,v3]` then `v.right_shift(1)` is `[0,v0,v1,v2]`.
    /// New bits entering from the right are set to zero.
    ///
    /// # Note
    /// - Right shifting in vector-order is the same as left shifting in bit-order.
    /// - Only accessible bits are affected by the shift.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut bv: gf2::BitVector = gf2::BitVector::ones(10);
    /// bv.right_shift(3);
    /// assert_eq!(bv.to_string(), "0001111111");
    /// ```
    fn right_shift(&mut self, shift: usize) {
        // Edge cases where there is nothing to do.
        if shift == 0 || self.len() == 0 {
            return;
        }

        // Perhaps we have shifted all the contents out and we are left with all zeros.
        if shift >= self.len() {
            self.set_all(false);
            return;
        }

        // For larger shifts, we can efficiently shift by whole words first.
        let word_shift = shift / Word::UBITS;

        // Do the whole word shifts first, pushing in zero words to fill the empty slots.
        let mut shift = shift;
        if word_shift > 0 {
            // Shift whole words -- starting at the end of the vector.
            for i in (word_shift..self.words()).rev() {
                self.set_word(i, self.word(i - word_shift));
            }

            // Fill in the low order words with zeros.
            for i in 0..word_shift {
                self.set_word(i, Word::ZERO);
            }

            // How many bits are left to shift?
            shift -= word_shift * Word::UBITS;
        }

        // Perhaps there are some partial word shifts left to do.
        if shift != 0 {
            // Do the "interior" words where the shift moves bits from one word to the next.
            let shift_complement = Word::UBITS - shift;
            for i in (word_shift + 1..self.words()).rev() {
                let lo = self.word(i - 1) >> shift_complement;
                let hi = self.word(i) << shift;
                self.set_word(i, lo | hi);
            }

            // Do the "first" word.
            let value = self.word(word_shift);
            self.set_word(word_shift, value << shift);
        }
    }

    /// Returns a new bit-vector that is the result of right-shifting this bit-store by `shift` places.
    ///
    /// Shifting is in *vector-order* so if `v = [v0,v1,v2,v3]` then `v.left_shifted(1)` returns `[v1,v2,v3,0]`.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v1: gf2::BitVector = gf2::BitVector::ones(10);
    /// let v2 = v1.right_shifted(3);
    /// assert_eq!(v1.to_string(), "1111111111");
    /// assert_eq!(v2.to_string(), "0001111111");
    /// let s2 = v2.slice(0..7);
    /// assert_eq!(s2.to_string(), "0001111");
    /// let v3 = s2.right_shifted(3);
    /// assert_eq!(v3.to_string(), "0000001");
    /// assert_eq!(s2.to_string(), "0001111");
    /// ```
    fn right_shifted(&self, shift: usize) -> BitVector<Word> {
        let mut result: BitVector<Word> = BitVector::from_store(self);
        result.right_shift(shift);
        result
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated methods to perform bitwise operations between stores.
    // ----------------------------------------------------------------------------------------------------------------

    /// Performs an in-place bitwise XOR of this bit-store with another bit-store.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// v1.xor_eq(&v2);
    /// assert_eq!(v1.to_string(), "1111111111");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// ```
    fn xor_eq<Rhs: BitStore<Word>>(&mut self, rhs: &Rhs) {
        assert_eq!(self.len(), rhs.len(), "Length mismatch {} != {}", self.len(), rhs.len());
        for i in 0..self.words() {
            let word = self.word(i) ^ rhs.word(i);
            self.set_word(i, word);
        }
    }

    /// Returns a new bit-vector that is the result of XOR'ing this bit-store with another.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// let v3 = v1.xor(&v2);
    /// assert_eq!(v1.to_string(), "1010101010");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// assert_eq!(v3.to_string(), "1111111111");
    /// ```
    fn xor<Rhs: BitStore<Word>>(&self, rhs: &Rhs) -> BitVector<Word> {
        let mut result: BitVector<Word> = BitVector::from_store(self);
        result.xor_eq(rhs);
        result
    }

    /// Performs an in-place bitwise AND of this bit-store with another bit-store.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// v1.and_eq(&v2);
    /// assert_eq!(v1.to_string(), "0000000000");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// ```
    fn and_eq<Rhs: BitStore<Word>>(&mut self, rhs: &Rhs) {
        assert_eq!(self.len(), rhs.len(), "Length mismatch {} != {}", self.len(), rhs.len());
        for i in 0..self.words() {
            let word = self.word(i) & rhs.word(i);
            self.set_word(i, word);
        }
    }

    /// Returns a new bit-vector that is the result of AND'ing this bit-store with another.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// let v3 = v1.and(&v2);
    /// assert_eq!(v1.to_string(), "1010101010");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// assert_eq!(v3.to_string(), "0000000000");
    /// ```
    fn and<Rhs: BitStore<Word>>(&self, rhs: &Rhs) -> BitVector<Word> {
        let mut result: BitVector<Word> = BitVector::from_store(self);
        result.and_eq(rhs);
        result
    }

    /// Performs an in-place bitwise OR of this bit-store with another bit-store.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// v1.or_eq(&v2);
    /// assert_eq!(v1.to_string(), "1111111111");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// ```
    fn or_eq<Rhs: BitStore<Word>>(&mut self, rhs: &Rhs) {
        assert_eq!(self.len(), rhs.len(), "Length mismatch {} != {}", self.len(), rhs.len());
        for i in 0..self.words() {
            let word = self.word(i) | rhs.word(i);
            self.set_word(i, word);
        }
    }

    /// Returns a new bit-vector that is the result of OR'ing this bit-store with another.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// let v3 = v1.or(&v2);
    /// assert_eq!(v1.to_string(), "1010101010");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// assert_eq!(v3.to_string(), "1111111111");
    /// ```
    fn or<Rhs: BitStore<Word>>(&self, rhs: &Rhs) -> BitVector<Word> {
        let mut result: BitVector<Word> = BitVector::from_store(self);
        result.or_eq(rhs);
        result
    }

    // ----------------------------------------------------------------------------------------------------------------
    // Associated arithmetic-operations-in-place methods.
    // ----------------------------------------------------------------------------------------------------------------

    /// Performs an in-place addition of this bit-store with another bit-store.
    ///
    /// # Note
    /// In GF(2), addition is equivalent to bitwise XOR.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// v1.plus_eq(&v2);
    /// assert_eq!(v1.to_string(), "1111111111");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// ```
    fn plus_eq<Rhs: BitStore<Word>>(&mut self, rhs: &Rhs) { self.xor_eq(rhs); }

    /// Returns a new bit-vector that is the result of adding this bit-store to another.
    ///
    /// # Note
    /// In GF(2), addition is equivalent to bitwise XOR.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// let v3 = v1.plus(&v2);
    /// assert_eq!(v1.to_string(), "1010101010");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// assert_eq!(v3.to_string(), "1111111111");
    /// ```
    fn plus<Rhs: BitStore<Word>>(&self, rhs: &Rhs) -> BitVector<Word> {
        let mut result: BitVector<Word> = BitVector::from_store(self);
        result.xor_eq(rhs);
        result
    }

    /// Performs an in-place subtraction of another bit-store from this bit-store.
    ///
    /// # Note
    /// In GF(2), addition and subtraction are both equivalent to bitwise XOR.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let mut v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// v1.minus_eq(&v2);
    /// assert_eq!(v1.to_string(), "1111111111");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// ```
    fn minus_eq<Rhs: BitStore<Word>>(&mut self, rhs: &Rhs) { self.xor_eq(rhs); }

    /// Returns a new bit-vector that is the result of subtracting a bit-store from this one.
    ///
    /// # Note
    /// In GF(2), addition and subtraction are both equivalent to bitwise XOR.
    ///
    /// # Panics
    /// This method panics if the lengths of the input operands do not match.
    ///
    /// # Examples
    /// ```
    /// use gf2::*;
    /// let v1: gf2::BitVector = gf2::BitVector::from_string("1010101010").unwrap();
    /// let v2: gf2::BitVector = gf2::BitVector::from_string("0101010101").unwrap();
    /// let v3 = v1.minus(&v2);
    /// assert_eq!(v1.to_string(), "1010101010");
    /// assert_eq!(v2.to_string(), "0101010101");
    /// assert_eq!(v3.to_string(), "1111111111");
    /// ```
    fn minus<Rhs: BitStore<Word>>(&self, rhs: &Rhs) -> BitVector<Word> {
        let mut result: BitVector<Word> = BitVector::from_store(self);
        result.xor_eq(rhs);
        result
    }
}