Skip to content

Commit c548423

Browse files
authored
Merge pull request #310 from ionspin/fixOr
Fix or
2 parents a53ea6f + 8aef7df commit c548423

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,15 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)
635635
return BigInteger(arithmetic.and(this.magnitude, other.magnitude), sign)
636636
}
637637

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

642649
override infix fun xor(other: BigInteger): BigInteger {

bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/base63/array/BigInteger63Arithmetic.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,7 @@ internal object BigInteger63Arithmetic : BigIntegerArithmetic {
19721972
}
19731973

19741974
override fun or(operand: ULongArray, mask: ULongArray): ULongArray {
1975+
if (operand.size < mask.size) return or(mask, operand)
19751976
return removeLeadingZeros(
19761977
ULongArray(operand.size) {
19771978
if (it < mask.size) {

bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/integer/BigIntegerBitwiseOperations.kt

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,59 @@ import kotlin.test.assertEquals
2727
* on 01-Nov-2019
2828
*/
2929
class BigIntegerBitwiseOperations {
30+
@Test
31+
fun andWithZero() {
32+
val operand = BigInteger.parseString("11110000", 2)
33+
val mask = BigInteger.ZERO
34+
35+
assertEquals(mask, operand and mask)
36+
assertEquals(mask, mask and operand)
37+
}
38+
39+
@Test
40+
fun andBiggerThanLongMaxWithZero() {
41+
val operand = BigInteger.parseString("9223372036854775808", 10)
42+
val mask = BigInteger.ZERO
43+
44+
assertEquals(mask, operand and mask)
45+
assertEquals(mask, mask and operand)
46+
}
47+
48+
@Test
49+
fun orWithZero() {
50+
val operand = BigInteger.parseString("11110000", 2)
51+
val mask = BigInteger.ZERO
52+
53+
assertEquals(operand, operand or mask)
54+
assertEquals(operand, mask or operand)
55+
}
56+
57+
@Test
58+
fun orBiggerThanLongMaxWithZero() {
59+
val operand = BigInteger.parseString("9223372036854775808", 10)
60+
val mask = BigInteger.ZERO
61+
62+
assertEquals(operand, operand or mask)
63+
assertEquals(operand, mask or operand)
64+
}
65+
3066
@Test
3167
fun xorWithZero() {
3268
val operand = BigInteger.parseString("11110000", 2)
3369
val mask = BigInteger.ZERO
3470
val xorResult = operand xor mask
3571
println("Xor result: ${xorResult.toString(2)}")
3672

37-
val expectedResult = operand
38-
39-
assertEquals(expectedResult, xorResult)
40-
assertEquals(expectedResult, mask xor operand)
73+
assertEquals(operand, xorResult)
74+
assertEquals(operand, mask xor operand)
4175
}
4276

4377
@Test
4478
fun xorBiggerThanLongMaxWithZero() {
4579
val operand = BigInteger.parseString("9223372036854775808", 10)
4680
val mask = BigInteger.ZERO
4781

48-
val expectedResult = operand
49-
50-
assertEquals(expectedResult, operand xor mask)
51-
assertEquals(expectedResult, mask xor operand)
82+
assertEquals(operand, operand xor mask)
83+
assertEquals(operand, mask xor operand)
5284
}
5385
}

0 commit comments

Comments
 (0)