- Joined
- Jan 2, 2020
- Messages
- 1,414
Here's an example of how to implement an Exclusive OR (XOR) , using only AND, OR, and NOT :
Complete working example :
Cerberus:
Function XOR:Int(a:Int,b:Int)
Return (a | b) & ~ (a & b)
End
' A short Logical XOR operator using bitwise operators
'
' In Cerberus-X True is 1 (you may cast Bool into Int using Int(False) = 0 and Int(True) = 1)
'
' This example uses Int instead of Bool to make it compatible with true being both -1 and 1
' Example Outputs :
Print XOR(0,0) ' 0
Print XOR(1,1) ' 0
Print XOR(1,0) ' 1
Print XOR(0,1) ' 1
Print "------"
Print XOR(0,0) ' 0
Print XOR(-1,-1) ' 0
Print XOR(-1,0) '-1
Print XOR(0,-1) '-1
Return 0
Complete working example :
Cerberus:
Strict
Import mojo2
Class myClass Extends App
Field canvas:Canvas
Method OnCreate:Int()
canvas = New Canvas
' A short Logical XOR operator using bitwise operators
'
' In Cerberus-X True is 1 (you may cast Bool into Int using Int(False) = 0 and Int(True) = 1)
'
' This example uses Int instead of Bool to make it compatible with true being both -1 and 1
' Example Outputs :
Print XOR(0,0) ' 0
Print XOR(1,1) ' 0
Print XOR(1,0) ' 1
Print XOR(0,1) ' 1
Print "------"
Print XOR(0,0) ' 0
Print XOR(-1,-1) ' 0
Print XOR(-1,0) '-1
Print XOR(0,-1) '-1
Return 0
End
Method OnUpdate:Int()
Return 0
End
Method OnRender:Int()
canvas.Clear (0,0.5,0)
canvas.DrawText( String("Check the console").Split("~n")),DeviceWidth()/2,DeviceHeight()/2,0.5,0.5
canvas.Flush
Return 0
End
End
Function Main:Int()
New myClass
Return 0
End
Function XOR:Int(a:Int,b:Int)
Return (a | b) & ~ (a & b)
End
Last edited: