implement bitwise operations
This commit is contained in:
35
src/commonMain/kotlin/com/infendro/bytearray/operation.kt
Normal file
35
src/commonMain/kotlin/com/infendro/bytearray/operation.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import kotlin.experimental.and
|
||||
import kotlin.experimental.or
|
||||
import kotlin.experimental.xor
|
||||
|
||||
infix fun ByteArray.and(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
if (this.size != that.size) throw Exception()
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] and that[i]
|
||||
}
|
||||
}
|
||||
|
||||
infix fun ByteArray.or(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
if (this.size != that.size) throw Exception()
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] or that[i]
|
||||
}
|
||||
}
|
||||
|
||||
infix fun ByteArray.xor(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
if (this.size != that.size) throw Exception()
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] xor that[i]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user