summaryrefslogtreecommitdiff
path: root/src/mp-binary.c
diff options
context:
space:
mode:
authorStefan Tauner <[email protected]>2012-04-13 01:45:02 +0200
committerStefano Karapetsas <[email protected]>2012-04-22 19:27:00 +0200
commit7155e47537e3707d42a675d2603386963c6d9156 (patch)
treea1b6e49f43b9408f1dbcb83e41b9606c83d19fd0 /src/mp-binary.c
parent5f32b5b06eed534bf0201266169dcb7e8d833349 (diff)
downloadmate-calc-7155e47537e3707d42a675d2603386963c6d9156.tar.bz2
mate-calc-7155e47537e3707d42a675d2603386963c6d9156.tar.xz
Support shifts with keyboard
This patch also changes the shift operation itself to use MPNumber for the multiplier too. Previously a signed int was used, which led to "interesting" results for bigger numbers. This was hidden because the GUI did not allow shifts with more than 15 places. Signed-off-by: Stefan Tauner <[email protected]>
Diffstat (limited to 'src/mp-binary.c')
-rw-r--r--src/mp-binary.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/mp-binary.c b/src/mp-binary.c
index e4cedba..f85b3ee 100644
--- a/src/mp-binary.c
+++ b/src/mp-binary.c
@@ -167,7 +167,9 @@ mp_mask(const MPNumber *x, int wordlen, MPNumber *z)
void
mp_shift(const MPNumber *x, int count, MPNumber *z)
{
- int i, multiplier = 1;
+ int i;
+ MPNumber multiplier;
+ mp_set_from_integer(1, &multiplier);
if (!mp_is_integer(x)) {
/* Translators: Error displayed when bit shift attempted on non-integer values */
@@ -177,15 +179,14 @@ mp_shift(const MPNumber *x, int count, MPNumber *z)
if (count >= 0) {
for (i = 0; i < count; i++)
- multiplier *= 2;
- mp_multiply_integer(x, multiplier, z);
+ mp_multiply_integer(&multiplier, 2, &multiplier);
+ mp_multiply(x, &multiplier, z);
}
else {
- MPNumber temp;
for (i = 0; i < -count; i++)
- multiplier *= 2;
- mp_divide_integer(x, multiplier, &temp);
- mp_floor(&temp, z);
+ mp_multiply_integer(&multiplier, 2, &multiplier);
+ mp_divide(x, &multiplier, z);
+ mp_floor(z, z);
}
}