on 8564483 help sign in
676 lines 9.2 kB history raw
1---
2navhome: /docs
3---
6# 2c: bit arithmetic
7### `++bex`
9Binary exponent
11Computes the result of `2^a`, producing an atom.
13Accepts
14-------
16`a` is an atom.
18Produces
19--------
21An atom.
23Source
24------
26 ++ bex :: binary exponent
27 ~/ %bex
28 |= a/@
29 ^- @
30 ?: =(0 a) 1
31 (mul 2 $(a (dec a)))
33Examples
34--------
36 > (bex 4)
37 16
38 > (bex (add 19 1))
39 1.048.576
40 > (bex 0)
41 1
45***
47### `++can`
49Assemble
51Produces an atom from a list `b` of length-value pairs `p` and `q`,
52where `p` is the length in bloqs of size `a`, and `q` is an atomic
53value.
55Accepts
56-------
58`a` is a block size (see `++bloq`).
60`b` is a `++list` of length value pairs, `p` and `q`.
62Produces
63--------
65An atom.
67Source
68------
70 ++ can :: assemble
71 ~/ %can
72 |= [a/bloq b/(list [p=@ q=@])]
73 ^- @
74 ?~ b 0
75 (mix (end a p.i.b q.i.b) (lsh a p.i.b $(b t.b)))
77Examples
78--------
80 > `@ub`(can 3 ~[[1 1]])
81 0b1
82 > `@ub`(can 0 ~[[1 255]])
83 0b1
84 > `@ux`(can 3 [3 0xc1] [1 0xa] ~)
85 0xa00.00c1
86 > `@ux`(can 3 [3 0xc1] [1 0xa] [1 0x23] ~)
87 0x23.0a00.00c1
88 > `@ux`(can 4 [3 0xc1] [1 0xa] [1 0x23] ~)
89 0x23.000a.0000.0000.00c1
90 > `@ux`(can 3 ~[[1 'a'] [2 'bc']])
91 0x63.6261
94***
95### `++cat`
97Concatenate
99Concatenates two atoms, `b` and `c`, according to bloq size `a`, producing an
100atom.
102Accepts
103-------
105`a` is a block size (see `++bloq`).
107`b` is an atom.
109`c` is an atom.
111Produces
112--------
114An atom.
116Source
117------
119 ++ cat :: concatenate
120 ~/ %cat
121 |= {a/bloq b/@ c/@}
122 (add (lsh a (met a b) c) b)
125Examples
126--------
128 > `@ub`(cat 3 1 0)
129 0b1
130 > `@ub`(cat 0 1 1)
131 0b11
132 > `@ub`(cat 0 2 1)
133 0b110
134 > `@ub`(cat 2 1 1)
135 0b1.0001
136 > `@ub`256
137 0b1.0000.0000
138 > `@ub`255
139 0b1111.1111
140 > `@ub`(cat 3 256 255)
141 0b1111.1111.0000.0001.0000.0000
142 > `@ub`(cat 2 256 255)
143 0b1111.1111.0001.0000.0000
144 > (cat 3 256 255)
145 16.711.936
149***
150### `++cut`
152Slice
154Slices `c` blocks of size `a` that are `b` blocks from the end of `d`.
156Accepts
157-------
159`a` is a block size (see ++bloq).
161`b` is an atom.
163`c` is an atom.
165Produces
166--------
168An atom.
170Source
171------
173 ++ cut :: slice
174 ~/ %cut
175 |= {a/bloq {b/@u c/@u} d/@}
176 (end a c (rsh a b d))
178Examples
179--------
181 > (cut 0 [1 1] 2)
182 1
183 > (cut 0 [2 1] 4)
184 1
185 > `@t`(cut 3 [0 3] 'abcdefgh')
186 'abc'
187 > `@t`(cut 3 [1 3] 'abcdefgh')
188 'bcd'
189 > `@ub`(cut 0 [0 3] 0b1111.0000.1101)
190 0b101
191 > `@ub`(cut 0 [0 6] 0b1111.0000.1101)
192 0b1101
193 > `@ub`(cut 0 [4 6] 0b1111.0000.1101)
194 0b11.0000
195 > `@ub`(cut 0 [3 6] 0b1111.0000.1101)
196 0b10.0001
200***
201### `++end`
203Tail
205Produces an atom by taking the last `b` blocks of size `a` from `c`.
207Accepts
208-------
210`a` is a block size (see `++bloq`).
212`b` is an atom.
214`c` is an atom.
216Produces
217--------
219An atom.
222Source
223------
225 ++ end :: tail
226 ~/ %end
227 |= {a/bloq b/@u c/@}
228 (mod c (bex (mul (bex a) b)))
231Examples
232--------
234 > `@ub`12
235 0b1100
236 > `@ub`(end 0 3 12)
237 0b100
238 > (end 0 3 12)
239 4
240 > `@ub`(end 1 3 12)
241 0b1100
242 > (end 1 3 12)
243 12
244 > `@ux`'abc'
245 0x63.6261
246 > `@ux`(end 3 2 'abc')
247 0x6261
248 > `@t`(end 3 2 'abc')
249 'ab'
253***
254### `++fil`
256Fill bloqstream
258Produces an atom by repeating `c` for `b` blocks of size `a`.
260Accepts
261-------
263`a` is a block size (see `++bloq`).
265`b` is an atom.
267`c` is an atom.
269Produces
270--------
272An atom.
274Source
275------
277 ++ fil :: fill bloqstream
278 |= {a/bloq b/@u c/@}
279 =+ n=0
280 =+ d=c
281 |- ^- @
282 ?: =(n b)
283 (rsh a 1 d)
284 $(d (add c (lsh a 1 d)), n +(n))
287Examples
288--------
290 > `@t`(fil 3 5 %a)
291 'aaaaa'
292 > `@t`(fil 5 10 %ceeb)
293 'ceebceebceebceebceebceebceebceebceebceeb'
294 > `@t`(fil 4 10 %eced)
295 'ʇʇʇʇʇʇʇʇʇʇed'
296 > `@tas`(fil 4 10 %bf)
297 %bfbfbfbfbfbfbfbfbfbf
301***
302### `++lsh`
304Left-shift
306Produces an atom by left-shifting `c` by `b` blocks of size `a`.
308Accepts
309-------
311`a` is a block size (see `++bloq`).
313`b` is an atom.
315`c` is an atom.
317Produces
318--------
320An atom.
322Source
323------
325 ++ lsh :: left-shift
326 ~/ %lsh
327 |= {a/bloq b/@u c/@}
328 (mul (bex (mul (bex a) b)) c)
331Examples
332--------
334 > `@ub`1
335 0b1
336 > `@ub`(lsh 0 1 1)
337 0b10
338 > (lsh 0 1 1)
339 2
340 > `@ub`255
341 0b1111.1111
342 > `@ub`(lsh 3 1 255)
343 0b1111.1111.0000.0000
344 > (lsh 3 1 255)
345 65.280
349***
350### `++met`
352Measure
354Computes the number of blocks of size `a` in `b`, producing an atom.
356`a` is a block size (see `++bloq`).
358`b` is an atom.
360Source
361------
363 ++ met :: measure
364 ~/ %met
365 |= {a/bloq b/@}
366 ^- @
367 =+ c=0
368 |-
369 ?: =(0 b) c
370 $(b (rsh a 1 b), c +(c))
372Examples
373--------
375 > (met 0 1)
376 1
377 > (met 0 2)
378 2
379 > (met 3 255)
380 1
381 > (met 3 256)
382 2
383 > (met 3 'abcde')
384 5
388***
389### `++rap`
391Assemble non-zero
393Concatenates a list of atoms `b` using blocksize `a`, producing an atom.
395Accepts
396-------
398`a` is a block size (see `++bloq`).
400`b` is a `++list` of atoms.
402Produces
403--------
405An atom.
407Source
408------
410 ++ rap :: assemble nonzero
411 ~/ %rap
412 |= {a/bloq b/(list @)}
413 ^- @
414 ?~ b 0
415 (cat a i.b $(b t.b))
417Examples
418--------
420 > `@ub`(rap 2 (limo [1 2 3 4 ~]))
421 0b100.0011.0010.0001
422 > `@ub`(rap 1 (limo [1 2 3 4 ~]))
423 0b1.0011.1001
424 > (rap 0 (limo [0 0 0 ~]))
425 0
426 > (rap 0 (limo [1 0 1 ~]))
427 3
431***
432### `++rep`
434Assemble single
436Produces an atom by assembling a list of atoms `b` using block size `a`.
438Accepts
439-------
441`a` is a block size (see `++bloq`).
443`b` is a list of atoms.
445Produces
446--------
448An atom.
450Source
451------
453 ++ rep :: assemble single
454 ~/ %rep
455 |= {a/bloq b/(list @)}
456 ^- @
457 =+ c=0
458 |-
459 ?~ b 0
460 (con (lsh a c (end a 1 i.b)) $(c +(c), b t.b))
462Examples
463--------
465 > `@ub`(rep 2 (limo [1 2 3 4 ~]))
466 0b100.0011.0010.0001
467 > (rep 0 (limo [0 0 1 ~]))
468 4
469 > (rep 0 (limo [0 0 0 1 ~]))
470 8
471 > (rep 0 (limo [0 1 0 0 ~]))
472 2
473 > (rep 0 (limo [0 1 0 1 ~]))
474 10
475 > (rep 0 (limo [0 1 0 1 0 1 ~]))
476 42
477 > `@ub`42
478 0b10.1010
482***
483### `++rip`
485Disassemble
487Produces a list of atoms from the bits of `b` using block size `a`.
489Accepts
490-------
492`a` is a block size (see `++bloq`).
494`b` is an atom.
496Produces
497--------
499A list of atoms.
501Source
502------
504 ++ rip :: disassemble
505 ~/ %rip
506 |= {a/bloq b/@}
507 ^- (list @)
508 ?: =(0 b) ~
509 [(end a 1 b) $(b (rsh a 1 b))]
511Examples
512--------
514 > `@ub`155
515 0b1001.1011
516 > (rip 0 155)
517 ~[1 1 0 1 1 0 0 1]
518 > (rip 2 155)
519 ~[11 9]
520 > (rip 1 155)
521 ~[3 2 1 2]
522 > `@ub`256
523 0b1.0000.0000
524 > (rip 0 256)
525 ~[0 0 0 0 0 0 0 0 1]
526 > (rip 2 256)
527 ~[0 0 1]
528 > (rip 3 256)
529 ~[0 1]
533***
534### `++rsh`
536Right-shift
538Right-shifts `c` by `b` blocks of size `a`, producing an atom.
540Accepts
541-------
543`a` is a block size (see `++bloq`).
545`b` is an atom.
547`c` is an atom.
549Produces
550--------
552An atom.
554Source
555------
557 ++ rsh :: right-shift
558 ~/ %rsh
559 |= {a/bloq b/@u c/@}
560 (div c (bex (mul (bex a) b)))
562Examples
563--------
565 > `@ub`145
566 0b1001.0001
567 > `@ub`(rsh 1 1 145)
568 0b10.0100
569 > (rsh 1 1 145)
570 36
571 > `@ub`(rsh 2 1 145)
572 0b1001
573 > (rsh 2 1 145)
574 9
575 > `@ub`10
576 0b1010
577 > `@ub`(rsh 0 1 10)
578 0b101
579 > (rsh 0 1 10)
580 5
581 > `@ux`'abc'
582 0x63.6261
583 > `@t`(rsh 3 1 'abc')
584 'bc'
585 > `@ux`(rsh 3 1 'abc')
586 0x6362
590***
591### `++swp`
593Reverse block order
595Switches little endian to big and vice versa: produces an atom by
596reversing the block order of `b` using block size `a`.
599Accepts
600-------
602`a` is a block size (see `++bloq`).
604`b` is an atom.
606Produces
607--------
609An atom
611Source
612------
614 ++ swap |=({a/bloq b/@} (rep a (flop (rip a b)))) :: reverse bloq order
616Examples
617--------
619 > `@ub`24
620 0b1.1000
621 > (swap 0 24)
622 3
623 > `@ub`3
624 0b11
625 > (swap 0 0)
626 0
627 > (swap 0 128)
628 1
632***
633### `++xeb`
635Binary logarithm
637Computes the base-2 logarithm of `a`, producing an atom.
639Accepts
640-------
642`a` is an atom.
644Produces
645--------
647An atom.
649Source
650------
652 ++ xeb :: binary logarithm
653 ~/ %xeb
654 |= a/@
655 ^- @
656 (met 0 a)
658Examples
659--------
661 > (xeb 31)
662 5
663 > (xeb 32)
664 6
665 > (xeb 49)
666 6
667 > (xeb 0)
668 0
669 > (xeb 1)
670 1
671 > (xeb 2)
672 2
676***