From 76d0af5599554d11f104d582cdac8fbaa8569fcc Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 4 Mar 2016 23:50:00 +0100 Subject: Clean up, add zerror and zperror, fix bugs and add more tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/zrsh.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/zrsh.c') 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)); -- cgit v1.2.3-70-g09d2