on master help sign in
Update 2d
f52e641rmariani
docs/hoon/library/2d.md+169−48
@@ -8,27 +8,23 @@ navhome: /docs/
8
8
9Binary OR
9Binary OR
10
10
11−Computes the bit-wise logical OR of two [atom]()s, `a` and `b`, producing an
11+Computes the bitwise logical OR of two atoms, `a` and `b`, producing an atom.
12−atom.
13
12
14−Accepts
13+#### Accepts
15−-------
16
14
17`a` is an atom
15`a` is an atom
18
16
19`b` is an atom
17`b` is an atom
20
18
21−Produces
19+#### Produces
22−--------
23
20
24An atom.
21An atom.
25
22
26−Source
23+#### Source
27−------
28
24
29− ++ con :: binary or
25+ ++ con
30 ~/ %con
26 ~/ %con
31− |= {a/@ b/@}
27+ |= [a=@ b=@]
32 =+ [c=0 d=0]
28 =+ [c=0 d=0]
33 |- ^- @
29 |- ^- @
34 ?: ?&(=(0 a) =(0 b)) d
30 ?: ?&(=(0 a) =(0 b)) d
@@ -43,22 +39,58 @@ Source
43 ==
39 ==
44 ==
40 ==
45
41
46−Examples
42+#### Examples
47−--------
48
43
49 > (con 0b0 0b1)
44 > (con 0b0 0b1)
50 1
45 1
46+
51 > (con 0 1)
47 > (con 0 1)
52 1
48 1
49+
53 > (con 0 0)
50 > (con 0 0)
54 0
51 0
52+
55 > `@ub`(con 0b1111.0000 0b1.0011)
53 > `@ub`(con 0b1111.0000 0b1.0011)
56 0b1111.0011
54 0b1111.0011
55+
57 > (con 4 4)
56 > (con 4 4)
58 4
57 4
58+
59 > (con 10.000 234)
59 > (con 10.000 234)
60 10.234
60 10.234
61
61
62+ > `@ub`534
63+ 0b10.0001.0110
64+
65+ > `@ub`987
66+ 0b11.1101.1011
67+
68+ > `@ub`(con 534 987)
69+ 0b11.1101.1111
70+
71+ > (con 534 987)
72+ 991
73+
74+
75+#### Discussion
76+
77+`con` performs the bitwise operation OR, a concept that's general to computing.
78+It compares each bit of its first sample to the corresponding bit of its second
79+sample. If either bit is 1, the corresponding product bit is set
80+to 1. Otherwise, the corresponding product bit is set to 0.
81+
82+Take the example of `(con 8 12)`. It's easy to see how this operation works when
83+ its samples and its product are stacked.
84+
85+ 0b1000 :: 8 (sample)
86+ 0b1100 :: 12 (sample)
87+ 0b1100 :: 12 (product)
88+
89+Note that the names `con` (conjunction) for OR and `dis` (disjunction) for AND
90+are given to opposite operators in Hoon when compared to other computing
91+contexts. That's because `0` is true in Hoon and `1` is false. Outside of Hoon,
92+where `0` is false and `1` is true, bitwise OR is the logical disjunction and
93+bitwise AND is the logical conjunction.
62
94
63
95
64***
96***
@@ -66,28 +98,24 @@ Examples
66
98
67Binary AND
99Binary AND
68
100
69−Computes the bit-wise logical AND of two atoms `a` and `b`, producing an
101+Computes the bitwise logical AND of two atoms, `a` and `b`, producing an atom.
70−atom.
71
102
72−Accepts
103+#### Accepts
73−-------
74
104
75`a` is an atom.
105`a` is an atom.
76
106
77`b` is an atom.
107`b` is an atom.
78
108
79−Produces
109+#### Produces
80−--------
81
110
82An atom.
111An atom.
83
112
84−Source
113+#### Source
85−------
86
114
87− ++ dis :: binary and
115+ ++ dis
88 ~/ %dis
116 ~/ %dis
89− |= {a/@ b/@}
117+ |= [a=@ b=@]
90− =| {c/@ d/@}
118+ =| [c=@ d=@]
91 |- ^- @
119 |- ^- @
92 ?: ?|(=(0 a) =(0 b)) d
120 ?: ?|(=(0 a) =(0 b)) d
93 %= $
121 %= $
@@ -101,53 +129,75 @@ Source
101 ==
129 ==
102 ==
130 ==
103
131
104−Examples
132+#### Examples
105−--------
106
133
107 > `@ub`9
134 > `@ub`9
108 0b1001
135 0b1001
136+
109 > `@ub`5
137 > `@ub`5
110 0b101
138 0b101
139+
111 > `@ub`(dis 9 5)
140 > `@ub`(dis 9 5)
112 0b1
141 0b1
142+
113 > (dis 9 5)
143 > (dis 9 5)
114 1
144 1
145+
115 > `@ub`534
146 > `@ub`534
116 0b10.0001.0110
147 0b10.0001.0110
148+
117 > `@ub`987
149 > `@ub`987
118 0b11.1101.1011
150 0b11.1101.1011
151+
119 > `@ub`(dis 534 987)
152 > `@ub`(dis 534 987)
120 0b10.0001.0010
153 0b10.0001.0010
154+
121 > (dis 534 987)
155 > (dis 534 987)
122 530
156 530
123
157
124
158
159+#### Discussion
160+
161+`dis` performs the bitwise AND, an operation general to computing. It compares
162+each bit of its first sample to the corresponding bit of its second sample. If
163+both bits are 1, the corresponding product bit is set to 1. Otherwise, the
164+corresponding product bit is set to 0.
165+
166+Take the example of `(dis 8 12)`. It's easy to see how this operation works when
167+its samples and its product are stacked.
168+
169+ 0b1000 :: 8 (sample)
170+ 0b1100 :: 12 (sample)
171+ 0b1000 :: 8 (product)
172+
173+Note that the names `dis` (disjunction) for AND and `con` (conjuction) for OR
174+are given to opposite operators in Hoon when compared to other computing
175+contexts. That's because `0` is true in Hoon and `1` is false. Outside of Hoon,
176+where `0` is false and `1` is true, bitwise OR is the logical disjunction and
177+bitwise AND is the logical conjunction.
125
178
126***
179***
127### `++mix`
180### `++mix`
128
181
129Binary XOR
182Binary XOR
130
183
131−Produces the bit-wise logical XOR of `a` and `b`, producing an atom.
184+Produces the bitwise logical XOR of two atoms, `a` and `b`, producing an atom.
132
185
133−Accepts
186+#### Accepts
134−-------
135
187
136`a` is an atom
188`a` is an atom
137
189
138`b` is an atom
190`b` is an atom
139
191
140−Produces
192+#### Produces
141−--------
142
193
143An atom.
194An atom.
144
195
145−Source
196+#### Source
146−------
147
197
148− ++ mix :: binary xor
198+ ++ mix
149 ~/ %mix
199 ~/ %mix
150− |= {a/@ b/@}
200+ |= [a=@ b=@]
151 ^- @
201 ^- @
152 =+ [c=0 d=0]
202 =+ [c=0 d=0]
153 |-
203 |-
@@ -159,69 +209,140 @@ Source
159 d (add d (lsh 0 c =((end 0 1 a) (end 0 1 b))))
209 d (add d (lsh 0 c =((end 0 1 a) (end 0 1 b))))
160 ==
210 ==
161
211
162−Examples
212+#### Examples
163−--------
164
213
165 > `@ub`2
214 > `@ub`2
166 0b10
215 0b10
216+
167 > `@ub`3
217 > `@ub`3
168 0b11
218 0b11
219+
169 > `@ub`(mix 2 3)
220 > `@ub`(mix 2 3)
170 0b1
221 0b1
222+
171 > (mix 2 3)
223 > (mix 2 3)
172 1
224 1
225+
173 > `@ub`(mix 2 2)
226 > `@ub`(mix 2 2)
174 0b0
227 0b0
228+
175 > (mix 2 2)
229 > (mix 2 2)
176 0
230 0
177
231
232+ > `@ub`534
233+ 0b10.0001.0110
234+
235+ > `@ub`987
236+ 0b11.1101.1011
237+
238+ > `@ub`(mix 534 987)
239+ 0b1.1100.1101
178
240
241+ > (mix 534 987)
242+ 461
243+
244+#### Discussion
245+
246+`mix` performs the bitwise XOR (exclusive-OR), an operation that's general to
247+computing. compares each bit of its first sample to the corresponding bit of its
248+second sample. If one bit is 0 and the other bit is 1, the corresponding product
249+bit is set to 1. Otherwise, the corresponding product bit is set to 0.
250+
251+Take the example of `(mix 8 12)`. It's easy to see how this operation works when
252+its samples and its product are stacked.
253+
254+ 0b1000 :: 8 (sample)
255+ 0b1100 :: 12 (sample)
256+ 0b100 :: 4 (product)
179
257
180***
258***
181### `++not`
259### `++not`
182
260
183Binary NOT
261Binary NOT
184
262
185−Computes the bit-wise logical NOT of the bottom `b` blocks of size `a`
263+Computes the bitwise logical NOT of the bottom `b` blocks of size `a`
186of `c`.
264of `c`.
187
265
188−Accepts
266+#### Accepts
189−-------
190
267
191−`a` is a block size (see `++bloq`).
268+`a` is a block size (see [`bloq`](../1c)).
192
269
193`b` is an atom.
270`b` is an atom.
194
271
195`c` is an atom.
272`c` is an atom.
196
273
197−Produces
274+#### Produces
198−--------
199
275
200−An atom.
276+An atom.
201
277
202−Source
278+#### Source
203−------
204
279
205− ++ not |= {a/bloq b/@ c/@} :: binary not (sized)
280+ ++ not |= [a=bloq b=@ c=@]
206 (mix c (dec (bex (mul b (bex a)))))
281 (mix c (dec (bex (mul b (bex a)))))
207
282
208
283
209−Examples
284+#### Examples
210−--------
211
285
212 > `@ub`24
286 > `@ub`24
213 0b1.1000
287 0b1.1000
288+
214 > (not 0 5 24)
289 > (not 0 5 24)
215 7
290 7
291+
216 > `@ub`7
292 > `@ub`7
217 0b111
293 0b111
294+
218 > (not 2 5 24)
295 > (not 2 5 24)
219 1.048.551
296 1.048.551
297+
220 > (not 2 5 1.048.551)
298 > (not 2 5 1.048.551)
221 24
299 24
300+
222 > (not 1 1 (not 1 1 10))
301 > (not 1 1 (not 1 1 10))
223 10
302 10
224
303
304+#### Discussion
305+
306+In computing in general, the bitwise operation NOT simply turns a binary
307+number's `0`s into `1`s, and vice versa.
308+
309+In Hoon, we ask for a little more information to use `not`, because binary
310+numbers have a number of implicit leading zeroes differently depending on on
311+their [block size](../1c). Decimal `7`, for example, is `0b111` in binary, and
312+has one implicit zero in the context of a block of size `2`, which has a
313+bitwidth of 4. Let's try `not` on `7` with a single block of size `2`.
314+
315+ > `@ub`(not 2 1 0b111)
316+ 0b1000
317+
318+ > `@u`0b1000
319+ 8
320+
321+This happened because `0b111` is considered as `0b0111` by the `not` operator
322+when dealing with a single block of size `2`. The NOT of `7`, then, is `0b1000`,
323+ or `8` in decimal.
324+
325+When we pass `not` a single block of size `3`, there is a bitwidth of 8 to
326+fill with binary information. So the remaining leading digits of `0b111` are,
327+again, treated as `0`.
328+
329+ > `@ub`(not 3 1 0b111)
330+ 0b1111.1000
331+
332+ > `@u`0b1111.1000
333+ 248
334+
335+This works when going to a smaller block size, too.
336+
337+ > `@ub`(not 1 1 0b1011)
338+ 0b100
339+
340+ > `@u`0b100
341+ 4
342+
343+What's happening here may not be readily apparent. But we're only flipping the
344+last block of size `1` (bitwidth 2) of the binary `0b111`. That is, we leave
345+the "0b1" piece just the same and manipulate the "11" that the number ends with.
225
346
226
347
227***
348***