diff options
author | Guido van Rossum <guido@python.org> | 1992-01-14 18:33:22 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1992-01-14 18:33:22 (GMT) |
commit | f3b351f140e9365edd32fbde866219dbd98f97cc (patch) | |
tree | 1dda7a9a30c08422c0a879b40500ed019ad56856 /Objects/intobject.c | |
parent | a38c0ff7cd289b73b32ee3c7b16ceef3f229d42b (diff) | |
download | cpython-f3b351f140e9365edd32fbde866219dbd98f97cc.zip cpython-f3b351f140e9365edd32fbde866219dbd98f97cc.tar.gz cpython-f3b351f140e9365edd32fbde866219dbd98f97cc.tar.bz2 |
Different shift implementation.
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r-- | Objects/intobject.c | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c index 181cb4f..edfface 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -375,7 +375,19 @@ int_lshift(v, w) } a = v->ob_ival; b = ((intobject *)w) -> ob_ival; - return newintobject((unsigned long)a << b); + if (b < 0) { + err_setstr(ValueError, "negative shift count"); + return NULL; + } + if (a == 0 || b == 0) { + INCREF(v); + return (object *) v; + } + if (b >= 32) { + return newintobject(0L); + } + a = (unsigned long)a << b; + return newintobject(a); } static object * @@ -390,7 +402,27 @@ int_rshift(v, w) } a = v->ob_ival; b = ((intobject *)w) -> ob_ival; - return newintobject((unsigned long)a >> b); + if (b < 0) { + err_setstr(ValueError, "negative shift count"); + return NULL; + } + if (a == 0 || b == 0) { + INCREF(v); + return (object *) v; + } + if (b >= 32) { + if (a < 0) + a = -1; + else + a = 0; + } + else { + if (a < 0) + a = ~( ~(unsigned long)a >> b ); + else + a = (unsigned long)a >> b; + } + return newintobject(a); } static object * |