#
Bitwise NON EQUIVALENCE operator.
The bitwise operators perform operations of bitwise AND, bitwise OR, bitwise NOT and bitwise non-equivalence.
The priority of these operators is less binding that the arithmetic ones, but more binding that the comparisons.
Parentheses may be used as above to force particular groupings.
Syntax
a # b ( bitwise NON EQUIVALENCE )
where the arguments \(a\) and \(b\) may be further expressions. They are converted to the nearest integer to machine precision and then the operation is performed.
Examples
Modern Classic
Here is an example for the bitwise-group, comparing the different bitwise opcodes. It uses the file bitwise-group-modern.csd .
Example of the bitwise group. <CsoundSynthesizer>
<CsOptions>
--nosound
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
; By Daniele Cucchi - 2020
;a & b: bitwise AND
;a | b: bitwise OR
;a # b: bitwise NON EQUIVALENCE - XOR
; ~ a: bitwise NOT
instr 1
ArrayA : k [] = fillarray ( 0 , 0 , 1 , 1 ) ; Fill array "A" with 4 values: 0, 0, 1, 1
i 0A = i ( ArrayA , 0 )
i 1A = i ( ArrayA , 1 )
i 2A = i ( ArrayA , 2 )
i 3A = i ( ArrayA , 3 )
ArrayB : k [] = fillarray ( 0 , 1 , 0 , 1 ) ; Fill array "B" with 4 values: 0, 1, 0, 1
i 0B = i ( ArrayB , 0 )
i 1B = i ( ArrayB , 1 )
i 2B = i ( ArrayB , 2 )
i 3B = i ( ArrayB , 3 )
; Bitwise operations & fill arrays
AND : k [] = ArrayA & ArrayB
OR : k [] = ArrayA | ArrayB
NON : k [] = fillarray ( i 0B # i 0A , i 1B # i 1A , i 2B # i 2A , i 3B # i 3A )
NOT : k [] = fillarray ( ~ i 0A & 1 , ~ i 1A & 1 , ~ i 2A & 1 , ~ i 3A & 1 )
; Print values
printarray ( AND , " %d " , "= bitwise AND" )
printarray ( OR , " %d " , "= bitwise OR" )
printarray ( NON , " %d " , "= bitwise NON" )
printarray ( NOT , " %d " , "= bitwise NOT" )
endin
</CsInstruments>
<CsScore>
i 1 0 0.001
e
</CsScore>
</CsoundSynthesizer>
Here is an example for the bitwise-group, comparing the different bitwise opcodes. It uses the file bitwise-group.csd .
Example of the bitwise group. <CsoundSynthesizer>
<CsOptions>
--nosound
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
; By Daniele Cucchi - 2020
;a & b: bitwise AND
;a | b: bitwise OR
;a # b: bitwise NON EQUIVALENCE - XOR
; ~ a: bitwise NOT
instr 1
k ArrayA [] fillarray 0 , 0 , 1 , 1 ; Fill array "A" with 4 values: 0, 0, 1, 1
i 0A = i ( k ArrayA , 0 )
i 1A = i ( k ArrayA , 1 )
i 2A = i ( k ArrayA , 2 )
i 3A = i ( k ArrayA , 3 )
k ArrayB [] fillarray 0 , 1 , 0 , 1 ; Fill array "B" with 4 values: 0, 1, 0, 1
i 0B = i ( k ArrayB , 0 )
i 1B = i ( k ArrayB , 1 )
i 2B = i ( k ArrayB , 2 )
i 3B = i ( k ArrayB , 3 )
; Bitwise operations & fill arrays
k AND [] = k ArrayA & k ArrayB
k OR [] = k ArrayA | k ArrayB
k NON [] fillarray i 0B # i 0A , i 1B # i 1A , i 2B # i 2A , i 3B # i 3A
k NOT [] fillarray ~ i 0A , ~ i 1A , ~ i 2A , ~ i 3A
; Print values
printarray k AND , " %d " , "= bitwise AND"
printarray k OR , " %d " , "= bitwise OR"
printarray k NON , " %d " , "= bitwise NON"
printarray k NOT , " %d " , "= bitwise NOT \n "
endin
</CsInstruments>
<CsScore>
i 1 0 1
e
</CsScore>
</CsoundSynthesizer>
See also
Arithmetic and Logic Operations