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=" |
<syntaxhighlight lang="kotlin"> |
||
fun main() { |
|||
we wrap Java's 'rotate' methods as infix functions */ |
|||
⚫ | |||
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>) { |
|||
⚫ | |||
val x = 10 |
val x = 10 |
||
val y = 2 |
val y = 2 |
||
println("x |
println("x = $x") |
||
println("y |
println("y = $y") |
||
println("NOT x |
println("NOT x = ${x.inv()}") |
||
println("x AND y = ${x and y}") |
println("x AND y = ${x and y}") |
||
println("x OR |
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}") |
println("x ASR y = ${x shr y}") // arithmetic shift right (sign bit filled) |
||
println("x LSR y = ${x ushr y}") |
println("x LSR y = ${x ushr y}") // logical shift right (zero filled) |
||
println("x ROL y = ${x |
println("x ROL y = ${x.rotateLeft(y)}") |
||
println("x ROR y = ${x |
println("x ROR y = ${x.rotateRight(y)}") |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
x |
x = 10 |
||
y |
y = 2 |
||
NOT x |
NOT x = -11 |
||
x AND y = 2 |
x AND y = 2 |
||
x OR |
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}}== |
||