aboutsummaryrefslogtreecommitdiffstats
path: root/src/zrsh.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/zrsh.c')
-rw-r--r--src/zrsh.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/zrsh.c b/src/zrsh.c
index c5a1a05..a7b886b 100644
--- a/src/zrsh.c
+++ b/src/zrsh.c
@@ -20,7 +20,7 @@ zrsh(z_t a, z_t b, size_t bits)
}
bits = BITS_IN_LAST_CHAR(bits);
- cbits = BITS_PER_CHAR - 1 - bits;
+ cbits = BITS_PER_CHAR - bits;
if (chars && a == b) {
a->used -= chars;
@@ -31,10 +31,14 @@ zrsh(z_t a, z_t b, size_t bits)
zmemcpy(a->chars, b->chars + chars, a->used);
}
- a->chars[0] >>= bits;
- for (i = 1; i < a->used; i++) {
- a->chars[i - 1] |= a->chars[i] >> cbits;
- a->chars[i] >>= bits;
+ if (bits) { /* This if statement is very important in C. */
+ a->chars[0] >>= bits;
+ for (i = 1; i < a->used; i++) {
+ a->chars[i - 1] |= a->chars[i] << cbits;
+ a->chars[i] >>= bits;
+ }
+ while (!a->chars[a->used - 1])
+ a->used--;
}
SET_SIGNUM(a, zsignum(b));