Bitwise operations: Difference between revisions

Added FreeBASIC
(Lingo added)
(Added FreeBASIC)
Line 1,108:
</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>
' FB 1.05.0 Win64 (Note the (U)Integer type is 64 bits)
 
' FB doesn't have built-in logical shift right or rotation operators
' but, as they're not difficult to implement, I've done so below.
 
Function lsr(x As Const Integer, y As Const Integer) As Integer
Dim As UInteger z = x
Return z Shr y
End Function
Function rol(x As Const Integer, y As Const UInteger) As Integer
Dim z As Integer = x
Dim high As Integer
For i As Integer = 1 To y
high = Bit(z, 63)
For j As Integer = 62 To 0 Step -1
If Bit(z, j) Then
z = BitSet(z, j + 1)
Else
z = BitReset (z, j + 1)
End If
Next j
If high Then
z = BitSet(z, 0)
Else
z = BitReset(z, 0)
End If
Next i
Return z
End Function
 
Function ror(x As Const Integer, y As Const UInteger) As Integer
Dim z As Integer = x
Dim low As Integer
For i As Integer = 1 To y
low = Bit(z, 0)
For j As Integer = 1 To 63
If Bit(z, j) Then
z = BitSet(z, j - 1)
Else
z = BitReset (z, j - 1)
End If
Next j
If low Then
z = BitSet(z, 63)
Else
z = BitReset(z, 63)
End If
Next i
Return z
End Function
 
Sub bitwise(x As Integer, y As Integer)
Print "x = "; x
Print "y = "; y
Print "x AND y = "; x And y
Print "x OR y = "; x Or y
Print "x XOR y = "; x XOr y
Print "NOT x = "; Not x
Print "x SHL y = "; x Shl y
Print "x SHR y = "; x Shr y
Print "x LSR y = "; lsr(x, y)
Print "x ROL y = "; rol(x, y)
Print "x ROR y = "; ror(x, y)
End Sub
 
bitwise -15, 3
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<pre>
x = -15
y = 3
x AND y = 1
x OR y = -13
x XOR y = -14
NOT x = 14
x SHL y = -120
x SHR y = -2
x LSR y = 2305843009213693950
x ROL y = -113
x ROR y = 4611686018427387902
</pre>
 
=={{header|FutureBasic}}==
9,490

edits