Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function or bitwise #307

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we forcing the sign to positive here?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand the reasoning behind forcing to positive. I added a TODO and I'll reopen the issue 290 after this is merged.

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)
}
}