aboutsummaryrefslogtreecommitdiffstats
path: root/src/zrsh.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-03-04 23:50:00 +0100
committerMattias Andrée <maandree@kth.se>2016-03-04 23:50:00 +0100
commit76d0af5599554d11f104d582cdac8fbaa8569fcc (patch)
tree0ed9889a86d52ebd208382f2fd49dad0570d1f8c /src/zrsh.c
parentClean up, fix a few bugs, and add a test (diff)
downloadlibzahl-76d0af5599554d11f104d582cdac8fbaa8569fcc.tar.gz
libzahl-76d0af5599554d11f104d582cdac8fbaa8569fcc.tar.bz2
libzahl-76d0af5599554d11f104d582cdac8fbaa8569fcc.tar.xz
Clean up, add zerror and zperror, fix bugs and add more tests
Signed-off-by: Mattias Andrée <maandree@kth.se>
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));