]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Different shift implementation.
authorGuido van Rossum <guido@python.org>
Tue, 14 Jan 1992 18:33:22 +0000 (18:33 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 14 Jan 1992 18:33:22 +0000 (18:33 +0000)
Objects/intobject.c

index 181cb4f42114b3b8b771425b2ea6494719282955..edffacefe48a42c91635d13a241587bd27b3180e 100644 (file)
@@ -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 *