Bitwise operations: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: Segregated bitwise and rotation/shift functions. Simplified verb 'combine'.)
Line 215: Line 215:


=={{header|J}}==
=={{header|J}}==
Actual bitwise calculation occurs in<tt> bwo</tt>. Other definitions provide formatting.
Actual calculations occur in<tt> bwo</tt> and <tt> brs</tt>. Other definitions provide formatting.
bwo =: 17 b., 23 b., 22 b., 28 b., 33 b.~, (33 b.~ -), (34 b.~ -), 32 b.~, (32 b.~ -)
bwo =: 17 b. , 23 b. , 22 b. , 28 b.
brs =: 33 b.~ , (33 b.~ -), (34 b.~ -), 32 b.~ , (32 b.~ -)
bwotxt =: title"_ combine bwo
boolf =: title"_ combine bwo, brs
combine =: [ ,. ":@|:@,:@]
combine =: ,. ":@|:@,:
t =. >;._2 'Bitwise x AND y: |Bitwise x OR y:|Bitwise x XOR y:|Bitwise NOT-x:|'
title=: t,>;._2'Left shift:|Right shift:|Right signed shift:|Left rotate:|Right rotate:|'
t =. >;._2 'Bitwise x AND y: |Bitwise x OR y:|Bitwise x XOR y:|Bitwise NOT-x:|'
title=: t,>;._2'Left shift:|Right shift:|Right signed shift:|Left rotate:|Right rotate:|'
f=: (32#2)&#: { '.x'"_
f=: (32#2)&#: { '.x'"_
Examples of execution, with output:
Examples of execution, with output:
_255 bwo 5 NB. numeric output
255 bwo 5 NB. numeric output of bitwise operations
5 255 250 _256
1 _251 _252 254 _8160 134217720 _8 _8129 268435448
_255 bwotxt 5 NB. formatted text with English titling
255 brs 5 NB. numeric output of boolean rotation and shift operations
8160 7 7 8160 _134217721
Bitwise x AND y: 1
255 boolf 5 NB. formatted text with English titling
Bitwise x OR y: _251
Bitwise x XOR y: _252
Bitwise x AND y: 5
Bitwise NOT-x: 254
Bitwise x OR y: 255
Left shift: _8160
Bitwise x XOR y: 250
Right shift: 134217720
Bitwise NOT-x: _256
Right signed shift: _8
Left shift: 8160
Left rotate: _8129
Right shift: 7
Right rotate: 268435448
Right signed shift: 7
f _255 bwo 5 NB. 32-bit diagram
Left rotate: 8160
Right rotate: _134217721
f _255 (bwo, brs) 5 NB. 32-bit diagrams
...............................x
...............................x
xxxxxxxxxxxxxxxxxxxxxxxx.....x.x
xxxxxxxxxxxxxxxxxxxxxxxx.....x.x