|
Bitwise OR operator.
The bitwise operators perform operations of bitwise AND, bitwise OR, bitwise NOT and bitwise non-equivalence.
Syntax
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.
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.
Examples
Modern Classic Modern Classic
Here is an example of the bitwise AND and OR operators. It uses the file bitwise-modern.csd .
Example of the bitwise operators. <CsoundSynthesizer>
<CsOptions>
-n
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
instr 1
resultOr : i = p4 | p5
resultAnd : i = p4 & p5
prints ( " %i | %i = %i \\ n" , p4 , p5 , resultOr )
prints ( " %i & %i = %i \\ n" , p4 , p5 , resultAnd )
endin
instr 2 ; decimal to binary converter
binary : S = ""
numbits : i = 8
Count : i = init ( numbits - 1 )
while Count >= 0 do
value : i = 2 ^ Count
if ( p4 & value ) == value then
digit : S = "1"
else
digit = "0"
endif
binary : S = strcat ( binary , digit )
Count -= 1
od
text : S = sprintf ( " %i is %s in binary \\ n" , p4 , binary )
prints ( text )
endin
</CsInstruments>
<CsScore>
i 1 0 0.1 1 2
i 1 + . 1 3
i 1 + . 2 4
i 1 + . 3 10
i 2 2 0.1 12
i 2 + . 9
i 2 + . 15
i 2 + . 49
e
</CsScore>
</CsoundSynthesizer>
Here is an example of the bitwise AND and OR operators. It uses the file bitwise.csd .
Example of the bitwise operators. <CsoundSynthesizer>
<CsOptions>
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
instr 1
i resultOr = p4 | p5
i resultAnd = p4 & p5
prints " %i | %i = %i \\n " , p4 , p5 , i resultOr
prints " %i & %i = %i \\n " , p4 , p5 , i resultAnd
endin
instr 2 ; decimal to binary converter
S binary = ""
i numbits = 8
i count init i numbits - 1
pass :
i value = 2 ^ i count
if (( p4 & i value ) >= i value ) then
S digit = "1"
else
S digit = "0"
endif
S binary strcat S binary , S digit
loop_ge i count , 1 , 0 , pass
S text sprintf " %i is %s in binary \\ n" , p4 , S binary
prints S text
endin
</CsInstruments>
<CsScore>
i 1 0 0.1 1 2
i 1 + . 1 3
i 1 + . 2 4
i 1 + . 3 10
i 2 2 0.1 12
i 2 + . 9
i 2 + . 15
i 2 + . 49
e
</CsScore>
</CsoundSynthesizer>
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