bitstring 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/CONTRIBUTORS.txt +5 -0
  2. data/Changelog.txt +20 -0
  3. data/LICENCE.txt +202 -0
  4. data/NOTICE.txt +5 -0
  5. data/README.txt +63 -0
  6. data/doc/classes/BitString.html +2479 -0
  7. data/doc/classes/BitString.src/M000001.html +99 -0
  8. data/doc/classes/BitString.src/M000002.html +18 -0
  9. data/doc/classes/BitString.src/M000003.html +20 -0
  10. data/doc/classes/BitString.src/M000004.html +19 -0
  11. data/doc/classes/BitString.src/M000005.html +24 -0
  12. data/doc/classes/BitString.src/M000006.html +44 -0
  13. data/doc/classes/BitString.src/M000007.html +21 -0
  14. data/doc/classes/BitString.src/M000008.html +18 -0
  15. data/doc/classes/BitString.src/M000009.html +18 -0
  16. data/doc/classes/BitString.src/M000010.html +22 -0
  17. data/doc/classes/BitString.src/M000011.html +29 -0
  18. data/doc/classes/BitString.src/M000012.html +22 -0
  19. data/doc/classes/BitString.src/M000013.html +43 -0
  20. data/doc/classes/BitString.src/M000014.html +19 -0
  21. data/doc/classes/BitString.src/M000015.html +40 -0
  22. data/doc/classes/BitString.src/M000016.html +21 -0
  23. data/doc/classes/BitString.src/M000017.html +18 -0
  24. data/doc/classes/BitString.src/M000018.html +18 -0
  25. data/doc/classes/BitString.src/M000019.html +18 -0
  26. data/doc/classes/BitString.src/M000020.html +20 -0
  27. data/doc/classes/BitString.src/M000021.html +24 -0
  28. data/doc/classes/BitString.src/M000022.html +23 -0
  29. data/doc/classes/BitString.src/M000023.html +42 -0
  30. data/doc/classes/BitString.src/M000024.html +61 -0
  31. data/doc/classes/BitString.src/M000025.html +18 -0
  32. data/doc/classes/BitString.src/M000026.html +18 -0
  33. data/doc/classes/BitString.src/M000027.html +20 -0
  34. data/doc/classes/BitString.src/M000028.html +18 -0
  35. data/doc/classes/BitString.src/M000029.html +18 -0
  36. data/doc/classes/BitString.src/M000030.html +18 -0
  37. data/doc/classes/BitString.src/M000031.html +18 -0
  38. data/doc/classes/BitString.src/M000032.html +18 -0
  39. data/doc/created.rid +1 -0
  40. data/doc/files/lib/bitstring/operators_rb.html +122 -0
  41. data/doc/files/lib/bitstring_rb.html +153 -0
  42. data/doc/fr_class_index.html +27 -0
  43. data/doc/fr_file_index.html +28 -0
  44. data/doc/fr_method_index.html +60 -0
  45. data/doc/index.html +24 -0
  46. data/doc/rdoc-style.css +208 -0
  47. data/lib/bitstring.rb +1318 -0
  48. data/lib/bitstring/operators.rb +481 -0
  49. data/test/Rakefile +21 -0
  50. data/test/test_basic.rb +848 -0
  51. data/test/test_data.rb +24 -0
  52. data/test/test_enum.rb +671 -0
  53. data/test/test_helper.rb +3 -0
  54. data/test/test_operators.rb +454 -0
  55. metadata +121 -0
