1 | [[tags: egg]] |
---|
2 | |
---|
3 | == bitwise-utils |
---|
4 | |
---|
5 | [[toc:]] |
---|
6 | |
---|
7 | |
---|
8 | == Documentation |
---|
9 | |
---|
10 | Additional bitwise routines. |
---|
11 | |
---|
12 | === logical-shift-left |
---|
13 | |
---|
14 | <procedure>(logical-shift-left N1 N2) --> integer</procedure> |
---|
15 | |
---|
16 | Returns left shift of {{N1}} by {{N2}} bits. |
---|
17 | |
---|
18 | ; {{N1}} : {{integer}} ; what to shift. |
---|
19 | ; {{N2}} : {{fixnum}} ; amount to shift. |
---|
20 | |
---|
21 | Aliases {{arithmetic-shift}}. |
---|
22 | |
---|
23 | === logical-shift-right |
---|
24 | |
---|
25 | <procedure>(logical-shift-right N1 N2) --> integer</procedure> |
---|
26 | |
---|
27 | Returns right shift of {{N1}} by {{N2}} bits. Sign preserving. |
---|
28 | |
---|
29 | ; {{N1}} : {{integer}} ; what to shift. |
---|
30 | ; {{N2}} : {{fixnum}} ; amount to shift. |
---|
31 | |
---|
32 | === arithmetic-shift-left |
---|
33 | |
---|
34 | <procedure>(arithmetic-shift-left N1 N2) --> integer</procedure> |
---|
35 | |
---|
36 | Returns left shift of {{N1}} by {{N2}} bits. |
---|
37 | |
---|
38 | ; {{N1}} : {{integer}} ; what to shift. |
---|
39 | ; {{N2}} : {{fixnum}} ; amount to shift. |
---|
40 | |
---|
41 | Aliases {{arithmetic-shift}}. |
---|
42 | |
---|
43 | === arithmetic-shift-right |
---|
44 | |
---|
45 | <procedure>(arithmetic-shift-right N1 N2) --> integer</procedure> |
---|
46 | |
---|
47 | Returns right shift of {{N1}} by {{N2}} bits. Sign extending. |
---|
48 | |
---|
49 | ; {{N1}} : {{integer}} ; what to shift. |
---|
50 | ; {{N2}} : {{fixnum}} ; amount to shift. |
---|
51 | |
---|
52 | Essentially {{(arithmetic-shift <> (- <>))}}. |
---|
53 | |
---|
54 | === bitwise-mask |
---|
55 | |
---|
56 | <procedure>(bitwise-mask WID [ON?]) --> integer</procedure> |
---|
57 | |
---|
58 | Returns {{WID}} low-order bits {{ON?}}. |
---|
59 | |
---|
60 | ; {{WID}} : {{fixnum}} ; mask bit width. |
---|
61 | ; {{ON?}} : {{boolean}} ; {{#t}} is {{#b1}}, {{#f}} is {{#b0}}; default {{#t}}. |
---|
62 | |
---|
63 | <enscript language=scheme> |
---|
64 | (bitwise-mask 5 #t) ;=> +0...011111 (#b+011111) |
---|
65 | (bitwise-mask 5 #f) ;=> -1...100000 (#b-100000) |
---|
66 | </enscript> |
---|
67 | |
---|
68 | === bitwise-join |
---|
69 | |
---|
70 | <procedure>(bitwise-join N0 [N1 ...]) --> integer</procedure> |
---|
71 | |
---|
72 | Returns adjoined bits. |
---|
73 | |
---|
74 | ; {{N0}} : {{integer}} ; seed. |
---|
75 | ; {{N1 ...}} : {{integer ...}} ; adjoins seed left-to-right. |
---|
76 | |
---|
77 | <enscript language=scheme> |
---|
78 | (bitwise-join #b10 #b0000001 #b101) ;=> #b101101 |
---|
79 | </enscript> |
---|
80 | |
---|
81 | === bitwise-split |
---|
82 | |
---|
83 | <procedure>(bitwise-split N WID) --> (list-of integer)</procedure> |
---|
84 | |
---|
85 | Returns {{N}} disjoined every {{WID}} bits. |
---|
86 | |
---|
87 | ; {{N}} : {{integer}} ; seed. |
---|
88 | ; {{WID}} : {{fixnum}} ; bit width. |
---|
89 | |
---|
90 | <enscript language=scheme> |
---|
91 | (bitwise-split #xabcdef 4) ;=> (#xa #xb #xc #xd #xe #xf) |
---|
92 | (bitwise-split #x-abcdef 4) ;=> (#x-a #x-b #x-c #x-d #x-e #x-f) |
---|
93 | </enscript> |
---|
94 | |
---|
95 | === bitwise-count |
---|
96 | |
---|
97 | <procedure>(bitwise-count N) --> fixnum</procedure> |
---|
98 | |
---|
99 | Returns number of set bits in {{N}}. |
---|
100 | |
---|
101 | ; {{N}} : {{integer}} |
---|
102 | |
---|
103 | === bitwise-merge |
---|
104 | |
---|
105 | <procedure>(bitwise-merge MASK N0 N1) --> integer</procedure> |
---|
106 | |
---|
107 | ; {{MASK}} : {{integer}} |
---|
108 | ; {{N0}} : {{integer}} |
---|
109 | ; {{N1}} : {{integer}} |
---|
110 | |
---|
111 | Renamed {{SRFI-60}} {{bitwise-if}}. |
---|
112 | |
---|
113 | === bitwise-nth? |
---|
114 | |
---|
115 | <procedure>(bitwise-nth? INDEX N) --> boolean</procedure> |
---|
116 | |
---|
117 | ; {{INDEX}} : {{integer}} |
---|
118 | ; {{N}} : {{fixnum}} |
---|
119 | |
---|
120 | Renamed {{SRFI-60}} {{bit-set?}}. |
---|
121 | |
---|
122 | === bitwise-any? |
---|
123 | |
---|
124 | <procedure>(bitwise-any? N1 N2) --> boolean</procedure> |
---|
125 | |
---|
126 | ; {{N1}} : {{integer}} |
---|
127 | ; {{N2}} : {{integer}} |
---|
128 | |
---|
129 | Renamed {{SRFI-60}} {{any-bits-set?}}. |
---|
130 | |
---|
131 | === bitwise-first-set |
---|
132 | |
---|
133 | <procedure>(bitwise-first-set N) --> fixnum</procedure> |
---|
134 | |
---|
135 | ; {{N}} :integer |
---|
136 | |
---|
137 | Renamed {{SRFI-60}} {{first-set-bit}}. |
---|
138 | |
---|
139 | === bitwise-reverse |
---|
140 | |
---|
141 | <procedure>(bitwise-reverse N K) --> integer</procedure> |
---|
142 | |
---|
143 | ; {{N}} :integer |
---|
144 | ; {{K}} :fixnum |
---|
145 | |
---|
146 | === bitwise-rotate |
---|
147 | |
---|
148 | <procedure>(bitwise-rotate K COUNT LEN) --> integer</procedure> |
---|
149 | |
---|
150 | Performs {{(bitwise-field-rotate K COUNT 0 LEN)}}. |
---|
151 | |
---|
152 | ; {{K}} : {{integer}} |
---|
153 | ; {{COUNT}} : {{fixnum}} |
---|
154 | ; {{LEN}} : {{fixnum}} |
---|
155 | |
---|
156 | === bitwise-set-nth |
---|
157 | |
---|
158 | <procedure>(bitwise-set-nth TO INDEX ON?) --> integer</procedure> |
---|
159 | |
---|
160 | ; {{TO}} : {{integer}} |
---|
161 | ; {{INDEX}} : {{fixnum}} |
---|
162 | ; {{ON?}} : {{boolean}} ; {{#t}} is {{#b1}}, {{#f}} is {{#b0}} |
---|
163 | |
---|
164 | Renamed {{SRFI-60}} {{(copy-bit k n b)}}; note argument order change. |
---|
165 | |
---|
166 | === bitwise-field |
---|
167 | |
---|
168 | <procedure>(bitwise-field N START END) --> integer</procedure> |
---|
169 | |
---|
170 | ; {{N}} : {{integer}} |
---|
171 | ; {{START}} : {{fixnum}} |
---|
172 | ; {{END}} : {{fixnum}} |
---|
173 | |
---|
174 | Renamed {{SRFI-60}} {{bit-field}}. |
---|
175 | |
---|
176 | === bitwise-field-copy |
---|
177 | |
---|
178 | <procedure>(bitwise-field-copy TO FROM START END) --> integer</procedure> |
---|
179 | |
---|
180 | ; {{TO}} : {{integer}} |
---|
181 | ; {{FROM}} : integer |
---|
182 | ; {{START}} : {{fixnum}} |
---|
183 | ; {{END}} : {{fixnum}} |
---|
184 | |
---|
185 | Renamed {{SRFI-60}} {{copy-bit-field}}. |
---|
186 | |
---|
187 | === bitwise-field-reverse |
---|
188 | |
---|
189 | <procedure>(bitwise-field-reverse N START END) --> integer</procedure> |
---|
190 | |
---|
191 | ; {{N}} : {{integer}} |
---|
192 | ; {{START}} : {{fixnum}} |
---|
193 | ; {{END}} : {{fixnum}} |
---|
194 | |
---|
195 | Renamed {{SRFI-60}} {{reverse-bit-field}}. |
---|
196 | |
---|
197 | <procedure>(bitwise-field-rotate N COUNT START END) --> integer</procedure> |
---|
198 | |
---|
199 | ; {{N}} : {{integer}} |
---|
200 | ; {{COUNT}} : {{fixnum}} |
---|
201 | ; {{START}} : {{fixnum}} |
---|
202 | ; {{END}} : {{fixnum}} |
---|
203 | |
---|
204 | Renamed {{SRFI-60}} {{rotate-bit-field}}. |
---|
205 | |
---|
206 | === integer->list |
---|
207 | |
---|
208 | <procedure>(integer->list K [LEN]) --> (list-of fixnum)</procedure> |
---|
209 | |
---|
210 | ; {{K}} : {{integer}} |
---|
211 | ; {{LEN}} : {{(or false fixnum)}} ; {{#f}} is all bits, otherwise number of bits |
---|
212 | |
---|
213 | === list->integer |
---|
214 | |
---|
215 | <procedure>(list->integer BOOLS) --> integer</procedure> |
---|
216 | |
---|
217 | ; {{BOOLS}} : {{list}} ; treated as {{(list-of boolean-like)}} |
---|
218 | |
---|
219 | |
---|
220 | === Usage |
---|
221 | |
---|
222 | <enscript language=scheme> |
---|
223 | (import bitwise-utils) |
---|
224 | </enscript> |
---|
225 | |
---|
226 | |
---|
227 | == Author |
---|
228 | |
---|
229 | [[/users/kon-lovett|Kon Lovett]] |
---|
230 | |
---|
231 | |
---|
232 | == Version history |
---|
233 | |
---|
234 | ; 1.2.0 : Remove {{srfi-60}} module. (Use {{srfi-60}} egg.) |
---|
235 | ; 1.1.4: Fix {{integer->list}} type. |
---|
236 | ; 1.1.3 : . |
---|
237 | ; 1.1.2 : . |
---|
238 | ; 1.1.1 : . |
---|
239 | ; 1.1.0 : Add {{srfi-60}} module. |
---|
240 | ; 1.0.1 : . |
---|
241 | ; 1.0.0 : Release. |
---|
242 | |
---|
243 | |
---|
244 | == License |
---|
245 | |
---|
246 | This code is in the public domain. |
---|