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 |
Actual calculations occur in<tt> bwo</tt> and <tt> brs</tt>. Other definitions provide formatting. |
||
bwo =: 17 b. , 23 b. , 22 b. , 28 b. |
|||
brs =: 33 b.~ , (33 b.~ -), (34 b.~ -), 32 b.~ , (32 b.~ -) |
|||
boolf =: title"_ combine bwo, brs |
|||
combine =: ,. ":@|:@,: |
|||
t =. >;._2 'Bitwise x AND y: |Bitwise x OR y:|Bitwise x XOR y:|Bitwise NOT-x:|' |
|||
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'"_ |
|||
Examples of execution, with output: |
Examples of execution, with output: |
||
255 bwo 5 NB. numeric output of bitwise operations |
|||
5 255 250 _256 |
|||
1 _251 _252 254 _8160 134217720 _8 _8129 268435448 |
|||
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 |
Bitwise x AND y: 5 |
||
Bitwise |
Bitwise x OR y: 255 |
||
Bitwise x XOR y: 250 |
|||
Bitwise NOT-x: _256 |
|||
Left shift: 8160 |
|||
Right shift: 7 |
|||
Right |
Right signed shift: 7 |
||
Left rotate: 8160 |
|||
Right rotate: _134217721 |
|||
f _255 (bwo, brs) 5 NB. 32-bit diagrams |
|||
...............................x |
...............................x |
||
xxxxxxxxxxxxxxxxxxxxxxxx.....x.x |
xxxxxxxxxxxxxxxxxxxxxxxx.....x.x |