Bitwise operations: Difference between revisions

→‎{{header|Kotlin}}: uses pure kotlin; no java
(Add Ecstasy example)
(→‎{{header|Kotlin}}: uses pure kotlin; no java)
Line 3,689:
</pre>
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">/* for symmetry with Kotlin's other binary bitwise operators
fun main() {
we wrap Java's 'rotate' methods as infix functions */
// inferred type of x and y is Int i.e. (32 -bit signed integersinteger)
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 y = 2
println("x = $x")
println("y = $y")
println("NOT x = ${x.inv()}")
println("x AND y = ${x and y}")
println("x OR y = ${x or 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 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 ROL y = ${x rol .rotateLeft(y)}")
println("x ROR y = ${x ror .rotateRight(y)}")
}</syntaxhighlight>
 
{{out}}
<pre>
x = 10
y = 2
NOT x = -11
x AND y = 2
x OR y = 10
x XOR y = 8
x SHL y = 40
Line 3,725 ⟶ 3,723:
x ROR y = -2147483646
</pre>
 
=={{header|LFE}}==
 
32

edits