Bitwise operations: Difference between revisions

→‎{{header|J}}: Segregated bitwise and rotation/shift functions. Simplified verb 'combine'.
(→‎{{header|J}}: Segregated bitwise and rotation/shift functions. Simplified verb 'combine'.)
Line 215:
 
=={{header|J}}==
Actual bitwisecalculations calculation occursoccur 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.~ -)
brs =: 33 b.~ , (33 b.~ -), (34 b.~ -), 32 b.~ , (32 b.~ -)
bwotxt boolf =: title"_ combine bwo, brs
combine =: [ ,. ":@|:@,:@]
t =. >;._2 'Bitwise x AND y: |Bitwise x OR y:|Bitwise x XOR y:|Bitwise NOT-x:|'
title=: t, =. >;._2 'LeftBitwise shift:|Rightx shiftAND y: |RightBitwise signedx shiftOR y:|LeftBitwise x XOR rotatey:|RightBitwise rotateNOT-x:|'
title=: t,>;._2'Left shift:|Right shift:|Right signed shift:|Left rotate:|Right rotate:|'
f=: (32#2)&#: { '.x'"_
Examples of execution, with output:
_255255 bwo 5 NB. numeric output of bitwise operations
5 255 250 _256
1 _251 _252 254 _8160 134217720 _8 _8129 268435448
_255255 bwotxtbrs 5 NB. formattednumeric output of boolean textrotation withand Englishshift titlingoperations
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 XORAND y: _252 5
Bitwise NOT-x OR y: 254255
LeftBitwise shift:x XOR y: _8160250
RightBitwise shiftNOT-x: 134217720 _256
Right signedLeft shift: _8 8160
LeftRight rotateshift: _8129 7
Right rotatesigned shift: 268435448 7
Left rotate: f _255 bwo 5 NB. 32-bit diagram8160
Right rotate: _134217721
f _255 (bwo, brs) 5 NB. 32-bit diagrams
...............................x
xxxxxxxxxxxxxxxxxxxxxxxx.....x.x
Anonymous user