Bitwise operations: Difference between revisions

Content added Content deleted
(Add Ecstasy example)
(→‎{{header|Kotlin}}: uses pure kotlin; no java)
Line 3,689: Line 3,689:
</pre>
</pre>
=={{header|Kotlin}}==
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">/* for symmetry with Kotlin's other binary bitwise operators
<syntaxhighlight lang="kotlin">
fun main() {
we wrap Java's 'rotate' methods as infix functions */
// inferred type of x and y is Int (32-bit signed integer)
infix fun Int.rol(distance: Int): Int = Integer.rotateLeft(this, distance)
infix fun Int.ror(distance: Int): Int = Integer.rotateRight(this, distance)

fun main(args: Array<String>) {
// inferred type of x and y is Int i.e. 32 bit signed integers
val x = 10
val x = 10
val y = 2
val y = 2
println("x = $x")
println("x = $x")
println("y = $y")
println("y = $y")
println("NOT x = ${x.inv()}")
println("NOT x = ${x.inv()}")
println("x AND y = ${x and y}")
println("x AND y = ${x and y}")
println("x OR y = ${x or y}")
println("x OR y = ${x or y}")
println("x XOR y = ${x xor y}")
println("x XOR y = ${x xor y}")

// All operations below actually return (x OP (y % 32)) so that a value is never completely shifted out
println("x SHL y = ${x shl y}")
println("x SHL y = ${x shl y}")
println("x ASR y = ${x shr y}") // arithmetic shift right (sign bit filled)
println("x ASR y = ${x shr y}") // arithmetic shift right (sign bit filled)
println("x LSR y = ${x ushr y}") // logical shift right (zero filled)
println("x LSR y = ${x ushr y}") // logical shift right (zero filled)
println("x ROL y = ${x rol y}")
println("x ROL y = ${x.rotateLeft(y)}")
println("x ROR y = ${x ror y}")
println("x ROR y = ${x.rotateRight(y)}")
}</syntaxhighlight>
}</syntaxhighlight>


{{out}}
{{out}}
<pre>
<pre>
x = 10
x = 10
y = 2
y = 2
NOT x = -11
NOT x = -11
x AND y = 2
x AND y = 2
x OR y = 10
x OR y = 10
x XOR y = 8
x XOR y = 8
x SHL y = 40
x SHL y = 40
Line 3,725: Line 3,723:
x ROR y = -2147483646
x ROR y = -2147483646
</pre>
</pre>

=={{header|LFE}}==
=={{header|LFE}}==