Skip to content

Commit

Permalink
Fix function or bitwise
Browse files Browse the repository at this point in the history
  • Loading branch information
HoangNguyen219 committed Jul 14, 2024
1 parent aa4202c commit a8bbec2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,14 @@ 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. */
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,6 +27,41 @@ 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() {
Expand All @@ -35,20 +70,16 @@ class BigIntegerBitwiseOperations {
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 a8bbec2

Please sign in to comment.