Bitwise operations: Difference between revisions

Content added Content deleted
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
(Added ZX81 BASIC)
Line 457: Line 457:
PRINT "a SHR b (logical) = "; u SHR b
PRINT "a SHR b (logical) = "; u SHR b
END SUB</lang>
END SUB</lang>

==={{header|Sinclair ZX81 BASIC}}===
ZX81 BASIC has no integer type (a major lacuna) and consequently no bitwise operations; but the CPU has them, so we can use a tiny machine code routine to do the actual work and then return to BASIC to print the answers.

This program is a proof of concept, really, and will only work with 8-bit values. In addition, with 1k of RAM there is only space for the first of the shifts/rotates; the others could be implemented along exactly the same lines.

The disassembly of the Z80 code would be:
<lang z80asm> org 4084
3a 83 40 ld a, (4083)
47 ld b, a
3a 82 40 ld a, (4082)
a0 and b
00 nop ; negate and shift instructions take 2 bytes
06 00 ld b, 0
4f ld c, a ; value in BC reg pair is returned to BASIC
c9 ret</lang>
We then use <code>POKE</code> statements to replace the <code>and</code> instruction with each successive operation we want to perform.

Note that the left shift instruction shifts by one bit at a time, so we need a loop. The present program has the loop written in BASIC, because it seemed sensible to use BASIC for anything we could use it for and only drop into machine code when there was no alternative; it would of course be faster to do the whole thing in machine code.

Finally, observe that the first line reserves 15 bytes for our machine code routine by hiding them in a comment.

<lang basic> 10 REM ABCDEFGHIJKLMNO
20 INPUT A
30 INPUT B
40 POKE 16514,A
50 POKE 16515,B
60 LET ADDR=16516
70 LET R$="3A8340473A8240A00006004FC9"
80 POKE ADDR,CODE R$*16+CODE R$(2)-476
90 LET R$=R$(3 TO )
100 LET ADDR=ADDR+1
110 IF R$<>"" THEN GOTO 80
120 PRINT A;" AND ";B;" = ";USR 16516
130 POKE 16523,176
140 PRINT A;" OR ";B;" = ";USR 16516
150 POKE 16523,168
160 PRINT A;" XOR ";B;" = ";USR 16516
170 POKE 16523,237
180 POKE 16524,68
190 PRINT "NOT ";A;" = ";USR 16516
200 POKE 16523,203
210 POKE 16524,39
220 FOR I=1 TO B
230 POKE 16514,USR 16516
240 NEXT I
250 PRINT A;" << ";B;" = ";PEEK 16514</lang>
{{in}}
<pre>21
3</pre>
{{out}}
<pre>21 AND 3 = 1
21 OR 3 = 23
21 XOR 3 = 22
NOT 21 = 235
21 << 3 = 168</pre>


=={{header|BASIC256}}==
=={{header|BASIC256}}==