b. (Boolean / Basic not covered yet) Adverb
To use the Boolean adverb b. , put a small integer m to the left of b. . This results in a Boolean verb. The choice of m determines which Boolean function you'll get. Do you need to look up the big operand table each time you want a Boolean function? No. Write the output you want for the function in binary as m. The next sentence defines the arguments. Thus if I want to make the verb B so that 1 1 0 1 is same as ( -: ) the sentence 0 0 1 1 B 0 1 0 1 (implication), I can define B=: 2b1101 b. . Note that these verbs will cause domain error if given non-Boolean arguments. Add 16 to m for a verb that operates bit-wise. All that's left is your preference for expression of the operand. Rotate, shift, and signed shift are also available. You may have to look up these. All of the Boolean functions have verbs already defining them for Iverson Boolean arguments (zeros and ones). Some of these have additional meaning in an extended domain, for instance the implementation of logical not ( -. y ) is 1-y and therefor has extended utility.
This example defines bit-wise not. Not is a monad. The monadic verb created from m b. is the same as 0&(m b.) . We need the sentence 0 0 1 1 not 0 1 0 1 to be equivalent to a vector that starts with 1 0 . Since 0 is Curried to the dyadic verb to create the monadic verb, all 4 choices of m as 2b1000+i.4 will work.
not_bitwise=: (#.1 1 0 0 0)b. NB. many ways to express the operand m not_bitwise=: (16 + 2b1000)b. NB. A lesson for another chapter. not_bitwise=: (16b18 b.) : [: NB. Ensure monadic use. not_bitwise i.4 _1 _2 _3 _4
Insert verb between items (at rank 1)
#:i.4 NB. binary representation of 0 1 2 3
0 0
0 1
1 0
1 1
verb=: ([,' V ',])&": NB. verb produces a character array
'hi' verb 'world'
hi V world
0 verb 23
0 V 23
verb/"1 #:i.4 NB. insert verb between items at rank 1
0 V 0
0 V 1
1 V 0
1 V 1
NB. compute the 16 dyadic Boolean operations
truths=: (; (i.16)b./"1) #:i.4
NB. decorate these truths.
|."1('b.';1j1#'0123456789abcdef'),:truths
┌────────────────────────────────┬───┐
│0 1 2 3 4 5 6 7 8 9 a b c d e f │b. │
├────────────────────────────────┼───┤
│0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 │0 0│
│0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 │0 1│
│0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 │1 0│
│0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 │1 1│
└────────────────────────────────┴───┘
Common uses
In this electrical engineering example we are to build eight 2 input xor circuits. There's no space left on the circuit board and all the exclusive or chips have been used. We have plenty of unused and and not gates. How do we connect these? The artifact verb byte limits the output to 8 bits. We use this because J uses the native word size for bit-wise Boolean operations. Wikipedia diagrams the circuit: http://en.wikipedia.org/wiki/Xor_gate One of the nand circuits is repeated in the solution below. Can you spot the duplicate pair?
byte=: 16bff&and NB. byte constrains the output to 8 bits. xor=: L byte@nand R L=: [ nand nand R=: ] nand nand nand=: [: not and and=: (16+2b0001) b. ([: :) NB. and is a fundamental circuit. Restrict and to dyadic use. NB. Oops! The token (: (parenthesis with suffix) is not yet defined in j. not=: (16+2b1000) b. (:[:) NB. not is the other fundamental circuit. Try to restrict as monad. |spelling error | not=: (16+2b1000) b. (: [:) | ^ not=: (16+2b1000) b. ( : [: ) NB. This is a correct way to restrict not to monadic use. NB. tests built_in_xor=: byte@((16+2b0110)b.) NB. truth table of xor is 0 1 1 0 NB. Insert the xors between items of a list of random integers. NB. They agree. (built_in_xor/ -: xor/) 30 ? 332442 1 NB. Visual inspections show that the common verb byte works correctly. 16 #. 4 6 NB. Written in base 16, find '46' in base 10. 70 byte 16babcd46 16bab10 NB. demonstrate byte 70 16 (i.8) xor (33+i.8) NB. look at xor output 33 35 33 39 33 35 33 47 (1234+i.8) xor (1234+33+i.8) NB. tests byte@xor 33 39 33 35 33 47 33 35
See Also
Entry in the J Dictionary for b.