@@ -0,0 +1,5 @@
1
+ CONTRIBUTORS:
2
+ -------------
3
+
4
+ * Rodent of Unusual Size <The.Rodent.of.Unusual.Size@GMail.Com> (Ken Coar)
5
+ Initial creator and maintainer.
@@ -0,0 +1,20 @@
1
+ -*- mode: change-log; mode: auto-fill; -*-
2
+
3
+ 1.0.0 2010-02-04 Initial release as a Ruby gem
4
+
5
+ 0.1.0
6
+ o Added population() and select().
7
+ o Added the example fragments.
8
+ o Added mask() and each().
9
+ o Added the [start,length]= handling. (FREQ #27742)
10
+ o Fixed a problem with the reporting of the BadDigit internal
11
+ exception.
12
+ o Added an 'Examples' section to each method header. (Not very
13
+ many filled in yet, though.)
14
+ o Normalised data checking and made Array/BitString/Integer/String
15
+ acceptable in more places (like the bitwise operators).
16
+
17
+ 0.1.0 2010-01-27 Initial import
18
+ -------------------------------
19
+ Still lots to do, but at least we're in a state suitable
20
+ to check in to the repository..
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,5 @@
1
+ BitString Ruby package
2
+ Copyright © 2010 Ken Coar
3
+
4
+ This file MUST contain attributions for any third-party
5
+ inclusions or contributions.
@@ -0,0 +1,63 @@
1
+ BitString - 'Arrays' of bits for Ruby
2
+ =====================================
3
+
4
+ * http://rubyforge.org/projects/bitstring/ - project page
5
+ * http://bitstring.rubyforge.org/rdoc/ - RDoc documentation
6
+
7
+ DESCRIPTION:
8
+ ------------
9
+
10
+ BitString is a Ruby class for providing access to lists of bits in
11
+ an 'Array'-like manner. Direct addressing, subranging, masking,
12
+ e cetera.
13
+
14
+
15
+ FEATURES:
16
+ ---------
17
+
18
+ * Many of the features of the Array class are available for BitString.
19
+
20
+
21
+ PROBLEMS:
22
+ ---------
23
+
24
+ * None known.
25
+
26
+
27
+ SYNOPSIS:
28
+ ---------
29
+
30
+ require 'bitstring'
31
+ bs = BitString.new('100101001')
32
+ bs = BitString.new('100101001', 9)
33
+
34
+ REQUIREMENTS:
35
+ -------------
36
+
37
+ * Gem 'versionomy'
38
+
39
+
40
+ INSTALL:
41
+ --------
42
+
43
+ BitString is installed using the canonical method:
44
+
45
+ sudo gem install bitstring
46
+
47
+
48
+ LICENSE:
49
+ --------
50
+
51
+ Copyright © 2010 Ken Coar
52
+
53
+ Licensed under the Apache License, Version 2.0 (the "License"); you
54
+ may not use this file except in compliance with the License. You may
55
+ obtain a copy of the License at
56
+
57
+ http://www.apache.org/licenses/LICENSE-2.0
58
+
59
+ Unless required by applicable law or agreed to in writing, software
60
+ distributed under the License is distributed on an "AS IS" BASIS,
61
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
62
+ implied. See the License for the specific language governing
63
+ permissions and limitations under the License.
@@ -0,0 +1,2479 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Class: BitString</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Class</strong></td>
53
+ <td class="class-name-in-header">BitString</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/bitstring_rb.html">
59
+ lib/bitstring.rb
60
+ </a>
61
+ <br />
62
+ <a href="../files/lib/bitstring/operators_rb.html">
63
+ lib/bitstring/operators.rb
64
+ </a>
65
+ <br />
66
+ </td>
67
+ </tr>
68
+
69
+ <tr class="top-aligned-row">
70
+ <td><strong>Parent:</strong></td>
71
+ <td>
72
+ Object
73
+ </td>
74
+ </tr>
75
+ </table>
76
+ </div>
77
+ <!-- banner header -->
78
+
79
+ <div id="bodyContent">
80
+
81
+
82
+
83
+ <div id="contextContent">
84
+
85
+
86
+
87
+ </div>
88
+
89
+ <div id="method-list">
90
+ <h3 class="section-bar">Methods</h3>
91
+
92
+ <div class="name-list">
93
+ <a href="#M000026">&amp;</a>&nbsp;&nbsp;
94
+ <a href="#M000027">&lt;&lt;</a>&nbsp;&nbsp;
95
+ <a href="#M000034">==</a>&nbsp;&nbsp;
96
+ <a href="#M000028">&gt;&gt;</a>&nbsp;&nbsp;
97
+ <a href="#M000029">[]</a>&nbsp;&nbsp;
98
+ <a href="#M000030">[]=</a>&nbsp;&nbsp;
99
+ <a href="#M000031">^</a>&nbsp;&nbsp;
100
+ <a href="#M000002">bounded?</a>&nbsp;&nbsp;
101
+ <a href="#M000003">clear</a>&nbsp;&nbsp;
102
+ <a href="#M000004">clear!</a>&nbsp;&nbsp;
103
+ <a href="#M000005">each</a>&nbsp;&nbsp;
104
+ <a href="#M000006">from_i</a>&nbsp;&nbsp;
105
+ <a href="#M000007">grow</a>&nbsp;&nbsp;
106
+ <a href="#M000008">grow!</a>&nbsp;&nbsp;
107
+ <a href="#M000009">length</a>&nbsp;&nbsp;
108
+ <a href="#M000010">lsb</a>&nbsp;&nbsp;
109
+ <a href="#M000012">mask</a>&nbsp;&nbsp;
110
+ <a href="#M000011">mask</a>&nbsp;&nbsp;
111
+ <a href="#M000013">msb</a>&nbsp;&nbsp;
112
+ <a href="#M000001">new</a>&nbsp;&nbsp;
113
+ <a href="#M000014">population</a>&nbsp;&nbsp;
114
+ <a href="#M000015">resize</a>&nbsp;&nbsp;
115
+ <a href="#M000016">resize!</a>&nbsp;&nbsp;
116
+ <a href="#M000017">rotate</a>&nbsp;&nbsp;
117
+ <a href="#M000018">rotate!</a>&nbsp;&nbsp;
118
+ <a href="#M000019">select</a>&nbsp;&nbsp;
119
+ <a href="#M000020">shrink</a>&nbsp;&nbsp;
120
+ <a href="#M000021">shrink!</a>&nbsp;&nbsp;
121
+ <a href="#M000022">slice</a>&nbsp;&nbsp;
122
+ <a href="#M000023">slice!</a>&nbsp;&nbsp;
123
+ <a href="#M000024">to_i</a>&nbsp;&nbsp;
124
+ <a href="#M000025">to_s</a>&nbsp;&nbsp;
125
+ <a href="#M000032">|</a>&nbsp;&nbsp;
126
+ <a href="#M000033">~</a>&nbsp;&nbsp;
127
+ </div>
128
+ </div>
129
+
130
+ </div>
131
+
132
+
133
+ <!-- if includes -->
134
+ <div id="includes">
135
+ <h3 class="section-bar">Included Modules</h3>
136
+
137
+ <div id="includes-list">
138
+ <span class="include-name">Enumerable</span>
139
+ </div>
140
+ </div>
141
+
142
+ <div id="section">
143
+
144
+
145
+ <div id="constants-list">
146
+ <h3 class="section-bar">Constants</h3>
147
+
148
+ <div class="name-list">
149
+ <table summary="Constants">
150
+ <tr class="top-aligned-row context-row">
151
+ <td class="context-item-name">Version</td>
152
+ <td>=</td>
153
+ <td class="context-item-value">Versionomy.parse('0.1.0a2')</td>
154
+ <td width="3em">&nbsp;</td>
155
+ <td class="context-item-desc">
156
+ Versionomy object for the class, recording the current version.
157
+
158
+ </td>
159
+ </tr>
160
+ <tr class="top-aligned-row context-row">
161
+ <td class="context-item-name">VERSION</td>
162
+ <td>=</td>
163
+ <td class="context-item-value">Version.to_s</td>
164
+ <td width="3em">&nbsp;</td>
165
+ <td class="context-item-desc">
166
+ Version number as a simple readable string.
167
+
168
+ </td>
169
+ </tr>
170
+ <tr class="top-aligned-row context-row">
171
+ <td class="context-item-name">LOW_END</td>
172
+ <td>=</td>
173
+ <td class="context-item-value">:low</td>
174
+ <td width="3em">&nbsp;</td>
175
+ <td class="context-item-desc">
176
+ Identifer specifying the least significant (low) end of the bitstring (used
177
+ by <em><a href="BitString.html#M000007">grow</a></em>, <em><a
178
+ href="BitString.html#M000020">shrink</a></em>, and <em><a
179
+ href="BitString.html#M000011">mask</a></em>).
180
+
181
+ </td>
182
+ </tr>
183
+ <tr class="top-aligned-row context-row">
184
+ <td class="context-item-name">HIGH_END</td>
185
+ <td>=</td>
186
+ <td class="context-item-value">:high</td>
187
+ <td width="3em">&nbsp;</td>
188
+ <td class="context-item-desc">
189
+ Identifer specifying the most significant (high) end of the bitstring (used
190
+ by <em><a href="BitString.html#M000007">grow</a></em>, <em><a
191
+ href="BitString.html#M000020">shrink</a></em>, and <em><a
192
+ href="BitString.html#M000011">mask</a></em>).
193
+
194
+ </td>
195
+ </tr>
196
+ </table>
197
+ </div>
198
+ </div>
199
+
200
+
201
+
202
+ <div id="attribute-list">
203
+ <h3 class="section-bar">Attributes</h3>
204
+
205
+ <div class="name-list">
206
+ <table>
207
+ <tr class="top-aligned-row context-row">
208
+ <td class="context-item-name">bounded</td>
209
+ <td class="context-item-value">&nbsp;[R]&nbsp;</td>
210
+ <td class="context-item-desc">
211
+ <em>Boolean</em>. Whether or not the bitstring is bounded and limited to a
212
+ specific <a href="BitString.html#M000009">length</a>. Read-only; set at
213
+ object creation.
214
+
215
+ </td>
216
+ </tr>
217
+ </table>
218
+ </div>
219
+ </div>
220
+
221
+
222
+
223
+ <!-- if method_list -->
224
+ <div id="methods">
225
+ <h3 class="section-bar">Public Class methods</h3>
226
+
227
+ <div id="method-M000012" class="method-detail">
228
+ <a name="M000012"></a>
229
+
230
+ <div class="method-heading">
231
+ <a href="#M000012" class="method-signature">
232
+ <span class="method-name">BitString.mask<i>(bitcount)</i> => <i>Integer</i><br />
233
+ </span>
234
+ </a>
235
+ </div>
236
+
237
+ <div class="method-description">
238
+ <h3>Description</h3>
239
+ <p>
240
+ Class method to return an integer value with the specified number of bits
241
+ (starting at position 0) all set to 1.
242
+ </p>
243
+ <h3>Arguments</h3>
244
+ <dl>
245
+ <dt><em>bitcount</em></dt><dd><em>Integer</em>. Number of bits to set in the <a
246
+ href="BitString.html#M000011">mask</a>.
247
+
248
+ </dd>
249
+ </dl>
250
+ <h3>Examples</h3>
251
+ <pre>
252
+ BitString.mask(5).to_s(2)
253
+ =&gt; &quot;11111&quot;
254
+ </pre>
255
+ <h3>Exceptions</h3>
256
+ <p>
257
+ <em>None</em>.
258
+ </p>
259
+ <p><a class="source-toggle" href="#"
260
+ onclick="toggleCode('M000012-source');return false;">[Source]</a></p>
261
+ <div class="method-source-code" id="M000012-source">
262
+ <pre>
263
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 908</span>
264
+ 908: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">mask</span>(<span class="ruby-identifier">bits</span>=<span class="ruby-value">1</span>)
265
+ 909: <span class="ruby-identifier">new</span>.<span class="ruby-identifier">_raise</span>(<span class="ruby-constant">NoDeficitBits</span>) <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span>)
266
+ 910: <span class="ruby-identifier">vMask</span> = <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">bits</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
267
+ 911: <span class="ruby-keyword kw">end</span>
268
+ </pre>
269
+ </div>
270
+ </div>
271
+ </div>
272
+
273
+ <div id="method-M000001" class="method-detail">
274
+ <a name="M000001"></a>
275
+
276
+ <div class="method-heading">
277
+ <a href="#M000001" class="method-signature">
278
+ <span class="method-name">new<i>([val], [bitcount])</i> => <i>BitString</i><br />
279
+ new<i>(length) {|index| block }</i> => <i>BitString</i><br />
280
+ </span>
281
+ </a>
282
+ </div>
283
+
284
+ <div class="method-description">
285
+ <h3>Description</h3>
286
+ <p>
287
+ Create a <a href="BitString.html#M000001">new</a> <em><a
288
+ href="BitString.html">BitString</a></em> object. By default, it will be
289
+ unbounded and all bits <a href="BitString.html#M000003">clear</a>.
290
+ </p>
291
+ <h3>Arguments</h3>
292
+ <dl>
293
+ <dt><em>val</em></dt><dd><em>Array</em>, <em>Integer</em>, <em>String</em>, or <em><a
294
+ href="BitString.html">BitString</a></em>. Initial value for the bitstring.
295
+ If a <em>String</em>, the value must contain only &#8216;0&#8217; and
296
+ &#8216;1&#8217; characters; if an <em>Array</em>, all elements must be 0 or
297
+ 1. Default 0.
298
+
299
+ </dd>
300
+ <dt><em>bitcount</em></dt><dd><em>Integer</em>. Optional <a href="BitString.html#M000009">length</a>
301
+ (number of bits) for a bounded bitstring.
302
+
303
+ </dd>
304
+ </dl>
305
+ <h3>Examples</h3>
306
+ <pre>
307
+ bs = BitString.new(4095)
308
+ bs.to_s
309
+ =&gt; &quot;111111111111&quot;
310
+ bs.bounded?
311
+ =&gt; false
312
+
313
+ bs = BitString.new('110000010111', 12)
314
+ bs.bounded?
315
+ =&gt; true
316
+ bs.to_i
317
+ =&gt; 3095
318
+
319
+ bs = BitString.new(12) { |pos| pos % 2 }
320
+ bs.to_s
321
+ =&gt; &quot;101010101010&quot;
322
+ </pre>
323
+ <h3>Exceptions</h3>
324
+ <dl>
325
+ <dt><tt>RangeError</tt></dt><dd><em>val</em> is a string, but contains non-binary digits.
326
+
327
+ </dd>
328
+ </dl>
329
+ <p><a class="source-toggle" href="#"
330
+ onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
331
+ <div class="method-source-code" id="M000001-source">
332
+ <pre>
333
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 289</span>
334
+ 289: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args_p</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
335
+ 290: <span class="ruby-comment cmt">#</span>
336
+ 291: <span class="ruby-comment cmt"># Two constructor scenarios:</span>
337
+ 292: <span class="ruby-comment cmt">#</span>
338
+ 293: <span class="ruby-comment cmt"># 1. With a block and an optional length (defaults to 1)</span>
339
+ 294: <span class="ruby-comment cmt"># 2. No block, and with value and length both optional.</span>
340
+ 295: <span class="ruby-comment cmt">#</span>
341
+ 296: <span class="ruby-comment cmt"># We don't do any type-checking on the arguments until later.</span>
342
+ 297: <span class="ruby-comment cmt">#</span>
343
+ 298: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">block</span>.<span class="ruby-identifier">nil?</span>)
344
+ 299: <span class="ruby-comment cmt">#</span>
345
+ 300: <span class="ruby-comment cmt"># We got a block; set up the constraints. Bitstrings constructed</span>
346
+ 301: <span class="ruby-comment cmt"># this way are bounded.</span>
347
+ 302: <span class="ruby-comment cmt">#</span>
348
+ 303: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">args_p</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span>)
349
+ 304: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'only bitstring length '</span> <span class="ruby-operator">+</span>
350
+ 305: <span class="ruby-value str">'may be specified with a block'</span>)
351
+ 306: <span class="ruby-keyword kw">end</span>
352
+ 307: <span class="ruby-ivar">@bounded</span> = <span class="ruby-keyword kw">true</span>
353
+ 308: <span class="ruby-ivar">@length</span> = <span class="ruby-identifier">args_p</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">args_p</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
354
+ 309: <span class="ruby-keyword kw">else</span>
355
+ 310: <span class="ruby-comment cmt">#</span>
356
+ 311: <span class="ruby-comment cmt"># Get value and possibly length from the argument list.</span>
357
+ 312: <span class="ruby-comment cmt">#</span>
358
+ 313: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">args_p</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">3</span>)
359
+ 314: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'wrong number of arguments '</span> <span class="ruby-operator">+</span>
360
+ 315: <span class="ruby-value str">'(must be 2 or fewer)'</span>)
361
+ 316: <span class="ruby-keyword kw">end</span>
362
+ 317: <span class="ruby-identifier">val</span> = <span class="ruby-identifier">args_p</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">||</span> <span class="ruby-value">0</span>
363
+ 318: <span class="ruby-ivar">@length</span> = <span class="ruby-identifier">args_p</span>[<span class="ruby-value">1</span>] <span class="ruby-keyword kw">if</span> (<span class="ruby-ivar">@bounded</span> = <span class="ruby-operator">!</span> <span class="ruby-identifier">args_p</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">nil?</span>)
364
+ 319: <span class="ruby-keyword kw">end</span>
365
+ 320: <span class="ruby-comment cmt">#</span>
366
+ 321: <span class="ruby-comment cmt"># Now do some validation on the arguments.</span>
367
+ 322: <span class="ruby-comment cmt">#</span>
368
+ 323: <span class="ruby-keyword kw">if</span> (<span class="ruby-ivar">@bounded</span>)
369
+ 324: <span class="ruby-keyword kw">unless</span> ((<span class="ruby-ivar">@length</span> = <span class="ruby-ivar">@length</span>.<span class="ruby-identifier">to_i</span>) <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>)
370
+ 325: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'bitstring length must be greater than 0'</span>)
371
+ 326: <span class="ruby-keyword kw">end</span>
372
+ 327: <span class="ruby-keyword kw">end</span>
373
+ 328: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">block</span>.<span class="ruby-identifier">nil?</span>)
374
+ 329: <span class="ruby-comment cmt">#</span>
375
+ 330: <span class="ruby-comment cmt"># We weren't passed a block, so get the info directly from the argument</span>
376
+ 331: <span class="ruby-comment cmt"># list.</span>
377
+ 332: <span class="ruby-comment cmt">#</span>
378
+ 333: <span class="ruby-ivar">@value</span> = <span class="ruby-identifier">_arg2int</span>(<span class="ruby-identifier">val</span>)
379
+ 334: <span class="ruby-keyword kw">else</span>
380
+ 335: <span class="ruby-comment cmt">#</span>
381
+ 336: <span class="ruby-comment cmt"># We were passed a block, so invoke it for each bit position to</span>
382
+ 337: <span class="ruby-comment cmt"># determine that bit's value from the LSB of the result.</span>
383
+ 338: <span class="ruby-comment cmt">#</span>
384
+ 339: <span class="ruby-ivar">@value</span> = <span class="ruby-value">0</span>
385
+ 340: <span class="ruby-ivar">@length</span>.<span class="ruby-identifier">times</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">self</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">i</span>).<span class="ruby-identifier">to_i</span> <span class="ruby-operator">&amp;</span> <span class="ruby-value">1</span> }
386
+ 341: <span class="ruby-keyword kw">end</span>
387
+ 342: <span class="ruby-keyword kw">end</span>
388
+ </pre>
389
+ </div>
390
+ </div>
391
+ </div>
392
+
393
+ <h3 class="section-bar">Public Instance methods</h3>
394
+
395
+ <div id="method-M000026" class="method-detail">
396
+ <a name="M000026"></a>
397
+
398
+ <div class="method-heading">
399
+ <a href="#M000026" class="method-signature">
400
+ <span class="method-name">bitstring <i>& value</i> => <i>BitString</i><br />
401
+ bitstring.&<i>(value)</i> => <i>BitString</i><br />
402
+ </span>
403
+ </a>
404
+ </div>
405
+
406
+ <div class="method-description">
407
+ <h3>Description</h3>
408
+ <p>
409
+ Perform a bitwise AND on the entire bitstring, returning a <a
410
+ href="BitString.html#M000001">new</a> <em><a
411
+ href="BitString.html">BitString</a></em> object. The <a
412
+ href="BitString.html#M000001">new</a> bitstring will have the same
413
+ boundedness as the original.
414
+ </p>
415
+ <h3>Arguments</h3>
416
+ <dl>
417
+ <dt><em>value</em></dt><dd><em><a href="BitString.html">BitString</a></em>, <em>Integer</em>, or
418
+ <em>String</em>. Value to AND with the bitstring.
419
+
420
+ </dd>
421
+ </dl>
422
+ <h3>Examples</h3>
423
+ <pre>
424
+ bs = BitString.new('111111111')
425
+ nbs = bs &amp; '111100011'
426
+ nbs.to_s
427
+ =&gt; &quot;111100011&quot;
428
+
429
+ bs = BitString.new('10101010101', 11)
430
+ nbs = bs &amp; '11110001111'
431
+ nbs.to_s
432
+ =&gt; &quot;10100000101&quot;
433
+ nbs = bs &amp; '00001110000'
434
+ nbs.to_s
435
+ =&gt; &quot;00001010000&quot;
436
+ </pre>
437
+ <h3>Exceptions</h3>
438
+ <p>
439
+ <em>None</em>.
440
+ </p>
441
+ <p><a class="source-toggle" href="#"
442
+ onclick="toggleCode('M000026-source');return false;">[Source]</a></p>
443
+ <div class="method-source-code" id="M000026-source">
444
+ <pre>
445
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 91</span>
446
+ 91: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">&amp;</span>(<span class="ruby-identifier">val</span>)
447
+ 92: <span class="ruby-identifier">val</span> = <span class="ruby-identifier">_arg2int</span>(<span class="ruby-identifier">val</span>)
448
+ 93: <span class="ruby-identifier">vMask</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">mask</span>
449
+ 94: <span class="ruby-identifier">newval</span> = (<span class="ruby-ivar">@value</span> <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">val</span>) <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">vMask</span>
450
+ 95: <span class="ruby-identifier">bounded?</span> <span class="ruby-value">? </span><span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">newval</span>, <span class="ruby-ivar">@length</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">newval</span>)
451
+ 96: <span class="ruby-keyword kw">end</span>
452
+ </pre>
453
+ </div>
454
+ </div>
455
+ </div>
456
+
457
+ <div id="method-M000027" class="method-detail">
458
+ <a name="M000027"></a>
459
+
460
+ <div class="method-heading">
461
+ <a href="#M000027" class="method-signature">
462
+ <span class="method-name">bitstring <i>&lt;&lt; bitcount</i> => <i>BitString</i><br />
463
+ bitstring.&lt;&lt;<i>(bitcount)</i> => <i>BitString</i><br />
464
+ </span>
465
+ </a>
466
+ </div>
467
+
468
+ <div class="method-description">
469
+ <h3>Description</h3>
470
+ <p>
471
+ Perform a left-shift on the bitstring, returning a <a
472
+ href="BitString.html#M000001">new</a> <em><a
473
+ href="BitString.html">BitString</a></em> containing the shifted value. If
474
+ the bitstring is bounded, bits shifted off the high end are lost.
475
+ </p>
476
+ <h3>Arguments</h3>
477
+ <dl>
478
+ <dt><em>bitcount</em></dt><dd><em>Integer</em>. Number of positions by which to left-shift.
479
+
480
+ </dd>
481
+ </dl>
482
+ <h3>Examples</h3>
483
+ <pre>
484
+ bs = BitString.new('1100111')
485
+ nbs = bs &lt;&lt; 2
486
+ nbs.to_s
487
+ =&gt; &quot;110011100&quot;
488
+
489
+ bs = BitString.new('00010001', 8)
490
+ nbs = bs &lt;&lt; 2
491
+ nbs.to_s
492
+ =&gt; &quot;01000100&quot;
493
+ nbs = bs &lt;&lt; 4
494
+ nbs.to_s
495
+ =&gt; &quot;00010000&quot; # High bits shifted off and lost
496
+ </pre>
497
+ <h3>Exceptions</h3>
498
+ <p>
499
+ <em>None</em>.
500
+ </p>
501
+ <p><a class="source-toggle" href="#"
502
+ onclick="toggleCode('M000027-source');return false;">[Source]</a></p>
503
+ <div class="method-source-code" id="M000027-source">
504
+ <pre>
505
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 129</span>
506
+ 129: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">&lt;&lt;</span>(<span class="ruby-identifier">bits</span>)
507
+ 130: <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@value</span> <span class="ruby-operator">*</span> (<span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">bits</span>.<span class="ruby-identifier">to_i</span>)
508
+ 131: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bounded?</span>)
509
+ 132: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">mask</span>, <span class="ruby-ivar">@length</span>)
510
+ 133: <span class="ruby-keyword kw">else</span>
511
+ 134: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>)
512
+ 135: <span class="ruby-keyword kw">end</span>
513
+ 136: <span class="ruby-keyword kw">end</span>
514
+ </pre>
515
+ </div>
516
+ </div>
517
+ </div>
518
+
519
+ <div id="method-M000034" class="method-detail">
520
+ <a name="M000034"></a>
521
+
522
+ <div class="method-heading">
523
+ <a href="#M000034" class="method-signature">
524
+ <span class="method-name">bitstring == <i>compstring</i> => <i>Boolean</i><br />
525
+ bitstring.==<i>(compstring)</i> => <i>Boolean</i><br />
526
+ </span>
527
+ </a>
528
+ </div>
529
+
530
+ <div class="method-description">
531
+ <h3>Description</h3>
532
+ <p>
533
+ Perform an equality check against another bitstring or representation of
534
+ one. The value and the boundedness must both match to be considered equal.
535
+ </p>
536
+ <h3>Arguments</h3>
537
+ <dl>
538
+ <dt><em>compstring</em></dt><dd><em>Array</em>, <em><a href="BitString.html">BitString</a></em>,
539
+ <em>Integer</em>, or <em>String</em>. Bitstring (or something representing
540
+ one) against which to compare.
541
+
542
+ </dd>
543
+ </dl>
544
+ <h3>Examples</h3>
545
+ <pre>
546
+ bs1 = BitString.new('111111111')
547
+ bs2 = BitString.new('111111111', 9)
548
+ bs1 == bs2
549
+ =&gt; false # Boundedness doesn't match
550
+
551
+ bs1 = BitString.new('111111111')
552
+ bs2 = BitString.new('111111111')
553
+ bs1 == bs2
554
+ =&gt; true # Values and boundedness match
555
+
556
+ bs1 = BitString.new('111111111')
557
+ bs1 == '111111111'
558
+ =&gt; true # When converted to an unbounded bitstring, it matches
559
+
560
+ bs1 = BitString.new('111111111')
561
+ bs1 == '0000000111111111'
562
+ =&gt; true # When converted to an unbounded bitstring, it matches
563
+
564
+ bs1 = BitString.new('111111111')
565
+ bs1 == 511
566
+ =&gt; true # When converted to an unbounded bitstring, it matches
567
+
568
+ bs1 = BitString.new('111111111', 9)
569
+ bs1 == '1000111111111'
570
+ =&gt; false # When converted to a bounded bitstring (because bs1 is),
571
+ # lengths and values don't match
572
+ </pre>
573
+ <h3>Exceptions</h3>
574
+ <p>
575
+ <em>None</em>.
576
+ </p>
577
+ <p><a class="source-toggle" href="#"
578
+ onclick="toggleCode('M000034-source');return false;">[Source]</a></p>
579
+ <div class="method-source-code" id="M000034-source">
580
+ <pre>
581
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 487</span>
582
+ 487: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">==</span>(<span class="ruby-identifier">value</span>)
583
+ 488: <span class="ruby-comment cmt">#</span>
584
+ 489: <span class="ruby-comment cmt"># Bitstring/bitstring comparison</span>
585
+ 490: <span class="ruby-comment cmt">#</span>
586
+ 491: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">value</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>)
587
+ 492: <span class="ruby-identifier">value</span> = <span class="ruby-identifier">_arg2int</span>(<span class="ruby-identifier">value</span>)
588
+ 493: <span class="ruby-keyword kw">if</span> (<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">bounded?</span>)
589
+ 494: <span class="ruby-identifier">bits</span> = [<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">length</span>, <span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_s</span>(<span class="ruby-value">2</span>).<span class="ruby-identifier">length</span>].<span class="ruby-identifier">max</span>
590
+ 495: <span class="ruby-identifier">value</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">bits</span>)
591
+ 496: <span class="ruby-keyword kw">else</span>
592
+ 497: <span class="ruby-identifier">value</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>)
593
+ 498: <span class="ruby-keyword kw">end</span>
594
+ 499: <span class="ruby-keyword kw">end</span>
595
+ 500: ((<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">class</span>) <span class="ruby-operator">&amp;&amp;</span>
596
+ 501: (<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">bounded?</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">bounded?</span>) <span class="ruby-operator">&amp;&amp;</span>
597
+ 502: (<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">length</span>) <span class="ruby-operator">&amp;&amp;</span>
598
+ 503: (<span class="ruby-ivar">@value</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_i</span>))
599
+ 504: <span class="ruby-keyword kw">end</span>
600
+ </pre>
601
+ </div>
602
+ </div>
603
+ </div>
604
+
605
+ <div id="method-M000028" class="method-detail">
606
+ <a name="M000028"></a>
607
+
608
+ <div class="method-heading">
609
+ <a href="#M000028" class="method-signature">
610
+ <span class="method-name">bitstring &gt;&gt; <i>bitcount</i> => <i>BitString</i><br />
611
+ bitstring.&gt;&gt;<i>(bitcount)</i> => <i>BitString</i><br />
612
+ </span>
613
+ </a>
614
+ </div>
615
+
616
+ <div class="method-description">
617
+ <h3>Description</h3>
618
+ <p>
619
+ Right-shift the bitstring (toward the low end) by the specified number of
620
+ bits. Bits shifted off the end are lost; <a
621
+ href="BitString.html#M000001">new</a> bits shifted in at the high end are
622
+ 0.
623
+ </p>
624
+ <h3>Arguments</h3>
625
+ <dl>
626
+ <dt><em>bitcount</em></dt><dd><em>Integer</em>. Number of positions to right-shift by.
627
+
628
+ </dd>
629
+ </dl>
630
+ <h3>Examples</h3>
631
+ <pre>
632
+ bs = BitString.new('1100111')
633
+ nbs = bs &gt;&gt; 2
634
+ nbs.to_s
635
+ =&gt; &quot;11001&quot;
636
+
637
+ bs = BitString.new('00010001', 8)
638
+ nbs = bs &gt;&gt; 2
639
+ nbs.to_s
640
+ =&gt; &quot;00000100&quot;
641
+ nbs = bs &gt;&gt; 4
642
+ nbs.to_s
643
+ =&gt; &quot;00000001&quot;
644
+ </pre>
645
+ <h3>Exceptions</h3>
646
+ <p>
647
+ <em>None</em>.
648
+ </p>
649
+ <p><a class="source-toggle" href="#"
650
+ onclick="toggleCode('M000028-source');return false;">[Source]</a></p>
651
+ <div class="method-source-code" id="M000028-source">
652
+ <pre>
653
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 169</span>
654
+ 169: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">&gt;&gt;</span>(<span class="ruby-identifier">bits</span>)
655
+ 170: <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@value</span> <span class="ruby-operator">/</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">bits</span>.<span class="ruby-identifier">to_i</span>
656
+ 171: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bounded?</span>)
657
+ 172: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>, <span class="ruby-ivar">@length</span>)
658
+ 173: <span class="ruby-keyword kw">else</span>
659
+ 174: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>)
660
+ 175: <span class="ruby-keyword kw">end</span>
661
+ 176: <span class="ruby-keyword kw">end</span>
662
+ </pre>
663
+ </div>
664
+ </div>
665
+ </div>
666
+
667
+ <div id="method-M000029" class="method-detail">
668
+ <a name="M000029"></a>
669
+
670
+ <div class="method-heading">
671
+ <a href="#M000029" class="method-signature">
672
+ <span class="method-name">bitstring<i>[index]</i> => <i>Integer</i><br />
673
+ bitstring<i>[start, length]</i> => <i>BitString</i><br />
674
+ bitstring<i>[range]</i> => <i>BitString</i><br />
675
+ </span>
676
+ </a>
677
+ </div>
678
+
679
+ <div class="method-description">
680
+ <h3>Description</h3>
681
+ <p>
682
+ Extract a particular substring from the bitstring. If we&#8216;re given a
683
+ single index, we return an integer; if a range, we return a <a
684
+ href="BitString.html#M000001">new</a> <a
685
+ href="BitString.html">BitString</a>.
686
+ </p>
687
+ <h3>Arguments</h3>
688
+ <dl>
689
+ <dt><em>index</em></dt><dd><em>Integer</em>. Single bit position.
690
+
691
+ </dd>
692
+ <dt><em>start</em></dt><dd><em>Integer</em>. Start position of subset.
693
+
694
+ </dd>
695
+ <dt><em><a href="BitString.html#M000009">length</a></em></dt><dd><em>Integer</em>. Length of subset.
696
+
697
+ </dd>
698
+ <dt><em>range</em></dt><dd><em>Range</em>. Subset specified as a range.
699
+
700
+ </dd>
701
+ </dl>
702
+ <h3>Examples</h3>
703
+ <pre>
704
+ bs = BitString.new('110010110')
705
+ bs[0]
706
+ =&gt; 0
707
+ bs[1]
708
+ =&gt; 1
709
+ bs[0..1].to_s # Multi-bit indexing returns a bitsring
710
+ =&gt; &quot;10&quot;
711
+ bs[0,9].to_s
712
+ =&gt; &quot;110010110&quot;
713
+ bs[100..102].to_s # Can index 'way past the end for unbounded bitstrings
714
+ =&gt; &quot;000&quot;
715
+
716
+ bs = BitString.new('110010110', 9)
717
+ bs[100..102].to_s #
718
+ =&gt; exception: &quot;IndexError: index out of range&quot;
719
+ </pre>
720
+ <h3>Exceptions</h3>
721
+ <dl>
722
+ <dt><tt>ArgumentError</tt></dt><dd>If <a href="BitString.html#M000009">length</a> specified with a range.
723
+
724
+ </dd>
725
+ <dt><tt>IndexError</tt></dt><dd>If bitstring is bounded and the substring is illegal.
726
+
727
+ </dd>
728
+ </dl>
729
+ <p><a class="source-toggle" href="#"
730
+ onclick="toggleCode('M000029-source');return false;">[Source]</a></p>
731
+ <div class="method-source-code" id="M000029-source">
732
+ <pre>
733
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 217</span>
734
+ 217: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">[]</span>(<span class="ruby-identifier">pos_p</span>, <span class="ruby-identifier">length_p</span>=<span class="ruby-keyword kw">nil</span>)
735
+ 218: <span class="ruby-comment cmt">#</span>
736
+ 219: <span class="ruby-comment cmt"># Turn a position/length into a range.</span>
737
+ 220: <span class="ruby-comment cmt">#</span>
738
+ 221: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">pos_p</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">length_p</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Integer</span>))
739
+ 222: <span class="ruby-identifier">pos_p</span> = <span class="ruby-constant">Range</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">pos_p</span>, <span class="ruby-identifier">pos_p</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">length_p</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>)
740
+ 223: <span class="ruby-identifier">length_p</span> = <span class="ruby-keyword kw">nil</span>
741
+ 224: <span class="ruby-keyword kw">end</span>
742
+ 225: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">pos_p</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Range</span>))
743
+ 226: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">length_p</span>.<span class="ruby-identifier">nil?</span>)
744
+ 227: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'length not allowed with range'</span>)
745
+ 228: <span class="ruby-keyword kw">end</span>
746
+ 229: <span class="ruby-identifier">pos_a</span> = <span class="ruby-identifier">pos_p</span>.<span class="ruby-identifier">to_a</span>
747
+ 230: <span class="ruby-identifier">pos_a</span>.<span class="ruby-identifier">reverse!</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">pos_a</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">pos_a</span>.<span class="ruby-identifier">last</span>)
748
+ 231: <span class="ruby-identifier">r</span> = <span class="ruby-identifier">pos_a</span>.<span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">pos</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">self</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">to_s</span> }
749
+ 232: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">r</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">''</span>), <span class="ruby-identifier">r</span>.<span class="ruby-identifier">length</span>)
750
+ 233: <span class="ruby-keyword kw">end</span>
751
+ 234: <span class="ruby-identifier">pos</span> = <span class="ruby-identifier">pos_p</span>.<span class="ruby-identifier">to_i</span>
752
+ 235: <span class="ruby-comment cmt">#</span>
753
+ 236: <span class="ruby-comment cmt"># Blow an error if we were given an index out of range.</span>
754
+ 237: <span class="ruby-comment cmt">#</span>
755
+ 238: <span class="ruby-keyword kw">unless</span> ((<span class="ruby-identifier">pos</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">0</span>) <span class="ruby-operator">&amp;&amp;</span>
756
+ 239: ((<span class="ruby-operator">!</span> <span class="ruby-identifier">bounded?</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">pos</span>.<span class="ruby-identifier">between?</span>(<span class="ruby-value">0</span>, <span class="ruby-ivar">@length</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>)))
757
+ 240: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">OuttasightIndex</span>, <span class="ruby-identifier">pos</span>)
758
+ 241: <span class="ruby-keyword kw">end</span>
759
+ 242: (<span class="ruby-ivar">@value</span> <span class="ruby-operator">&amp;</span> (<span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">pos</span>)) <span class="ruby-operator">/</span> (<span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">pos</span>)
760
+ 243: <span class="ruby-keyword kw">end</span>
761
+ </pre>
762
+ </div>
763
+ </div>
764
+ </div>
765
+
766
+ <div id="method-M000030" class="method-detail">
767
+ <a name="M000030"></a>
768
+
769
+ <div class="method-heading">
770
+ <a href="#M000030" class="method-signature">
771
+ <span class="method-name">bitstring[<i>pos</i>] = <i>value</i> => <i>value</i><br />
772
+ bitstring[<i>start, length</i>] = <i>value</i> => <i>value</i><br />
773
+ bitstring[<i>range</i>] = <i>value</i> => <i>value</i><br />
774
+ </span>
775
+ </a>
776
+ </div>
777
+
778
+ <div class="method-description">
779
+ <h3>Description</h3>
780
+ <p>
781
+ Set a bit or range of bits with a single operation.
782
+ </p>
783
+ <h3>Arguments</h3>
784
+ <dl>
785
+ <dt><em>value</em></dt><dd><em>Array</em>, <em><a href="BitString.html">BitString</a></em>,
786
+ <em>Integer</em>, or <em>String</em>. Value (treated as a stream of bits)
787
+ used to set the bitstring.
788
+
789
+ </dd>
790
+ <dt><em>pos</em></dt><dd><em>Integer</em>. Single bit position to alter.
791
+
792
+ </dd>
793
+ <dt><em>start</em></dt><dd><em>Integer</em>. First bit position in substring.
794
+
795
+ </dd>
796
+ <dt><em><a href="BitString.html#M000009">length</a></em></dt><dd><em>Integer</em>. Number of bits in substring.
797
+
798
+ </dd>
799
+ <dt><em>range</em></dt><dd><em>Range</em>. Range of bits (<em>e.g.</em>, 5..10) to affect.
800
+
801
+ </dd>
802
+ </dl>
803
+ <h3>Examples</h3>
804
+ <pre>
805
+ bs = BitString.new('110010110')
806
+ bs[0] = 1
807
+ =&gt; 1
808
+ bs.to_s
809
+ =&gt; &quot;110010111&quot;
810
+ bs[1] = 0
811
+ =&gt; 0
812
+ bs.to_s
813
+ =&gt; &quot;110010101&quot;
814
+ bs[0..3] = 14 # Multi-bit indexing set a bitsring
815
+ =&gt; &quot;14&quot;
816
+ bs.to_s
817
+ =&gt; &quot;110011110&quot;
818
+ bs[12..14] = 5
819
+ =&gt; 5
820
+ bs.to_s
821
+ =&gt; &quot;101000110011110&quot; # Can index past the end for unbounded bitstrings
822
+
823
+ bs = BitString.new('110010110', 9)
824
+ bs[1,3] = 4095 # 111111111111, but gets truncated to low 3 bits
825
+ bs.to_s
826
+ =&gt; &quot;110011110&quot;
827
+ </pre>
828
+ <h3>Exceptions</h3>
829
+ <dl>
830
+ <dt><tt>ArgumentError</tt></dt><dd>Both range and <a href="BitString.html#M000009">length</a> specified, or
831
+ value cannot be interpreted as an integer.
832
+
833
+ </dd>
834
+ <dt><tt>IndexError</tt></dt><dd>Non-integer or negative index, or beyond the end of the bitstring.
835
+
836
+ </dd>
837
+ </dl>
838
+ <p><a class="source-toggle" href="#"
839
+ onclick="toggleCode('M000030-source');return false;">[Source]</a></p>
840
+ <div class="method-source-code" id="M000030-source">
841
+ <pre>
842
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 290</span>
843
+ 290: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">[]=</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args_p</span>)
844
+ 291: <span class="ruby-identifier">low</span> = <span class="ruby-identifier">high</span> = <span class="ruby-identifier">width</span> = <span class="ruby-keyword kw">nil</span>
845
+ 292: <span class="ruby-identifier">args</span> = <span class="ruby-identifier">args_p</span>
846
+ 293: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">args</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">2</span>)
847
+ 294: <span class="ruby-comment cmt">#</span>
848
+ 295: <span class="ruby-comment cmt"># [pos]= or [range]=</span>
849
+ 296: <span class="ruby-comment cmt">#</span>
850
+ 297: <span class="ruby-keyword kw">if</span> ((<span class="ruby-identifier">r</span> = <span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>]).<span class="ruby-identifier">class</span>.<span class="ruby-identifier">eql?</span>(<span class="ruby-constant">Range</span>))
851
+ 298: <span class="ruby-comment cmt">#</span>
852
+ 299: <span class="ruby-comment cmt"># Convert into a [start,length] format to reuse that stream.</span>
853
+ 300: <span class="ruby-comment cmt">#</span>
854
+ 301: (<span class="ruby-identifier">start</span>, <span class="ruby-identifier">stop</span>) = [<span class="ruby-identifier">r</span>.<span class="ruby-identifier">first</span>, <span class="ruby-identifier">r</span>.<span class="ruby-identifier">last</span>].<span class="ruby-identifier">sort</span>
855
+ 302: <span class="ruby-identifier">width</span> = <span class="ruby-identifier">stop</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">start</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
856
+ 303: <span class="ruby-identifier">args</span> = [<span class="ruby-identifier">start</span>, <span class="ruby-identifier">width</span>, <span class="ruby-identifier">args</span>[<span class="ruby-value">1</span>]]
857
+ 304: <span class="ruby-keyword kw">else</span>
858
+ 305: <span class="ruby-identifier">args</span> = [<span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>], <span class="ruby-value">1</span>, <span class="ruby-identifier">args</span>[<span class="ruby-value">1</span>]]
859
+ 306: <span class="ruby-keyword kw">end</span>
860
+ 307: <span class="ruby-keyword kw">elsif</span> (<span class="ruby-identifier">args_p</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-value">3</span>)
861
+ 308: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">WrongNargs</span>, <span class="ruby-identifier">args_p</span>.<span class="ruby-identifier">length</span>, <span class="ruby-value">3</span>)
862
+ 309: <span class="ruby-keyword kw">end</span>
863
+ 310: <span class="ruby-comment cmt">#</span>
864
+ 311: <span class="ruby-comment cmt"># Special cases of args have now been normalised to [start,length,value].</span>
865
+ 312: <span class="ruby-comment cmt"># Make sure the values are acceptable.</span>
866
+ 313: <span class="ruby-comment cmt">#</span>
867
+ 314: (<span class="ruby-identifier">start</span>, <span class="ruby-identifier">nBits</span>, <span class="ruby-identifier">value</span>) = <span class="ruby-identifier">args</span>
868
+ 315: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">BogoIndex</span>, <span class="ruby-identifier">start</span>) <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">start</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_i</span>))
869
+ 316: <span class="ruby-identifier">start</span> = <span class="ruby-identifier">start</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">start</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Integer</span>))
870
+ 317: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">BogoIndex</span>, <span class="ruby-identifier">nBits</span>) <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">nBits</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_i</span>))
871
+ 318: <span class="ruby-identifier">nBits</span> = <span class="ruby-identifier">length</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">nBits</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Integer</span>))
872
+ 319: <span class="ruby-identifier">highpos</span> = <span class="ruby-identifier">start</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">nBits</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
873
+ 320: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">OuttasightIndex</span>, <span class="ruby-identifier">highpos</span>) <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bounded?</span> <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">highpos</span> <span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@length</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>))
874
+ 321: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">BitsRInts</span>, <span class="ruby-identifier">value</span>) <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">value</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_i</span>))
875
+ 322: <span class="ruby-identifier">value</span> = <span class="ruby-identifier">_arg2int</span>(<span class="ruby-identifier">value</span>)
876
+ 323: <span class="ruby-comment cmt">#</span>
877
+ 324: <span class="ruby-comment cmt"># All the checking is done, let's do this thing.</span>
878
+ 325: <span class="ruby-comment cmt">#</span>
879
+ 326: <span class="ruby-identifier">vMask</span> = <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">nBits</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
880
+ 327: <span class="ruby-identifier">fvalue</span> = (<span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;=</span> <span class="ruby-identifier">vMask</span>)
881
+ 328: <span class="ruby-identifier">vMask</span> <span class="ruby-operator">*=</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">start</span>
882
+ 329: <span class="ruby-identifier">value</span> <span class="ruby-operator">*=</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">start</span>
883
+ 330:
884
+ 331: <span class="ruby-identifier">highpos</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">length</span>
885
+ 332: <span class="ruby-identifier">bValue</span> = <span class="ruby-ivar">@value</span> <span class="ruby-operator">&amp;</span> ((<span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">highpos</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>) <span class="ruby-operator">&amp;</span> <span class="ruby-operator">~</span><span class="ruby-identifier">vMask</span>)
886
+ 333: <span class="ruby-ivar">@value</span> = <span class="ruby-identifier">bValue</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">value</span>
887
+ 334: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">fvalue</span>
888
+ 335: <span class="ruby-keyword kw">end</span>
889
+ </pre>
890
+ </div>
891
+ </div>
892
+ </div>
893
+
894
+ <div id="method-M000031" class="method-detail">
895
+ <a name="M000031"></a>
896
+
897
+ <div class="method-heading">
898
+ <a href="#M000031" class="method-signature">
899
+ <span class="method-name">bitstring <i>^ value</i> => <i>BitString</i><br />
900
+ bitstring.^<i>(value)</i> => <i>BitString</i><br />
901
+ </span>
902
+ </a>
903
+ </div>
904
+
905
+ <div class="method-description">
906
+ <h3>Description</h3>
907
+ <p>
908
+ Perform a bitwise exclusive OR (XOR) and return a copy of the result.
909
+ </p>
910
+ <h3>Arguments</h3>
911
+ <dl>
912
+ <dt><em>value</em></dt><dd><em>Array</em>, <em><a href="BitString.html">BitString</a></em>,
913
+ <em>Integer</em>, or <em>String</em>. Value treated as a bitstream and
914
+ exclusively ORed with the bitstring.
915
+
916
+ </dd>
917
+ </dl>
918
+ <h3>Examples</h3>
919
+ <pre>
920
+ bs = BitString.new('110010110', 9)
921
+ nbs = bs ^ '001101001'
922
+ nbs.to_s
923
+ =&gt; &quot;111111111&quot;
924
+ nbs = bs ^ bs.mask # Equivalent to 'nbs = ~ bs'
925
+ nbs.to_s
926
+ =&gt; &quot;001101001&quot;
927
+ </pre>
928
+ <h3>Exceptions</h3>
929
+ <p>
930
+ <em>None</em>.
931
+ </p>
932
+ <p><a class="source-toggle" href="#"
933
+ onclick="toggleCode('M000031-source');return false;">[Source]</a></p>
934
+ <div class="method-source-code" id="M000031-source">
935
+ <pre>
936
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 362</span>
937
+ 362: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">^</span>(<span class="ruby-identifier">value</span>)
938
+ 363: <span class="ruby-identifier">value</span> = <span class="ruby-identifier">_arg2int</span>(<span class="ruby-identifier">value</span>)
939
+ 364: <span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;=</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">mask</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bounded?</span>)
940
+ 365: <span class="ruby-identifier">bs</span> = <span class="ruby-identifier">dup</span>
941
+ 366: <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">from_i</span>(<span class="ruby-identifier">value</span> <span class="ruby-operator">^</span> <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">to_i</span>)
942
+ 367: <span class="ruby-identifier">bs</span>
943
+ 368: <span class="ruby-keyword kw">end</span>
944
+ </pre>
945
+ </div>
946
+ </div>
947
+ </div>
948
+
949
+ <div id="method-M000002" class="method-detail">
950
+ <a name="M000002"></a>
951
+
952
+ <div class="method-heading">
953
+ <a href="#M000002" class="method-signature">
954
+ <span class="method-name">bitstring.bounded?()<br />
955
+ </span>
956
+ </a>
957
+ </div>
958
+
959
+ <div class="method-description">
960
+ <h3>Description</h3>
961
+ <p>
962
+ Return a Boolean indicating whether the bitstring has a fixed <a
963
+ href="BitString.html#M000009">length</a> (is bounded) or not.
964
+ </p>
965
+ <h3>Arguments</h3>
966
+ <p>
967
+ <em>None</em>.
968
+ </p>
969
+ <h3>Examples</h3>
970
+ <pre>
971
+ bs = BitString.new(3095)
972
+ bs.bounded?
973
+ =&gt; false
974
+
975
+ bs = BitString.new(3095, 12)
976
+ bs.bounded?
977
+ =&gt; true
978
+ </pre>
979
+ <h3>Exceptions</h3>
980
+ <p>
981
+ <em>None</em>.
982
+ </p>
983
+ <p><a class="source-toggle" href="#"
984
+ onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
985
+ <div class="method-source-code" id="M000002-source">
986
+ <pre>
987
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 565</span>
988
+ 565: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">bounded?</span>()
989
+ 566: <span class="ruby-ivar">@bounded</span>
990
+ 567: <span class="ruby-keyword kw">end</span>
991
+ </pre>
992
+ </div>
993
+ </div>
994
+ </div>
995
+
996
+ <div id="method-M000003" class="method-detail">
997
+ <a name="M000003"></a>
998
+
999
+ <div class="method-heading">
1000
+ <a href="#M000003" class="method-signature">
1001
+ <span class="method-name">bitstring.clear<i>()</i> => <i>BitString</i><br />
1002
+ </span>
1003
+ </a>
1004
+ </div>
1005
+
1006
+ <div class="method-description">
1007
+ <h3>Description</h3>
1008
+ <p>
1009
+ Return a duplicate of the current bitstring &#8212; with all bits cleared.
1010
+ </p>
1011
+ <h3>Arguments</h3>
1012
+ <p>
1013
+ <em>None</em>.
1014
+ </p>
1015
+ <h3>Examples</h3>
1016
+ <pre>
1017
+ bs = BitString.new(3095)
1018
+ nbs = bs.clear
1019
+ bs.to_s
1020
+ =&gt; &quot;110000010111&quot;
1021
+ nbs.to_s
1022
+ =&gt; &quot;0&quot;
1023
+ </pre>
1024
+ <h3>Exceptions</h3>
1025
+ <p>
1026
+ <em>None</em>.
1027
+ </p>
1028
+ <p><a class="source-toggle" href="#"
1029
+ onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
1030
+ <div class="method-source-code" id="M000003-source">
1031
+ <pre>
1032
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 591</span>
1033
+ 591: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">clear</span>()
1034
+ 592: <span class="ruby-identifier">bs</span> = <span class="ruby-identifier">dup</span>
1035
+ 593: <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">from_i</span>(<span class="ruby-value">0</span>)
1036
+ 594: <span class="ruby-identifier">bs</span>
1037
+ 595: <span class="ruby-keyword kw">end</span>
1038
+ </pre>
1039
+ </div>
1040
+ </div>
1041
+ </div>
1042
+
1043
+ <div id="method-M000004" class="method-detail">
1044
+ <a name="M000004"></a>
1045
+
1046
+ <div class="method-heading">
1047
+ <a href="#M000004" class="method-signature">
1048
+ <span class="method-name">bitstring.clear!<i>()</i> => <i>BitString</i><br />
1049
+ </span>
1050
+ </a>
1051
+ </div>
1052
+
1053
+ <div class="method-description">
1054
+ <h3>Description</h3>
1055
+ <p>
1056
+ Clear all the bits in the bitstring.
1057
+ </p>
1058
+ <h3>Arguments</h3>
1059
+ <p>
1060
+ <em>None</em>.
1061
+ </p>
1062
+ <h3>Examples</h3>
1063
+ <pre>
1064
+ bs = BitString.new(3095, 12)
1065
+ bs.to_s
1066
+ =&gt; &quot;110000010111&quot;
1067
+ bs.clear!
1068
+ bs.to_s
1069
+ =&gt; &quot;000000000000&quot;
1070
+ </pre>
1071
+ <h3>Exceptions</h3>
1072
+ <p>
1073
+ <em>None</em>.
1074
+ </p>
1075
+ <p><a class="source-toggle" href="#"
1076
+ onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
1077
+ <div class="method-source-code" id="M000004-source">
1078
+ <pre>
1079
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 619</span>
1080
+ 619: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">clear!</span>()
1081
+ 620: <span class="ruby-ivar">@value</span> = <span class="ruby-value">0</span>
1082
+ 621: <span class="ruby-keyword kw">self</span>
1083
+ 622: <span class="ruby-keyword kw">end</span>
1084
+ </pre>
1085
+ </div>
1086
+ </div>
1087
+ </div>
1088
+
1089
+ <div id="method-M000005" class="method-detail">
1090
+ <a name="M000005"></a>
1091
+
1092
+ <div class="method-heading">
1093
+ <a href="#M000005" class="method-signature">
1094
+ <span class="method-name">bitstring.<i>each</i> { |<i>bitval</i>| <i>block</i> } => <i>BitString</i><br />
1095
+ </span>
1096
+ </a>
1097
+ </div>
1098
+
1099
+ <div class="method-description">
1100
+ <h3>Description</h3>
1101
+ <p>
1102
+ Execute a block for <a href="BitString.html#M000005">each</a> bit in the
1103
+ bitstring.
1104
+ </p>
1105
+ <h3>Arguments</h3>
1106
+ <dl>
1107
+ <dt><em>block</em></dt><dd><em>Proc</em>. Block to be called for <a
1108
+ href="BitString.html#M000005">each</a> bit in the string. The block is
1109
+ passed the bit value.
1110
+
1111
+ </dd>
1112
+ </dl>
1113
+ <h3>Examples</h3>
1114
+ <pre>
1115
+ bs = BitString.new('100101')
1116
+ bs.each { |bitval| puts bitval }
1117
+ 1
1118
+ 0
1119
+ 1
1120
+ 0
1121
+ 0
1122
+ 1
1123
+ </pre>
1124
+ <h3>Exceptions</h3>
1125
+ <p>
1126
+ <em>None</em>.
1127
+ </p>
1128
+ <p><a class="source-toggle" href="#"
1129
+ onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
1130
+ <div class="method-source-code" id="M000005-source">
1131
+ <pre>
1132
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 648</span>
1133
+ 648: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
1134
+ 649: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">length</span>.<span class="ruby-identifier">times</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">bit</span><span class="ruby-operator">|</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-keyword kw">self</span>[<span class="ruby-identifier">bit</span>]) }
1135
+ 650: <span class="ruby-keyword kw">self</span>
1136
+ 651: <span class="ruby-keyword kw">end</span>
1137
+ </pre>
1138
+ </div>
1139
+ </div>
1140
+ </div>
1141
+
1142
+ <div id="method-M000006" class="method-detail">
1143
+ <a name="M000006"></a>
1144
+
1145
+ <div class="method-heading">
1146
+ <a href="#M000006" class="method-signature">
1147
+ <span class="method-name">bitstring.from_i<i>(newval)</i> => <i>BitString</i><br />
1148
+ </span>
1149
+ </a>
1150
+ </div>
1151
+
1152
+ <div class="method-description">
1153
+ <h3>Description</h3>
1154
+ <p>
1155
+ Treat the bitstring as an Integer and store its entire value at once.
1156
+ </p>
1157
+ <h3>Arguments</h3>
1158
+ <dl>
1159
+ <dt><em>newval</em></dt><dd><em>Integer</em>. Value from which bits will be copied to the bitstring.
1160
+
1161
+ </dd>
1162
+ </dl>
1163
+ <h3>Examples</h3>
1164
+ <pre>
1165
+ bs = BitString.new(0, 12)
1166
+ bs.to_s
1167
+ =&gt; &quot;000000000000&quot;
1168
+ bs.from_i(3095)
1169
+ bs.to_s
1170
+ =&gt; &quot;110000010111&quot;
1171
+ </pre>
1172
+ <h3>Exceptions</h3>
1173
+ <p>
1174
+ <em>None</em>.
1175
+ </p>
1176
+ <p><a class="source-toggle" href="#"
1177
+ onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
1178
+ <div class="method-source-code" id="M000006-source">
1179
+ <pre>
1180
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 676</span>
1181
+ 676: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">from_i</span>(<span class="ruby-identifier">newval</span>)
1182
+ 677: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">newval</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_i</span>))
1183
+ 678: <span class="ruby-identifier">what</span> = <span class="ruby-identifier">newval</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_s</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">newval</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">newval</span>.<span class="ruby-identifier">inspect</span>
1184
+ 679: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">UnInterable</span>, <span class="ruby-identifier">newval</span>)
1185
+ 680: <span class="ruby-keyword kw">end</span>
1186
+ 681: <span class="ruby-identifier">newval</span> = <span class="ruby-identifier">newval</span>.<span class="ruby-identifier">to_i</span>
1187
+ 682: <span class="ruby-identifier">newval</span> <span class="ruby-operator">&amp;=</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-ivar">@length</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bounded?</span>)
1188
+ 683: <span class="ruby-ivar">@value</span> = <span class="ruby-identifier">newval</span>
1189
+ 684: <span class="ruby-keyword kw">self</span>
1190
+ 685: <span class="ruby-keyword kw">end</span>
1191
+ </pre>
1192
+ </div>
1193
+ </div>
1194
+ </div>
1195
+
1196
+ <div id="method-M000007" class="method-detail">
1197
+ <a name="M000007"></a>
1198
+
1199
+ <div class="method-heading">
1200
+ <a href="#M000007" class="method-signature">
1201
+ <span class="method-name">bitstring.grow<i>(bits, [defval], [direction])</i> => <i>BitString</i><br />
1202
+ </span>
1203
+ </a>
1204
+ </div>
1205
+
1206
+ <div class="method-description">
1207
+ <h3>Description</h3>
1208
+ <p>
1209
+ Return a <a href="BitString.html#M000001">new</a> bitstring based on the
1210
+ current one, grown (made longer) in one direction (toward the least
1211
+ significant bits) or the other. Growing an unbounded string toward the high
1212
+ end is a no-op.
1213
+ </p>
1214
+ <p>
1215
+ Bits added are set to <em>defval</em> (default 0).
1216
+ </p>
1217
+ <h3>Arguments</h3>
1218
+ <dl>
1219
+ <dt><em>bits</em></dt><dd><em>Integer</em>. Number of bits to add.
1220
+
1221
+ </dd>
1222
+ <dt><em>defval</em></dt><dd><em>Integer</em>. Value to which added bits should be set.
1223
+
1224
+ </dd>
1225
+ <dt><em>direction</em></dt><dd><em>Constant</em>. Either <tt>BitString::HIGH_END</tt> (the default) or
1226
+ <tt>BitString::LOW_END&lt;/t&gt;. Indicates whether bits are added at the
1227
+ least or most significant end. Growing with
1228
+ &lt;tt&gt;BitString::LOW_END</tt> results in the bitstring being shifted
1229
+ left.
1230
+
1231
+ </dd>
1232
+ </dl>
1233
+ <h3>Examples</h3>
1234
+ <pre>
1235
+ bs = BitString(5, 3)
1236
+ bs.to_s
1237
+ =&gt; &quot;101&quot;
1238
+ nbs = bs.grow(3)
1239
+ nbs.to_s
1240
+ =&gt; &quot;000101&quot;
1241
+ nbs = bs.grow(3, 1)
1242
+ nbs.to_s
1243
+ =&gt; &quot;111101&quot;
1244
+ nbs = bs.grow(3, 0, BitString::LOW_END)
1245
+ nbs.to_s
1246
+ =&gt; &quot;101000&quot;
1247
+ </pre>
1248
+ <h3>Exceptions</h3>
1249
+ <dl>
1250
+ <dt><tt>ArgumentError</tt></dt><dd><em>bits</em> isn&#8216;t an integer, <em>defval</em> can&#8216;t be
1251
+ reduced to a binary value, or <em>direction</em> isn&#8216;t one of the
1252
+ defined values.
1253
+
1254
+ </dd>
1255
+ <dt><tt>IndexError</tt></dt><dd><em>bits</em> is negative or meaningless.
1256
+
1257
+ </dd>
1258
+ <dt><tt>RuntimeError</tt></dt><dd>Can&#8216;t <a href="BitString.html#M000007">grow</a> an unbounded string
1259
+ at the high end.
1260
+
1261
+ </dd>
1262
+ </dl>
1263
+ <p><a class="source-toggle" href="#"
1264
+ onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
1265
+ <div class="method-source-code" id="M000007-source">
1266
+ <pre>
1267
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 723</span>
1268
+ 723: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">grow</span>(<span class="ruby-identifier">bits</span>=<span class="ruby-value">1</span>, <span class="ruby-identifier">defval</span>=<span class="ruby-value">0</span>, <span class="ruby-identifier">direction</span>=<span class="ruby-constant">HIGH_END</span>)
1269
+ 724: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bits</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Integer</span>))
1270
+ 725: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">BitsRInts</span>)
1271
+ 726: <span class="ruby-keyword kw">end</span>
1272
+ 727: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">defval</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_i</span>))
1273
+ 728: <span class="ruby-identifier">what</span> = <span class="ruby-identifier">defval</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_s</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">defval</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">defval</span>.<span class="ruby-identifier">inspect</span>
1274
+ 729: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">UnInterable</span>, <span class="ruby-identifier">defval</span>)
1275
+ 730: <span class="ruby-keyword kw">end</span>
1276
+ 731: <span class="ruby-keyword kw">unless</span> ([<span class="ruby-constant">HIGH_END</span>, <span class="ruby-constant">LOW_END</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">direction</span>))
1277
+ 732: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">NeedGPS</span>)
1278
+ 733: <span class="ruby-keyword kw">end</span>
1279
+ 734: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">0</span>)
1280
+ 735: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">NoDeficitBits</span>)
1281
+ 736: <span class="ruby-keyword kw">end</span>
1282
+ 737: <span class="ruby-keyword kw">unless</span> ((<span class="ruby-identifier">direction</span> <span class="ruby-operator">==</span> <span class="ruby-constant">LOW_END</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">bounded?</span>)
1283
+ 738: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">UnboundedNonsense</span>)
1284
+ 739: <span class="ruby-keyword kw">end</span>
1285
+ 740: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">dup</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>)
1286
+ 741:
1287
+ 742: <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@value</span>
1288
+ 743: <span class="ruby-identifier">vMask</span> = <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">bits</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
1289
+ 744: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">direction</span> <span class="ruby-operator">==</span> <span class="ruby-constant">HIGH_END</span>)
1290
+ 745: <span class="ruby-identifier">vMask</span> <span class="ruby-operator">*=</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-ivar">@length</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bounded?</span>)
1291
+ 746: <span class="ruby-keyword kw">elsif</span> (<span class="ruby-identifier">direction</span> <span class="ruby-operator">==</span> <span class="ruby-constant">LOW_END</span>)
1292
+ 747: <span class="ruby-identifier">value</span> <span class="ruby-operator">*=</span> (<span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">bits</span>)
1293
+ 748: <span class="ruby-keyword kw">end</span>
1294
+ 749: <span class="ruby-identifier">value</span> <span class="ruby-operator">|=</span> <span class="ruby-identifier">vMask</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">defval</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>)
1295
+ 750: <span class="ruby-identifier">bounded?</span> <span class="ruby-value">? </span><span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>, <span class="ruby-ivar">@length</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">bits</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>)
1296
+ 751: <span class="ruby-keyword kw">end</span>
1297
+ </pre>
1298
+ </div>
1299
+ </div>
1300
+ </div>
1301
+
1302
+ <div id="method-M000008" class="method-detail">
1303
+ <a name="M000008"></a>
1304
+
1305
+ <div class="method-heading">
1306
+ <a href="#M000008" class="method-signature">
1307
+ <span class="method-name">bitstring.grow!<i>(bits, [defval], [direction])</i> => <i>BitString</i><br />
1308
+ </span>
1309
+ </a>
1310
+ </div>
1311
+
1312
+ <div class="method-description">
1313
+ <h3>Description</h3>
1314
+ <p>
1315
+ As <a href="BitString.html#M000007">grow</a>, except that the current
1316
+ bitstring is changed rather than a <a href="BitString.html#M000001">new</a>
1317
+ one returned.
1318
+ </p>
1319
+ <p>
1320
+ Bits added are set to <em>defval</em> (default 0).
1321
+ </p>
1322
+ <h3>Arguments</h3>
1323
+ <dl>
1324
+ <dt><em>bits</em></dt><dd><em>Integer</em> Number of bits to add.
1325
+
1326
+ </dd>
1327
+ <dt><em>defval</em></dt><dd><em>Integer</em> Value to which added bits should be set.
1328
+
1329
+ </dd>
1330
+ <dt><em>direction</em></dt><dd><em>Constant</em>. Either <tt>BitString::HIGH_END</tt> (the default) or
1331
+ <tt>BitString::LOW_END</tt>. Indicates whether bits are added at the least
1332
+ or most significant end. Growing with <tt>BitString::LOW_END</tt> results
1333
+ in the bitstring being shifted left.
1334
+
1335
+ </dd>
1336
+ </dl>
1337
+ <h3>Examples</h3>
1338
+ <pre>
1339
+ bs = BitString.new(5, 3)
1340
+ bs.to_s
1341
+ =&gt; &quot;101&quot;
1342
+ bs.grow!(3)
1343
+ bs.to_s
1344
+ =&gt; &quot;000101&quot;
1345
+ bs.grow!(3, 1, BitString::LOW_END)
1346
+ bs.to_s
1347
+ =&gt; &quot;000101111&quot;
1348
+ </pre>
1349
+ <h3>Exceptions</h3>
1350
+ <dl>
1351
+ <dt><tt>ArgumentError</tt></dt><dd><em>bits</em> isn&#8216;t an integer, <em>defval</em> can&#8216;t be
1352
+ reduced to a binary value, or <em>direction</em> isn&#8216;t one of the
1353
+ defined values.
1354
+
1355
+ </dd>
1356
+ <dt><tt>IndexError</tt></dt><dd><em>bits</em> is negative or meaningless.
1357
+
1358
+ </dd>
1359
+ <dt><tt>RuntimeError</tt></dt><dd>Can&#8216;t <a href="BitString.html#M000007">grow</a> an unbounded string
1360
+ at the high end.
1361
+
1362
+ </dd>
1363
+ </dl>
1364
+ <p><a class="source-toggle" href="#"
1365
+ onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
1366
+ <div class="method-source-code" id="M000008-source">
1367
+ <pre>
1368
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 785</span>
1369
+ 785: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">grow!</span>(<span class="ruby-identifier">bits</span>=<span class="ruby-value">1</span>, <span class="ruby-identifier">defval</span>=<span class="ruby-value">0</span>, <span class="ruby-identifier">direction</span>=<span class="ruby-constant">HIGH_END</span>)
1370
+ 786: <span class="ruby-identifier">bs</span> = <span class="ruby-identifier">grow</span>(<span class="ruby-identifier">bits</span>, <span class="ruby-identifier">defval</span>, <span class="ruby-identifier">direction</span>)
1371
+ 787: <span class="ruby-ivar">@length</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">length</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bs</span>.<span class="ruby-identifier">bounded?</span>)
1372
+ 788: <span class="ruby-ivar">@value</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">to_i</span>
1373
+ 789: <span class="ruby-keyword kw">self</span>
1374
+ 790: <span class="ruby-keyword kw">end</span>
1375
+ </pre>
1376
+ </div>
1377
+ </div>
1378
+ </div>
1379
+
1380
+ <div id="method-M000009" class="method-detail">
1381
+ <a name="M000009"></a>
1382
+
1383
+ <div class="method-heading">
1384
+ <a href="#M000009" class="method-signature">
1385
+ <span class="method-name">bitstring.length()<br />
1386
+ </span>
1387
+ </a>
1388
+ </div>
1389
+
1390
+ <div class="method-description">
1391
+ <h3>Description</h3>
1392
+ <p>
1393
+ Return the <a href="BitString.html#M000009">length</a> of the bitstring. If
1394
+ it&#8216;s bounded, the fixed size is returned, otherwise the number of
1395
+ significant binary digits.
1396
+ </p>
1397
+ <h3>Arguments</h3>
1398
+ <p>
1399
+ <em>None</em>.
1400
+ </p>
1401
+ <h3>Examples</h3>
1402
+ <pre>
1403
+ bs = BitString.new('101', 3)
1404
+ bs.length
1405
+ =&gt; 3
1406
+
1407
+ bs = BitString.new('00101', 5)
1408
+ bs.length
1409
+ =&gt; 5
1410
+
1411
+ bs = BitString.new('00101')
1412
+ bs.length
1413
+ =&gt; 3
1414
+ </pre>
1415
+ <h3>Exceptions</h3>
1416
+ <p>
1417
+ <em>None</em>.
1418
+ </p>
1419
+ <p><a class="source-toggle" href="#"
1420
+ onclick="toggleCode('M000009-source');return false;">[Source]</a></p>
1421
+ <div class="method-source-code" id="M000009-source">
1422
+ <pre>
1423
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 820</span>
1424
+ 820: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">length</span>()
1425
+ 821: <span class="ruby-identifier">bounded?</span> <span class="ruby-value">? </span><span class="ruby-ivar">@length</span> <span class="ruby-operator">:</span> <span class="ruby-ivar">@value</span>.<span class="ruby-identifier">to_s</span>(<span class="ruby-value">2</span>).<span class="ruby-identifier">length</span>
1426
+ 822: <span class="ruby-keyword kw">end</span>
1427
+ </pre>
1428
+ </div>
1429
+ </div>
1430
+ </div>
1431
+
1432
+ <div id="method-M000010" class="method-detail">
1433
+ <a name="M000010"></a>
1434
+
1435
+ <div class="method-heading">
1436
+ <a href="#M000010" class="method-signature">
1437
+ <span class="method-name">bitstring.lsb()<br />
1438
+ </span>
1439
+ </a>
1440
+ </div>
1441
+
1442
+ <div class="method-description">
1443
+ <h3>Description</h3>
1444
+ <p>
1445
+ Return the value of the least significant (low) bit.
1446
+ </p>
1447
+ <h3>Arguments</h3>
1448
+ <p>
1449
+ <em>None</em>.
1450
+ </p>
1451
+ <h3>Examples</h3>
1452
+ <pre>
1453
+ bs = BitString.new('101')
1454
+ bs.lsb
1455
+ =&gt; 1
1456
+
1457
+ bs = BitString.new('010')
1458
+ bs.lsb
1459
+ =&gt; 0
1460
+ </pre>
1461
+ <h3>Exceptions</h3>
1462
+ <p>
1463
+ <em>None</em>.
1464
+ </p>
1465
+ <p><a class="source-toggle" href="#"
1466
+ onclick="toggleCode('M000010-source');return false;">[Source]</a></p>
1467
+ <div class="method-source-code" id="M000010-source">
1468
+ <pre>
1469
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 847</span>
1470
+ 847: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">lsb</span>()
1471
+ 848: <span class="ruby-keyword kw">self</span>[<span class="ruby-value">0</span>]
1472
+ 849: <span class="ruby-keyword kw">end</span>
1473
+ </pre>
1474
+ </div>
1475
+ </div>
1476
+ </div>
1477
+
1478
+ <div id="method-M000011" class="method-detail">
1479
+ <a name="M000011"></a>
1480
+
1481
+ <div class="method-heading">
1482
+ <a href="#M000011" class="method-signature">
1483
+ <span class="method-name">bitstring.mask<i>([bitcount], [direction])</i> => <i>Integer</i><br />
1484
+ </span>
1485
+ </a>
1486
+ </div>
1487
+
1488
+ <div class="method-description">
1489
+ <h3>Description</h3>
1490
+ <p>
1491
+ Return an integer with bits set to <a
1492
+ href="BitString.html#M000011">mask</a> the specified portion of the
1493
+ bitstring.
1494
+ </p>
1495
+ <p>
1496
+ If you want to create a <a href="BitString.html#M000011">mask</a> wider
1497
+ than the bitstring, use the class method <em><a
1498
+ href="BitString.html#M000011">BitString.mask</a>()</em> instead.
1499
+ </p>
1500
+ <h3>Arguments</h3>
1501
+ <dl>
1502
+ <dt><em>bitcount</em></dt><dd><em>Integer</em>. Number of bits to set in the <a
1503
+ href="BitString.html#M000011">mask</a>.
1504
+
1505
+ </dd>
1506
+ <dt><em>direction</em></dt><dd><em>Constant</em>. <tt>BitString::HIGH_END</tt> (the default) or
1507
+ <tt>BitString::LOW_END</tt>. Specifies the end of the bitstring from which
1508
+ the <a href="BitString.html#M000011">mask</a> starts.
1509
+
1510
+ </dd>
1511
+ </dl>
1512
+ <h3>Examples</h3>
1513
+ <pre>
1514
+ bs = BitString.new(0, 12)
1515
+ bs.mask.to_s(2)
1516
+ =&gt; &quot;111111111111&quot;
1517
+ bs.mask(5).to_s(2)
1518
+ =&gt; &quot;111110000000&quot;
1519
+ bs.mask(5, BitString::LOW_END).to_s(2)
1520
+ =&gt; &quot;11111&quot;
1521
+ BitString.new(bs.mask(5, BitString::LOW_END), 12).to_s
1522
+ =&gt; &quot;000000011111&quot;
1523
+ </pre>
1524
+ <h3>Exceptions</h3>
1525
+ <dl>
1526
+ <dt><tt>IndexError</tt></dt><dd>Raised if <em>bitcount</em> is negative or grater than the bitstring <a
1527
+ href="BitString.html#M000009">length</a>.
1528
+
1529
+ </dd>
1530
+ </dl>
1531
+ <p><a class="source-toggle" href="#"
1532
+ onclick="toggleCode('M000011-source');return false;">[Source]</a></p>
1533
+ <div class="method-source-code" id="M000011-source">
1534
+ <pre>
1535
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 881</span>
1536
+ 881: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">mask</span>(<span class="ruby-identifier">bits</span>=<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">length</span>, <span class="ruby-identifier">direction</span>=<span class="ruby-constant">HIGH_END</span>)
1537
+ 882: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">OuttasightIndex</span>, <span class="ruby-identifier">bits</span>) <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">length</span>)
1538
+ 883: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">NoDeficitBits</span>) <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span>)
1539
+ 884: <span class="ruby-identifier">vMask</span> = <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">bits</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
1540
+ 885: <span class="ruby-identifier">vMask</span> <span class="ruby-operator">*=</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span>(<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">bits</span>) <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">direction</span> <span class="ruby-operator">==</span> <span class="ruby-constant">HIGH_END</span>)
1541
+ 886: <span class="ruby-identifier">vMask</span>
1542
+ 887: <span class="ruby-keyword kw">end</span>
1543
+ </pre>
1544
+ </div>
1545
+ </div>
1546
+ </div>
1547
+
1548
+ <div id="method-M000013" class="method-detail">
1549
+ <a name="M000013"></a>
1550
+
1551
+ <div class="method-heading">
1552
+ <a href="#M000013" class="method-signature">
1553
+ <span class="method-name">bitstring.msb()<br />
1554
+ </span>
1555
+ </a>
1556
+ </div>
1557
+
1558
+ <div class="method-description">
1559
+ <h3>Description</h3>
1560
+ <p>
1561
+ Return the value of the most significant (high) bit. Only meaningful for
1562
+ bounded bitstrings.
1563
+ </p>
1564
+ <h3>Arguments</h3>
1565
+ <p>
1566
+ <em>None</em>.
1567
+ </p>
1568
+ <h3>Examples</h3>
1569
+ <pre>
1570
+ bs = BitString.new('0101')
1571
+ bs.msb
1572
+ =&gt; 1 # Leading zeroes stripped in unbouded bitstrings
1573
+
1574
+ bs = BitString.new('0101', 4)
1575
+ bs.msb
1576
+ =&gt; 0
1577
+ </pre>
1578
+ <h3>Exceptions</h3>
1579
+ <dl>
1580
+ <dt><tt>RuntimeError</tt></dt><dd>There is no &#8216;MSB&#8217; for an unbounded bitstring.
1581
+
1582
+ </dd>
1583
+ </dl>
1584
+ <p><a class="source-toggle" href="#"
1585
+ onclick="toggleCode('M000013-source');return false;">[Source]</a></p>
1586
+ <div class="method-source-code" id="M000013-source">
1587
+ <pre>
1588
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 937</span>
1589
+ 937: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">msb</span>()
1590
+ 938: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bounded?</span>)
1591
+ 939: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">UnboundedNonsense</span>,
1592
+ 940: <span class="ruby-value str">'most significant bit only applies to bounded bitstrings'</span>)
1593
+ 941: <span class="ruby-keyword kw">end</span>
1594
+ 942: <span class="ruby-keyword kw">self</span>[<span class="ruby-ivar">@length</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>]
1595
+ 943: <span class="ruby-keyword kw">end</span>
1596
+ </pre>
1597
+ </div>
1598
+ </div>
1599
+ </div>
1600
+
1601
+ <div id="method-M000014" class="method-detail">
1602
+ <a name="M000014"></a>
1603
+
1604
+ <div class="method-heading">
1605
+ <a href="#M000014" class="method-signature">
1606
+ <span class="method-name">bitstring.population<i>(testval)</i> => <i>Integer</i><br />
1607
+ </span>
1608
+ </a>
1609
+ </div>
1610
+
1611
+ <div class="method-description">
1612
+ <h3>Description</h3>
1613
+ <p>
1614
+ Return the number of bits in the bitstring that are either set (1) or <a
1615
+ href="BitString.html#M000003">clear</a> (0). Leading zeroes on unbounded
1616
+ bitstrings are ignored.
1617
+ </p>
1618
+ <h3>Arguments</h3>
1619
+ <dl>
1620
+ <dt><em>testval</em></dt><dd><em>Array</em>, <em><a href="BitString.html">BitString</a></em>,
1621
+ <em>Integer</em>, or <em>String</em> whose low bit is used for the test.
1622
+
1623
+ </dd>
1624
+ </dl>
1625
+ <h3>Examples</h3>
1626
+ <pre>
1627
+ bs = BitString.new('0001111001010')
1628
+ bs.population(0)
1629
+ =&gt; 4
1630
+ bs.population(1)
1631
+ =&gt; 6
1632
+
1633
+ bs = BitString.new('0001111001010', 13)
1634
+ bs.population(0)
1635
+ =&gt; 7
1636
+ bs.population(1)
1637
+ =&gt; 6
1638
+ </pre>
1639
+ <h3>Exceptions</h3>
1640
+ <dl>
1641
+ <dt><tt>ArgumentError</tt></dt><dd>The argument cannot be reduced to a binary digit.
1642
+
1643
+ </dd>
1644
+ </dl>
1645
+ <p><a class="source-toggle" href="#"
1646
+ onclick="toggleCode('M000014-source');return false;">[Source]</a></p>
1647
+ <div class="method-source-code" id="M000014-source">
1648
+ <pre>
1649
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 973</span>
1650
+ 973: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">population</span>(<span class="ruby-identifier">val</span>)
1651
+ 974: <span class="ruby-identifier">val</span> = <span class="ruby-identifier">_arg2int</span>(<span class="ruby-identifier">val</span>) <span class="ruby-operator">&amp;</span> <span class="ruby-value">1</span>
1652
+ 975: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">select</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">bval</span><span class="ruby-operator">|</span> <span class="ruby-identifier">bval</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">val</span> }.<span class="ruby-identifier">length</span>
1653
+ 976: <span class="ruby-keyword kw">end</span>
1654
+ </pre>
1655
+ </div>
1656
+ </div>
1657
+ </div>
1658
+
1659
+ <div id="method-M000015" class="method-detail">
1660
+ <a name="M000015"></a>
1661
+
1662
+ <div class="method-heading">
1663
+ <a href="#M000015" class="method-signature">
1664
+ <span class="method-name">bitstring.resize<i>(bits)</i> => <i>BitString</i><br />
1665
+ </span>
1666
+ </a>
1667
+ </div>
1668
+
1669
+ <div class="method-description">
1670
+ <h3>Description</h3>
1671
+ <p>
1672
+ Return a copy of the bitstring resized to the specified number of bits,
1673
+ resulting in truncation or growth. Bits are added to, or removed from, the
1674
+ high (more significant) end. Resizing an unbounded bitstring makes it
1675
+ bounded.
1676
+ </p>
1677
+ <h3>Arguments</h3>
1678
+ <dl>
1679
+ <dt><em>bits</em></dt><dd>Width of the resulting bitstring.
1680
+
1681
+ </dd>
1682
+ </dl>
1683
+ <h3>Examples</h3>
1684
+ <pre>
1685
+ bs = BitString.new('101')
1686
+ bs.bounded?
1687
+ =&gt; false
1688
+ nbs = bs.resize(5)
1689
+ nbs.bounded?
1690
+ =&gt; true
1691
+ nbs.length
1692
+ =&gt; 5
1693
+ nbs.to_s
1694
+ =&gt; &quot;00101&quot;
1695
+ nbs = bs.resize(7)
1696
+ nbs.to_s
1697
+ =&gt; &quot;0000101&quot;
1698
+ </pre>
1699
+ <h3>Exceptions</h3>
1700
+ <dl>
1701
+ <dt><tt>IndexError</tt></dt><dd><em>bits</em> is negative or meaningless.
1702
+
1703
+ </dd>
1704
+ </dl>
1705
+ <p><a class="source-toggle" href="#"
1706
+ onclick="toggleCode('M000015-source');return false;">[Source]</a></p>
1707
+ <div class="method-source-code" id="M000015-source">
1708
+ <pre>
1709
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1010</span>
1710
+ 1010: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">resize</span>(<span class="ruby-identifier">bits</span>)
1711
+ 1011: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bits</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Integer</span>))
1712
+ 1012: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">BitsRInts</span>)
1713
+ 1013: <span class="ruby-keyword kw">end</span>
1714
+ 1014: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>)
1715
+ 1015: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">NoDeficitBits</span>)
1716
+ 1016: <span class="ruby-keyword kw">end</span>
1717
+ 1017: <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@value</span>
1718
+ 1018: <span class="ruby-identifier">length</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">length</span>
1719
+ 1019: <span class="ruby-identifier">bs</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">length</span>)
1720
+ 1020: <span class="ruby-identifier">diffbits</span> = <span class="ruby-identifier">bits</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">length</span>
1721
+ 1021: <span class="ruby-identifier">diffbits</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">shrink!</span>(<span class="ruby-operator">-</span><span class="ruby-identifier">diffbits</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">grow!</span>(<span class="ruby-identifier">diffbits</span>)
1722
+ 1022: <span class="ruby-keyword kw">end</span>
1723
+ </pre>
1724
+ </div>
1725
+ </div>
1726
+ </div>
1727
+
1728
+ <div id="method-M000016" class="method-detail">
1729
+ <a name="M000016"></a>
1730
+
1731
+ <div class="method-heading">
1732
+ <a href="#M000016" class="method-signature">
1733
+ <span class="method-name">bitstring.resize!<i>(bits)</i> => <i>BitString</i><br />
1734
+ </span>
1735
+ </a>
1736
+ </div>
1737
+
1738
+ <div class="method-description">
1739
+ <h3>Description</h3>
1740
+ <p>
1741
+ As <a href="BitString.html#M000015">resize</a> except it&#8216;s the
1742
+ current bitstring that gets resized.
1743
+ </p>
1744
+ <h3>Arguments</h3>
1745
+ <dl>
1746
+ <dt><em>bits</em></dt><dd>Width of the resulting bitstring.
1747
+
1748
+ </dd>
1749
+ </dl>
1750
+ <h3>Examples</h3>
1751
+ <pre>
1752
+ bs = BitString.new('101')
1753
+ bs.bounded?
1754
+ =&gt; false
1755
+ bs.resize!(5)
1756
+ bs.bounded?
1757
+ =&gt; true
1758
+ bs.length
1759
+ =&gt; 5
1760
+ bs.to_s
1761
+ =&gt; &quot;00101&quot;
1762
+ bs.resize!(7)
1763
+ bs.to_s
1764
+ =&gt; &quot;0000101&quot;
1765
+ </pre>
1766
+ <h3>Exceptions</h3>
1767
+ <dl>
1768
+ <dt><tt>IndexError</tt></dt><dd><em>bits</em> is negative or meaningless.
1769
+
1770
+ </dd>
1771
+ </dl>
1772
+ <p><a class="source-toggle" href="#"
1773
+ onclick="toggleCode('M000016-source');return false;">[Source]</a></p>
1774
+ <div class="method-source-code" id="M000016-source">
1775
+ <pre>
1776
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1053</span>
1777
+ 1053: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">resize!</span>(<span class="ruby-identifier">bits</span>)
1778
+ 1054: <span class="ruby-identifier">bs</span> = <span class="ruby-identifier">resize</span>(<span class="ruby-identifier">bits</span>)
1779
+ 1055: <span class="ruby-ivar">@bounded</span> = <span class="ruby-keyword kw">true</span>
1780
+ 1056: <span class="ruby-ivar">@length</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">length</span>
1781
+ 1057: <span class="ruby-ivar">@value</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">to_i</span>
1782
+ 1058: <span class="ruby-keyword kw">self</span>
1783
+ 1059: <span class="ruby-keyword kw">end</span>
1784
+ </pre>
1785
+ </div>
1786
+ </div>
1787
+ </div>
1788
+
1789
+ <div id="method-M000017" class="method-detail">
1790
+ <a name="M000017"></a>
1791
+
1792
+ <div class="method-heading">
1793
+ <a href="#M000017" class="method-signature">
1794
+ <span class="method-name">bistring.rotate<i>(bits)</i> => <i>BitString</i><br />
1795
+ </span>
1796
+ </a>
1797
+ </div>
1798
+
1799
+ <div class="method-description">
1800
+ <h3>Description</h3>
1801
+ <p>
1802
+ Rotate the bitstring, taking bits from one end and shifting them in at the
1803
+ other. Only makes sense with bounded strings.
1804
+ </p>
1805
+ <p>
1806
+ A negative value rotates left; a positive one rotates to the right.
1807
+ </p>
1808
+ <h3>Arguments</h3>
1809
+ <dl>
1810
+ <dt><em>bits</em></dt><dd><em>Integer</em>. Number of positions to <a
1811
+ href="BitString.html#M000017">rotate</a> left (negative) or right
1812
+ (positive). Bits rotated off one end are rotated in on the other.
1813
+
1814
+ </dd>
1815
+ </dl>
1816
+ <h3>Examples</h3>
1817
+ <pre>
1818
+ bs = BitString.new('000000011111', 12)
1819
+ bs.rotate(3).to_s
1820
+ =&gt; &quot;000011111000&quot;
1821
+ bs.rotate(-4).to_s
1822
+ =&gt; &quot;100000001111&quot;
1823
+ </pre>
1824
+ <h3>Exceptions</h3>
1825
+ <dl>
1826
+ <dt><tt>RuntimeError</tt></dt><dd>Can&#8216;t <a href="BitString.html#M000017">rotate</a> an unbounded
1827
+ bitstring.
1828
+
1829
+ </dd>
1830
+ </dl>
1831
+ <p><a class="source-toggle" href="#"
1832
+ onclick="toggleCode('M000017-source');return false;">[Source]</a></p>
1833
+ <div class="method-source-code" id="M000017-source">
1834
+ <pre>
1835
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1085</span>
1836
+ 1085: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">rotate</span>(<span class="ruby-identifier">bits_p</span>)
1837
+ 1086: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bounded?</span>)
1838
+ 1087: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">UnboundedNonsense</span>,
1839
+ 1088: <span class="ruby-value str">'rotation only applies to bounded bitstrings'</span>)
1840
+ 1089: <span class="ruby-keyword kw">end</span>
1841
+ 1090:
1842
+ 1091: <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@value</span>
1843
+ 1092: <span class="ruby-identifier">length</span> = <span class="ruby-ivar">@length</span>
1844
+ 1093: <span class="ruby-identifier">bits</span> = <span class="ruby-identifier">bits_p</span>.<span class="ruby-identifier">to_i</span>.<span class="ruby-identifier">abs</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">length</span>
1845
+ 1094: <span class="ruby-identifier">vMask</span> = (<span class="ruby-identifier">mult</span> = <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">bits</span>) <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
1846
+ 1095: <span class="ruby-identifier">ldiff</span> = <span class="ruby-identifier">length</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">bits</span>
1847
+ 1096: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bits_p</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>)
1848
+ 1097: <span class="ruby-comment cmt">#</span>
1849
+ 1098: <span class="ruby-comment cmt"># Rotate right (toward the LSB)</span>
1850
+ 1099: <span class="ruby-comment cmt">#</span>
1851
+ 1100: <span class="ruby-identifier">residue</span> = <span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">vMask</span>
1852
+ 1101: <span class="ruby-identifier">value</span> <span class="ruby-operator">/=</span> <span class="ruby-identifier">mult</span>
1853
+ 1102: <span class="ruby-identifier">value</span> <span class="ruby-operator">|=</span> <span class="ruby-identifier">residue</span> <span class="ruby-operator">*</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">ldiff</span>
1854
+ 1103: <span class="ruby-keyword kw">elsif</span> (<span class="ruby-identifier">bits_p</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span>)
1855
+ 1104: <span class="ruby-comment cmt">#</span>
1856
+ 1105: <span class="ruby-comment cmt"># Rotate left (toward the MSB)</span>
1857
+ 1106: <span class="ruby-comment cmt">#</span>
1858
+ 1107: <span class="ruby-identifier">vMask</span> <span class="ruby-operator">*=</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">ldiff</span>
1859
+ 1108: <span class="ruby-identifier">residue</span> = <span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">vMask</span>
1860
+ 1109: <span class="ruby-identifier">value</span> = ((<span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;</span> <span class="ruby-operator">~</span><span class="ruby-identifier">vMask</span>) <span class="ruby-operator">*</span> <span class="ruby-identifier">mult</span>) <span class="ruby-operator">|</span> (<span class="ruby-identifier">residue</span> <span class="ruby-operator">/</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">ldiff</span>)
1861
+ 1110: <span class="ruby-keyword kw">end</span>
1862
+ 1111: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>, <span class="ruby-ivar">@length</span>)
1863
+ 1112: <span class="ruby-keyword kw">end</span>
1864
+ </pre>
1865
+ </div>
1866
+ </div>
1867
+ </div>
1868
+
1869
+ <div id="method-M000018" class="method-detail">
1870
+ <a name="M000018"></a>
1871
+
1872
+ <div class="method-heading">
1873
+ <a href="#M000018" class="method-signature">
1874
+ <span class="method-name">bitstring.rotate!<i>(bits)</i> => <i>BitString</i><br />
1875
+ </span>
1876
+ </a>
1877
+ </div>
1878
+
1879
+ <div class="method-description">
1880
+ <h3>Description</h3>
1881
+ <p>
1882
+ Same as <a href="BitString.html#M000017">rotate</a> except that the result
1883
+ is stored back into the current object.
1884
+ </p>
1885
+ <h3>Arguments</h3>
1886
+ <dl>
1887
+ <dt><em>bits</em></dt><dd><em>Integer</em>. Number of positions to <a
1888
+ href="BitString.html#M000017">rotate</a> left (negative) or right
1889
+ (positive). Bits rotated off one end are rotated in on the other.
1890
+
1891
+ </dd>
1892
+ </dl>
1893
+ <h3>Examples</h3>
1894
+ <pre>
1895
+ bs = BitString.new('000000011111', 12)
1896
+ bs.rotate!(3)
1897
+ bs.to_s
1898
+ =&gt; &quot;000011111000&quot;
1899
+ bs.rotate!(-4)
1900
+ bs.to_s
1901
+ =&gt; &quot;100000001111&quot;
1902
+ </pre>
1903
+ <h3>Exceptions</h3>
1904
+ <p>
1905
+ <em>None</em>.
1906
+ </p>
1907
+ <p><a class="source-toggle" href="#"
1908
+ onclick="toggleCode('M000018-source');return false;">[Source]</a></p>
1909
+ <div class="method-source-code" id="M000018-source">
1910
+ <pre>
1911
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1138</span>
1912
+ 1138: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">rotate!</span>(<span class="ruby-identifier">bits_p</span>)
1913
+ 1139: <span class="ruby-ivar">@value</span> = <span class="ruby-identifier">rotate</span>(<span class="ruby-identifier">bits_p</span>).<span class="ruby-identifier">to_i</span>
1914
+ 1140: <span class="ruby-keyword kw">self</span>
1915
+ 1141: <span class="ruby-keyword kw">end</span>
1916
+ </pre>
1917
+ </div>
1918
+ </div>
1919
+ </div>
1920
+
1921
+ <div id="method-M000019" class="method-detail">
1922
+ <a name="M000019"></a>
1923
+
1924
+ <div class="method-heading">
1925
+ <a href="#M000019" class="method-signature">
1926
+ <span class="method-name">bitstring.select { <i>|bit| block</i> } => <i>Array</i><br />
1927
+ </span>
1928
+ </a>
1929
+ </div>
1930
+
1931
+ <div class="method-description">
1932
+ <h3>Description</h3>
1933
+ <p>
1934
+ Iterate through all the bits, invoking the specified block with the value
1935
+ of <a href="BitString.html#M000005">each</a> in turn. If the block returns
1936
+ a true value, the bit value is added to the result array.
1937
+ </p>
1938
+ <h3>Arguments</h3>
1939
+ <dl>
1940
+ <dt><em>bit</em></dt><dd><em>Integer</em>. The value (0 or 1) of the current bit.
1941
+
1942
+ </dd>
1943
+ <dt><em>block</em></dt><dd><em>Proc</em>. The block of code to execute.
1944
+
1945
+ </dd>
1946
+ </dl>
1947
+ <h3>Examples</h3>
1948
+ <pre>
1949
+ bs = BitString.new('11001110001')
1950
+ bs.select { |bit| bit == 1}
1951
+ =&gt; [1, 1, 1, 1, 1, 1]
1952
+
1953
+ bs = BitString.new('0011001110001')
1954
+ bs.select { |bit| bit == 0}
1955
+ =&gt; [0, 0, 0, 0, 0] # because unbounded leadings zeroes are dropped
1956
+
1957
+ bs = BitString.new('0011001110001', 13)
1958
+ bs.select { |bit| bit == 0}
1959
+ =&gt; [0, 0, 0, 0, 0, 0, 0]
1960
+ </pre>
1961
+ <h3>Exceptions</h3>
1962
+ <p>
1963
+ <em>None</em>.
1964
+ </p>
1965
+ <p><a class="source-toggle" href="#"
1966
+ onclick="toggleCode('M000019-source');return false;">[Source]</a></p>
1967
+ <div class="method-source-code" id="M000019-source">
1968
+ <pre>
1969
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1173</span>
1970
+ 1173: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">select</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
1971
+ 1174: <span class="ruby-identifier">result</span> = []
1972
+ 1175: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
1973
+ 1176: <span class="ruby-identifier">result</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">val</span>) <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">val</span>))
1974
+ 1177: <span class="ruby-keyword kw">end</span>
1975
+ 1178: <span class="ruby-identifier">result</span>
1976
+ 1179: <span class="ruby-keyword kw">end</span>
1977
+ </pre>
1978
+ </div>
1979
+ </div>
1980
+ </div>
1981
+
1982
+ <div id="method-M000020" class="method-detail">
1983
+ <a name="M000020"></a>
1984
+
1985
+ <div class="method-heading">
1986
+ <a href="#M000020" class="method-signature">
1987
+ <span class="method-name">bitstring.shrink<i>(bits, [direction])</i><br />
1988
+ </span>
1989
+ </a>
1990
+ </div>
1991
+
1992
+ <div class="method-description">
1993
+ <h3>Description</h3>
1994
+ <p>
1995
+ Shrink the bitstring (make it shorter) by truncating bits from one end or
1996
+ the other. Shrinking to fewer than 1 bits raises an exception.
1997
+ </p>
1998
+ <h3>Arguments</h3>
1999
+ <dl>
2000
+ <dt><em>bits</em></dt><dd><em>Integer</em>. Number of bits to truncate.
2001
+
2002
+ </dd>
2003
+ <dt><em>direction</em></dt><dd><em>Constant</em>. Either <tt>BitString::HIGH_END</tt> (the default) or
2004
+ <tt>BitString::LOW_END</tt>.
2005
+
2006
+ </dd>
2007
+ </dl>
2008
+ <h3>Examples</h3>
2009
+ <pre>
2010
+ bs = BitString.new(3095) # 110000010111
2011
+ nbs = bs.shrink(5)
2012
+ nbs.to_s
2013
+ =&gt; &quot;10111&quot; # Unbounded leading zeroes aren't significant
2014
+ nbs = nbs.shrink(2, BitString::LOW_END)
2015
+ nbs.to_s
2016
+ =&gt; &quot;101&quot;
2017
+
2018
+ bs = BitString.new(3095, 12) # 110000010111
2019
+ nbs = bs.shrink(5)
2020
+ nbs.to_s
2021
+ =&gt; &quot;0010111&quot;
2022
+ </pre>
2023
+ <h3>Exceptions</h3>
2024
+ <dl>
2025
+ <dt><tt>ArgumentError</tt></dt><dd><em>bitcount</em> isn&#8216;t an integer or <em>direction</em> isn&#8216;t
2026
+ one of the defined values.
2027
+
2028
+ </dd>
2029
+ <dt><tt>IndexError</tt></dt><dd><em>bits</em> is negative or meaningless.
2030
+
2031
+ </dd>
2032
+ </dl>
2033
+ <p><a class="source-toggle" href="#"
2034
+ onclick="toggleCode('M000020-source');return false;">[Source]</a></p>
2035
+ <div class="method-source-code" id="M000020-source">
2036
+ <pre>
2037
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1213</span>
2038
+ 1213: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">shrink</span>(<span class="ruby-identifier">bits</span>=<span class="ruby-value">1</span>, <span class="ruby-identifier">direction</span>=<span class="ruby-constant">HIGH_END</span>)
2039
+ 1214: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bits</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Integer</span>))
2040
+ 1215: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">BitsRInts</span>)
2041
+ 1216: <span class="ruby-keyword kw">end</span>
2042
+ 1217: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">0</span>)
2043
+ 1218: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">NoDeficitBits</span>)
2044
+ 1219: <span class="ruby-keyword kw">end</span>
2045
+ 1220: <span class="ruby-keyword kw">unless</span> ([<span class="ruby-constant">HIGH_END</span>, <span class="ruby-constant">LOW_END</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">direction</span>))
2046
+ 1221: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">NeedGPS</span>)
2047
+ 1222: <span class="ruby-keyword kw">end</span>
2048
+ 1223: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">dup</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>)
2049
+ 1224:
2050
+ 1225: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bounded?</span> <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">bits</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-ivar">@length</span>))
2051
+ 1226: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">RuntimeError</span>, <span class="ruby-value str">'shrink count greater than bitstring size'</span>)
2052
+ 1227: <span class="ruby-keyword kw">end</span>
2053
+ 1228: <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@value</span>
2054
+ 1229: <span class="ruby-identifier">length</span> = <span class="ruby-identifier">bounded?</span> <span class="ruby-value">? </span><span class="ruby-ivar">@length</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">bits</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">nil</span>
2055
+ 1230: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">direction</span> <span class="ruby-operator">==</span> <span class="ruby-constant">LOW_END</span>)
2056
+ 1231: <span class="ruby-identifier">value</span> <span class="ruby-operator">/=</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">bits</span>
2057
+ 1232: <span class="ruby-keyword kw">else</span>
2058
+ 1233: <span class="ruby-identifier">_raise</span>(<span class="ruby-constant">UnboundedNonsense</span>) <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">bounded?</span>)
2059
+ 1234: <span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;=</span> <span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-identifier">length</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
2060
+ 1235: <span class="ruby-keyword kw">end</span>
2061
+ 1236: <span class="ruby-identifier">bounded?</span> <span class="ruby-value">? </span><span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">length</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">value</span>)
2062
+ 1237: <span class="ruby-keyword kw">end</span>
2063
+ </pre>
2064
+ </div>
2065
+ </div>
2066
+ </div>
2067
+
2068
+ <div id="method-M000021" class="method-detail">
2069
+ <a name="M000021"></a>
2070
+
2071
+ <div class="method-heading">
2072
+ <a href="#M000021" class="method-signature">
2073
+ <span class="method-name">bitstring.shrink!<i>(bits, [direction])</i><br />
2074
+ </span>
2075
+ </a>
2076
+ </div>
2077
+
2078
+ <div class="method-description">
2079
+ <h3>Description</h3>
2080
+ <p>
2081
+ As <a href="BitString.html#M000020">shrink</a> except that the current
2082
+ bitstring is modified rather than a copy being made and altered.
2083
+ </p>
2084
+ <h3>Arguments</h3>
2085
+ <dl>
2086
+ <dt><em>bits</em></dt><dd><em>Integer</em>. Number of bits to truncate.
2087
+
2088
+ </dd>
2089
+ <dt><em>direction</em></dt><dd><em>Constant</em>. <tt>BitString::HIGH_END</tt> (the default) or
2090
+ <tt>BitString::LOW_END</tt>.
2091
+
2092
+ </dd>
2093
+ </dl>
2094
+ <h3>Examples</h3>
2095
+ <pre>
2096
+ bs = BitString.new(3095) # 110000010111
2097
+ bs.shrink!(5)
2098
+ bs.to_s
2099
+ =&gt; &quot;10111&quot; # Unbounded leading zeroes aren't significant
2100
+ bs.shrink!(2, BitString::LOW_END)
2101
+ bs.to_s
2102
+ =&gt; &quot;101&quot;
2103
+
2104
+ bs = BitString.new(3095, 12) # 110000010111
2105
+ bs.shrink!(5)
2106
+ bs.to_s
2107
+ =&gt; &quot;0010111&quot;
2108
+ </pre>
2109
+ <h3>Exceptions</h3>
2110
+ <dl>
2111
+ <dt><tt>ArgumentError</tt></dt><dd><em>bitcount</em> isn&#8216;t an integer or <em>direction</em> isn&#8216;t
2112
+ one of the defined values.
2113
+
2114
+ </dd>
2115
+ <dt><tt>IndexError</tt></dt><dd><em>bits</em> is negative or meaningless.
2116
+
2117
+ </dd>
2118
+ </dl>
2119
+ <p><a class="source-toggle" href="#"
2120
+ onclick="toggleCode('M000021-source');return false;">[Source]</a></p>
2121
+ <div class="method-source-code" id="M000021-source">
2122
+ <pre>
2123
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1270</span>
2124
+ 1270: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">shrink!</span>(<span class="ruby-identifier">bits</span>=<span class="ruby-value">1</span>, <span class="ruby-identifier">direction</span>=<span class="ruby-constant">HIGH_END</span>)
2125
+ 1271: <span class="ruby-identifier">bs</span> = <span class="ruby-identifier">shrink</span>(<span class="ruby-identifier">bits</span>, <span class="ruby-identifier">direction</span>)
2126
+ 1272: <span class="ruby-ivar">@length</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">length</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bs</span>.<span class="ruby-identifier">bounded?</span>)
2127
+ 1273: <span class="ruby-ivar">@value</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">to_i</span>
2128
+ 1274: <span class="ruby-keyword kw">self</span>
2129
+ 1275: <span class="ruby-keyword kw">end</span>
2130
+ </pre>
2131
+ </div>
2132
+ </div>
2133
+ </div>
2134
+
2135
+ <div id="method-M000022" class="method-detail">
2136
+ <a name="M000022"></a>
2137
+
2138
+ <div class="method-heading">
2139
+ <a href="#M000022" class="method-signature">
2140
+ <span class="method-name">bitstring.slice<i>(index)</i> => <i>Integer</i><br />
2141
+ bitstring.slice<i>(start, length)</i> => <i>BitString</i><br />
2142
+ bitstring.slice<i>(range)</i> => <i>BitString</i><br />
2143
+ </span>
2144
+ </a>
2145
+ </div>
2146
+
2147
+ <div class="method-description">
2148
+ <h3>Description</h3>
2149
+ <p>
2150
+ Extract a subset from the bitstring. See the description of the <tt>[]</tt>
2151
+ method.
2152
+ </p>
2153
+ <h3>Arguments</h3>
2154
+ <dl>
2155
+ <dt><em>index</em></dt><dd><em>Integer</em>. Single bit position.
2156
+
2157
+ </dd>
2158
+ <dt><em>start</em></dt><dd><em>Integer</em>. Start position of subset.
2159
+
2160
+ </dd>
2161
+ <dt><em><a href="BitString.html#M000009">length</a></em></dt><dd><em>Integer</em>. Length of subset.
2162
+
2163
+ </dd>
2164
+ <dt><em>range</em></dt><dd><em>Range</em>. Subset specified as a range.
2165
+
2166
+ </dd>
2167
+ </dl>
2168
+ <h3>Exceptions</h3>
2169
+ <dl>
2170
+ <dt><tt>ArgumentError</tt></dt><dd>If <a href="BitString.html#M000009">length</a> specified with a range.
2171
+
2172
+ </dd>
2173
+ <dt><tt>IndexError</tt></dt><dd>If bounded bitstring and substring is illegal.
2174
+
2175
+ </dd>
2176
+ </dl>
2177
+ <p><a class="source-toggle" href="#"
2178
+ onclick="toggleCode('M000022-source');return false;">[Source]</a></p>
2179
+ <div class="method-source-code" id="M000022-source">
2180
+ <pre>
2181
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1298</span>
2182
+ 1298: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">slice</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
2183
+ 1299: <span class="ruby-keyword kw">self</span>[<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>]
2184
+ 1300: <span class="ruby-keyword kw">end</span>
2185
+ </pre>
2186
+ </div>
2187
+ </div>
2188
+ </div>
2189
+
2190
+ <div id="method-M000023" class="method-detail">
2191
+ <a name="M000023"></a>
2192
+
2193
+ <div class="method-heading">
2194
+ <a href="#M000023" class="method-signature">
2195
+ <span class="method-name">bitstring.slice!<i>(index)</i> => <i>Integer</i><br />
2196
+ bitstring.slice!<i>(start, length)</i> => <i>BitString</i><br />
2197
+ bitstring.slice!<i>(range)</i> => <i>BitString</i><br />
2198
+ </span>
2199
+ </a>
2200
+ </div>
2201
+
2202
+ <div class="method-description">
2203
+ <h3>Description</h3>
2204
+ <p>
2205
+ Select the specified bits from the bitstring, and remake it using only
2206
+ those bits. If bounded, the size will be adjusted to match the number of
2207
+ bits selected. This is an alternative way to change the size and value of
2208
+ an existing bitstring (see the <a
2209
+ href="BitString.html#M000008">grow!</a>(), <a
2210
+ href="BitString.html#M000021">shrink!</a>(), and <a
2211
+ href="BitString.html#M000016">resize!</a>() methods.)
2212
+ </p>
2213
+ <h3>Arguments</h3>
2214
+ <dl>
2215
+ <dt><em>index</em></dt><dd><em>Integer</em>. Single bit position.
2216
+
2217
+ </dd>
2218
+ <dt><em>start</em></dt><dd><em>Integer</em>. Start position of subset.
2219
+
2220
+ </dd>
2221
+ <dt><em><a href="BitString.html#M000009">length</a></em></dt><dd><em>Integer</em>. Length of subset.
2222
+
2223
+ </dd>
2224
+ <dt><em>range</em></dt><dd><em>Range</em>. Subset specified as a range.
2225
+
2226
+ </dd>
2227
+ </dl>
2228
+ <h3>Examples</h3>
2229
+ <pre>
2230
+ bs = BitString.new(3095, 12)
2231
+ bs.to_s
2232
+ =&gt; &quot;110000010111&quot;
2233
+ bs.slice!(4..10)
2234
+ bs.to_s
2235
+ =&gt; &quot;1000001&quot;
2236
+ </pre>
2237
+ <h3>Exceptions</h3>
2238
+ <dl>
2239
+ <dt><tt>ArgumentError</tt></dt><dd>If <a href="BitString.html#M000009">length</a> specified with a range.
2240
+
2241
+ </dd>
2242
+ <dt><tt>IndexError</tt></dt><dd>If bounded bitstring and substring is illegal.
2243
+
2244
+ </dd>
2245
+ </dl>
2246
+ <p><a class="source-toggle" href="#"
2247
+ onclick="toggleCode('M000023-source');return false;">[Source]</a></p>
2248
+ <div class="method-source-code" id="M000023-source">
2249
+ <pre>
2250
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1334</span>
2251
+ 1334: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">slice!</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
2252
+ 1335: <span class="ruby-identifier">bs</span> = <span class="ruby-keyword kw">self</span>[<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>]
2253
+ 1336: <span class="ruby-ivar">@value</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">to_i</span>
2254
+ 1337: <span class="ruby-ivar">@bounded</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">bounded?</span>
2255
+ 1338: <span class="ruby-ivar">@length</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">length</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bs</span>.<span class="ruby-identifier">bounded?</span>)
2256
+ 1339: <span class="ruby-keyword kw">self</span>
2257
+ 1340: <span class="ruby-keyword kw">end</span>
2258
+ </pre>
2259
+ </div>
2260
+ </div>
2261
+ </div>
2262
+
2263
+ <div id="method-M000024" class="method-detail">
2264
+ <a name="M000024"></a>
2265
+
2266
+ <div class="method-heading">
2267
+ <a href="#M000024" class="method-signature">
2268
+ <span class="method-name">bitstring.to_i<i>()</i> => <i>Integer</i><br />
2269
+ </span>
2270
+ </a>
2271
+ </div>
2272
+
2273
+ <div class="method-description">
2274
+ <h3>Description</h3>
2275
+ <p>
2276
+ Return the full value of the bitstring represented as an integer.
2277
+ </p>
2278
+ <h3>Arguments</h3>
2279
+ <p>
2280
+ <em>None</em>.
2281
+ </p>
2282
+ <h3>Examples</h3>
2283
+ <pre>
2284
+ bs = BitString.new('110000010111')
2285
+ bs.to_i
2286
+ =&gt; 3095
2287
+ </pre>
2288
+ <h3>Exceptions</h3>
2289
+ <p>
2290
+ <em>None</em>.
2291
+ </p>
2292
+ <p><a class="source-toggle" href="#"
2293
+ onclick="toggleCode('M000024-source');return false;">[Source]</a></p>
2294
+ <div class="method-source-code" id="M000024-source">
2295
+ <pre>
2296
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1361</span>
2297
+ 1361: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_i</span>()
2298
+ 1362: <span class="ruby-ivar">@value</span>.<span class="ruby-identifier">to_i</span>
2299
+ 1363: <span class="ruby-keyword kw">end</span>
2300
+ </pre>
2301
+ </div>
2302
+ </div>
2303
+ </div>
2304
+
2305
+ <div id="method-M000025" class="method-detail">
2306
+ <a name="M000025"></a>
2307
+
2308
+ <div class="method-heading">
2309
+ <a href="#M000025" class="method-signature">
2310
+ <span class="method-name">bitstring.to_s<i>()</i> => <i>String</i><br />
2311
+ </span>
2312
+ </a>
2313
+ </div>
2314
+
2315
+ <div class="method-description">
2316
+ <h3>Description</h3>
2317
+ <p>
2318
+ Return the bitlist as a <em>String</em> object consisting of
2319
+ &#8216;0&#8217; and &#8216;1&#8217; characters. If the bitstring is
2320
+ bounded, the <em>String</em> will be zero-filled on the left (high end).
2321
+ </p>
2322
+ <h3>Arguments</h3>
2323
+ <p>
2324
+ <em>None</em>.
2325
+ </p>
2326
+ <h3>Examples</h3>
2327
+ <pre>
2328
+ bs = BitString.new(3095)
2329
+ bs.to_s
2330
+ =&gt; &quot;110000010111&quot;
2331
+
2332
+ bs = BitString.new(3095, 14)
2333
+ bs.to_s
2334
+ =&gt; &quot;00110000010111&quot;
2335
+ </pre>
2336
+ <h3>Exceptions</h3>
2337
+ <p>
2338
+ <em>None</em>.
2339
+ </p>
2340
+ <p><a class="source-toggle" href="#"
2341
+ onclick="toggleCode('M000025-source');return false;">[Source]</a></p>
2342
+ <div class="method-source-code" id="M000025-source">
2343
+ <pre>
2344
+ <span class="ruby-comment cmt"># File lib/bitstring.rb, line 1390</span>
2345
+ 1390: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_s</span>()
2346
+ 1391: <span class="ruby-identifier">_zfill</span>(<span class="ruby-ivar">@value</span>, <span class="ruby-ivar">@length</span>)
2347
+ 1392: <span class="ruby-keyword kw">end</span>
2348
+ </pre>
2349
+ </div>
2350
+ </div>
2351
+ </div>
2352
+
2353
+ <div id="method-M000032" class="method-detail">
2354
+ <a name="M000032"></a>
2355
+
2356
+ <div class="method-heading">
2357
+ <a href="#M000032" class="method-signature">
2358
+ <span class="method-name">bitstring <i>| value</i> => <i>BitString</i><br />
2359
+ bitstring.|<i>(value)</i> => <i>BitString</i><br />
2360
+ </span>
2361
+ </a>
2362
+ </div>
2363
+
2364
+ <div class="method-description">
2365
+ <h3>Description</h3>
2366
+ <p>
2367
+ Perform a bitwise inclusive OR with the current bitstring and return a
2368
+ bitstring containing the result.
2369
+ </p>
2370
+ <h3>Arguments</h3>
2371
+ <dl>
2372
+ <dt><em>value</em></dt><dd><em>Array</em>, <em><a href="BitString.html">BitString</a></em>,
2373
+ <em>Integer</em>, or <em>String</em>. Value treated as a bitstream and
2374
+ inclusively ORed with the bitstring.
2375
+
2376
+ </dd>
2377
+ </dl>
2378
+ <h3>Examples</h3>
2379
+ <pre>
2380
+ bs = BitString.new('110010110')
2381
+ nbs = bs | '11000000000000000'
2382
+ nbs.to_s # Bits cab be ORed in anywhere in an
2383
+ =&gt; &quot;11000000110010110&quot; # unbouded string
2384
+ </pre>
2385
+ <h3>Exceptions</h3>
2386
+ <p>
2387
+ <em>None</em>.
2388
+ </p>
2389
+ <p><a class="source-toggle" href="#"
2390
+ onclick="toggleCode('M000032-source');return false;">[Source]</a></p>
2391
+ <div class="method-source-code" id="M000032-source">
2392
+ <pre>
2393
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 392</span>
2394
+ 392: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">|</span>(<span class="ruby-identifier">value</span>)
2395
+ 393: <span class="ruby-identifier">value</span> = <span class="ruby-identifier">_arg2int</span>(<span class="ruby-identifier">value</span>)
2396
+ 394: <span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;=</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">mask</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">bounded?</span>)
2397
+ 395: <span class="ruby-identifier">bs</span> = <span class="ruby-identifier">dup</span>
2398
+ 396: <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">from_i</span>(<span class="ruby-identifier">value</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">to_i</span>)
2399
+ 397: <span class="ruby-identifier">bs</span>
2400
+ 398: <span class="ruby-keyword kw">end</span>
2401
+ </pre>
2402
+ </div>
2403
+ </div>
2404
+ </div>
2405
+
2406
+ <div id="method-M000033" class="method-detail">
2407
+ <a name="M000033"></a>
2408
+
2409
+ <div class="method-heading">
2410
+ <a href="#M000033" class="method-signature">
2411
+ <span class="method-name"><i>~</i> bitstring => <i>BitString</i><br />
2412
+ bitstring.~<i>()</i> => <i>BitString</i><br />
2413
+ </span>
2414
+ </a>
2415
+ </div>
2416
+
2417
+ <div class="method-description">
2418
+ <h3>Description</h3>
2419
+ <p>
2420
+ Perform a one&#8216;s complement on the current bitstring and return the
2421
+ result in a <a href="BitString.html#M000001">new</a> one.
2422
+ </p>
2423
+ <h3>Arguments</h3>
2424
+ <p>
2425
+ <em>None</em>.
2426
+ </p>
2427
+ <h3>Examples</h3>
2428
+ <pre>
2429
+ bs = BitString.new('110010110')
2430
+ nbs = ~ bs
2431
+ nbs.to_s
2432
+ =&gt; &quot;1101001&quot; # Leading zeroes stripped when unbounded
2433
+
2434
+ bs = BitString.new('110010110', 9)
2435
+ nbs = ~ bs
2436
+ nbs.to_s
2437
+ =&gt; &quot;001101001&quot;
2438
+
2439
+ bs = BitString.new('111111111')
2440
+ nbs = ~ bs
2441
+ nbs.to_s
2442
+ =&gt; &quot;0&quot;
2443
+
2444
+ bs = BitString.new('111111111', 9)
2445
+ nbs = ~ bs
2446
+ nbs.to_s
2447
+ =&gt; &quot;000000000&quot;
2448
+ </pre>
2449
+ <h3>Exceptions</h3>
2450
+ <p>
2451
+ <em>None</em>.
2452
+ </p>
2453
+ <p><a class="source-toggle" href="#"
2454
+ onclick="toggleCode('M000033-source');return false;">[Source]</a></p>
2455
+ <div class="method-source-code" id="M000033-source">
2456
+ <pre>
2457
+ <span class="ruby-comment cmt"># File lib/bitstring/operators.rb, line 437</span>
2458
+ 437: <span class="ruby-keyword kw">def</span> <span class="ruby-operator">~</span>()
2459
+ 438: <span class="ruby-identifier">newval</span> = (<span class="ruby-operator">~</span> <span class="ruby-ivar">@value</span>) <span class="ruby-operator">&amp;</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">mask</span>
2460
+ 439: <span class="ruby-identifier">bounded?</span> <span class="ruby-value">? </span><span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">newval</span>, <span class="ruby-ivar">@length</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">newval</span>)
2461
+ 440: <span class="ruby-keyword kw">end</span>
2462
+ </pre>
2463
+ </div>
2464
+ </div>
2465
+ </div>
2466
+
2467
+
2468
+ </div>
2469
+
2470
+
2471
+ </div>
2472
+
2473
+
2474
+ <div id="validator-badges">
2475
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
2476
+ </div>
2477
+
2478
+ </body>
2479
+ </html>