source: project/wiki/eggref/5/bitwise-utils @ 38589

Last change on this file since 38589 was 38589, checked in by Kon Lovett, 7 months ago

rel 1.0.0

File size: 2.4 KB
Line 
1[[tags: egg]]
2
3== bitwise-utils
4
5[[toc:]]
6
7
8== Documentation
9
10Additional bitwise routines.
11
12=== bitwise-mask
13
14<procedure>(bitwise-mask WID [ON?]) --> integer</procedure>
15
16Returns {{WID}} low-order bits {{ON?}}.
17
18; {{WID}} : {{fixnum}} ; mask bit width.
19; {{ON?]}} : {{boolean}} ; {{#t}} is {{#b1}}, {{#f}} is {{#b0}}; default {{#t}}.
20
21<enscript language=scheme>
22(bitwise-mask 5 #t) ;=> +0...011111 (#b+011111)
23(bitwise-mask 5 #f) ;=> -1...100000 (#b-100000)
24</enscript>
25
26=== bitwise-join
27
28<procedure>(bitwise-join N0 [N1 ...]) --> integer</procedure>
29
30Returns adjoined bits.
31
32; {{N0}} : {{integer}} ; seed.
33; {{N1 ...}} : {{integer ...}} ; adjoins seed left-to-right.
34
35<enscript language=scheme>
36(bitwise-join #b10 #b0000001 #b101) ;=> #b101101
37</enscript>
38
39=== bitwise-split
40
41<procedure>(bitwise-split N WID) --> (list-of integer)</procedure>
42
43Returns {{N}} disjoined every {{WID}} bits.
44
45; {{N}} : {{integer}} ; seed.
46; {{WID}} : {{fixnum}} ; bit width.
47
48<enscript language=scheme>
49(bitwise-split #xabcdef 4) ;=> (#xa #xb #xc #xd #xe #xf)
50(bitwise-split #x-abcdef 4) ;=> (#x-a #x-b #x-c #x-d #x-e #x-f)
51</enscript>
52
53=== bitwise-count
54
55<procedure>(bitwise-count N) --> fixnum</procedure>
56
57Returns number of set bits in {{N}}.
58
59; {{N}} : {{integer}}
60
61=== logical-shift-left
62
63<procedure>(logical-shift-left N1 N2) --> integer</procedure>
64
65Returns left shift of {{N1}} by {{N2}} bits.
66
67; {{N1}} : {{integer}} ; what to shift.
68; {{N2}} : {{fixnum}} ; amount to shift.
69
70Aliases {{arithmetic-shift}}.
71
72=== logical-shift-right
73
74<procedure>(logical-shift-right N1 N2) --> integer</procedure>
75
76Returns right shift of {{N1}} by {{N2}} bits. Sign preserving.
77
78; {{N1}} : {{integer}} ; what to shift.
79; {{N2}} : {{fixnum}} ; amount to shift.
80
81=== arithmetic-shift-left
82
83<procedure>(arithmetic-shift-left N1 N2) --> integer</procedure>
84
85Returns left shift of {{N1}} by {{N2}} bits.
86
87; {{N1}} : {{integer}} ; what to shift.
88; {{N2}} : {{fixnum}} ; amount to shift.
89
90Aliases {{arithmetic-shift}}.
91
92=== arithmetic-shift-right
93
94<procedure>(arithmetic-shift-right N1 N2) --> integer</procedure>
95
96Returns right shift of {{N1}} by {{N2}} bits. Sign extending.
97
98; {{N1}} : {{integer}} ; what to shift.
99; {{N2}} : {{fixnum}} ; amount to shift.
100
101Essentially {{(arithmetic-shift <> (- <>))}}.
102
103
104=== Usage
105
106<enscript language=scheme>
107(import bitwise-utils)
108</enscript>
109
110
111== Author
112
113[[/users/kon-lovett|Kon Lovett]]
114
115
116== Version history
117
118; 1.0.0 : Release.
119
120
121== License
122
123This code is in the public domain.
Note: See TracBrowser for help on using the repository browser.