From 8ef9117924709b0ce013b5f5d2db5bb7fe41b987 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 31 Mar 2014 15:38:26 -0300 Subject: [PATCH] fancier way to do sign extension --- lstrlib.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 3da669d7..880c4519 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.189 2014/03/21 14:26:44 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.190 2014/03/27 15:58:05 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -1049,9 +1049,8 @@ static int unpackint (const char *buff, lua_Integer *res, n |= (lua_Integer)(unsigned char)buff[littleendian ? size - 1 - i : i]; } if (size < SZINT) { /* need sign extension? */ - lua_Integer mask = (~(lua_Integer)0) << (size*NB - 1); - if (n & mask) /* negative value? */ - n |= mask; /* signal extension */ + lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); + n = (lua_Integer)((n ^ mask) - mask); /* do sign extension */ } *res = n; return 1;