Skip to content

Commit

Permalink
Merge pull request #310 from ionspin/fixOr
Browse files Browse the repository at this point in the history
Fix or
  • Loading branch information
ionspin authored Jul 14, 2024
2 parents a53ea6f + 8aef7df commit c548423
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,15 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)
return BigInteger(arithmetic.and(this.magnitude, other.magnitude), sign)
}

/** Returns a new BigInt with bits combining [this], [other] doing a bitwise `|`/`or` operation. Forces sign to positive. */
// TODO Investigate what is expected behavior when one of the operand is negative. Is it considered to be two's complement?
override infix fun or(other: BigInteger): BigInteger {
return BigInteger(arithmetic.or(this.magnitude, other.magnitude), sign)
val resultMagnitude = arithmetic.or(this.magnitude, other.magnitude)
val resultSign = when {
isResultZero(resultMagnitude) -> Sign.ZERO
else -> Sign.POSITIVE
}
return BigInteger(resultMagnitude, resultSign)
}

override infix fun xor(other: BigInteger): BigInteger {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,7 @@ internal object BigInteger63Arithmetic : BigIntegerArithmetic {
}

override fun or(operand: ULongArray, mask: ULongArray): ULongArray {
if (operand.size < mask.size) return or(mask, operand)
return removeLeadingZeros(
ULongArray(operand.size) {
if (it < mask.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,59 @@ import kotlin.test.assertEquals
* on 01-Nov-2019
*/
class BigIntegerBitwiseOperations {
@Test
fun andWithZero() {
val operand = BigInteger.parseString("11110000", 2)
val mask = BigInteger.ZERO

assertEquals(mask, operand and mask)
assertEquals(mask, mask and operand)
}

@Test
fun andBiggerThanLongMaxWithZero() {
val operand = BigInteger.parseString("9223372036854775808", 10)
val mask = BigInteger.ZERO

assertEquals(mask, operand and mask)
assertEquals(mask, mask and operand)
}

@Test
fun orWithZero() {
val operand = BigInteger.parseString("11110000", 2)
val mask = BigInteger.ZERO

assertEquals(operand, operand or mask)
assertEquals(operand, mask or operand)
}

@Test
fun orBiggerThanLongMaxWithZero() {
val operand = BigInteger.parseString("9223372036854775808", 10)
val mask = BigInteger.ZERO

assertEquals(operand, operand or mask)
assertEquals(operand, mask or operand)
}

@Test
fun xorWithZero() {
val operand = BigInteger.parseString("11110000", 2)
val mask = BigInteger.ZERO
val xorResult = operand xor mask
println("Xor result: ${xorResult.toString(2)}")

val expectedResult = operand

assertEquals(expectedResult, xorResult)
assertEquals(expectedResult, mask xor operand)
assertEquals(operand, xorResult)
assertEquals(operand, mask xor operand)
}

@Test
fun xorBiggerThanLongMaxWithZero() {
val operand = BigInteger.parseString("9223372036854775808", 10)
val mask = BigInteger.ZERO

val expectedResult = operand

assertEquals(expectedResult, operand xor mask)
assertEquals(expectedResult, mask xor operand)
assertEquals(operand, operand xor mask)
assertEquals(operand, mask xor operand)
}
}

0 comments on commit c548423

Please sign in to comment.