1.1 --- a/lib/__builtins__/int.py Mon Apr 03 00:13:46 2017 +0200
1.2 +++ b/lib/__builtins__/int.py Mon Apr 03 01:36:47 2017 +0200
1.3 @@ -22,9 +22,9 @@
1.4 from __builtins__.operator import _negate
1.5 from __builtins__.unicode import utf8string
1.6 from native import get_maxint, get_minint, is_int, \
1.7 - int_add, int_and, int_div, int_eq, int_gt, int_lt, int_mod, \
1.8 - int_mul, int_ne, int_neg, int_not, int_or, int_pow, \
1.9 - int_str, int_sub, int_xor
1.10 + int_add, int_and, int_div, int_eq, int_gt, int_lshift, \
1.11 + int_lt, int_mod, int_mul, int_ne, int_neg, int_not, \
1.12 + int_or, int_pow, int_rshift, int_str, int_sub, int_xor
1.13
1.14 class int:
1.15
1.16 @@ -164,6 +164,34 @@
1.17 __or__ = __ror__ = __ior__
1.18 __xor__ = __rxor__ = __ixor__
1.19
1.20 + def __lshift__(self, other):
1.21 +
1.22 + "Return a new int left-shifted by 'other'."
1.23 +
1.24 + return self._binary_op(int_lshift, other)
1.25 +
1.26 + def __rlshift__(self, other):
1.27 +
1.28 + "Return a new int left-shifted by 'other'."
1.29 +
1.30 + return self._binary_op_rev(int_lshift, other)
1.31 +
1.32 + def __rshift__(self, other):
1.33 +
1.34 + "Return a new int right-shifted by 'other'."
1.35 +
1.36 + return self._binary_op(int_rshift, other)
1.37 +
1.38 + def __rrshift__(self, other):
1.39 +
1.40 + "Return a new int right-shifted by 'other'."
1.41 +
1.42 + return self._binary_op_rev(int_rshift, other)
1.43 +
1.44 + __ilshift__ = __lshift__
1.45 +
1.46 + __irshift__ = __rshift__
1.47 +
1.48 def __lt__(self, other):
1.49
1.50 "Return whether this int is less than 'other'."
1.51 @@ -220,13 +248,6 @@
1.52
1.53 __repr__ = __str__
1.54
1.55 - def __lshift__(self): pass
1.56 - def __rlshift__(self): pass
1.57 - def __rshift__(self): pass
1.58 - def __rrshift__(self): pass
1.59 - def __ilshift__(self): pass
1.60 - def __irshift__(self): pass
1.61 -
1.62 def __bool__(self):
1.63
1.64 "Return whether this int is non-zero."
2.1 --- a/lib/native/__init__.py Mon Apr 03 00:13:46 2017 +0200
2.2 +++ b/lib/native/__init__.py Mon Apr 03 01:36:47 2017 +0200
2.3 @@ -25,6 +25,7 @@
2.4
2.5 from native.int import int_add, int_div, int_mod, int_mul, int_neg, int_pow, \
2.6 int_sub, int_and, int_not, int_or, int_xor, \
2.7 + int_lshift, int_rshift, \
2.8 int_eq, int_ge, int_gt, int_le, int_lt, int_ne, \
2.9 int_str, is_int
2.10
3.1 --- a/lib/native/int.py Mon Apr 03 00:13:46 2017 +0200
3.2 +++ b/lib/native/int.py Mon Apr 03 01:36:47 2017 +0200
3.3 @@ -42,6 +42,9 @@
3.4 def int_or(self, other): return 0
3.5 def int_xor(self, other): return 0
3.6
3.7 +def int_lshift(self, other): return 0
3.8 +def int_rshift(self, other): return 0
3.9 +
3.10 def int_eq(self, other): return True or False
3.11 def int_ge(self, other): return True or False
3.12 def int_gt(self, other): return True or False
4.1 --- a/lib/operator/binary.py Mon Apr 03 00:13:46 2017 +0200
4.2 +++ b/lib/operator/binary.py Mon Apr 03 01:36:47 2017 +0200
4.3 @@ -21,6 +21,7 @@
4.4
4.5 from operator.core import binary_op, is_, is_not
4.6 from native import int_add, int_div, int_mod, int_mul, int_pow, int_sub, \
4.7 + int_lshift, int_rshift, \
4.8 int_and, int_not, int_or, int_xor, \
4.9 is_int
4.10
4.11 @@ -58,6 +59,8 @@
4.12 return not b.__contains__(a)
4.13
4.14 def lshift(a, b):
4.15 + if is_int(a) and is_int(b):
4.16 + return int_lshift(a, b)
4.17 return binary_op(a, b, lambda a: a.__lshift__, lambda b: b.__rlshift__)
4.18
4.19 def mod(a, b):
4.20 @@ -81,6 +84,8 @@
4.21 return binary_op(a, b, lambda a: a.__pow__, lambda b: b.__rpow__)
4.22
4.23 def rshift(a, b):
4.24 + if is_int(a) and is_int(b):
4.25 + return int_rshift(a, b)
4.26 return binary_op(a, b, lambda a: a.__rshift__, lambda b: b.__rrshift__)
4.27
4.28 def sub(a, b):
5.1 --- a/templates/native/int.c Mon Apr 03 00:13:46 2017 +0200
5.2 +++ b/templates/native/int.c Mon Apr 03 01:36:47 2017 +0200
5.3 @@ -192,6 +192,28 @@
5.4 return __new_int(i ^ j);
5.5 }
5.6
5.7 +__attr __fn_native_int_int_lshift(__attr __self, __attr self, __attr other)
5.8 +{
5.9 + /* self and other interpreted as int */
5.10 + int i = __TOINT(self);
5.11 + int j = __TOINT(other);
5.12 +
5.13 + /* Return the new integer. */
5.14 + /* NOTE: No overflow test applied. */
5.15 + return __new_int(i << j);
5.16 +}
5.17 +
5.18 +__attr __fn_native_int_int_rshift(__attr __self, __attr self, __attr other)
5.19 +{
5.20 + /* self and other interpreted as int */
5.21 + int i = __TOINT(self);
5.22 + int j = __TOINT(other);
5.23 +
5.24 + /* Return the new integer. */
5.25 + /* NOTE: No overflow test applied. */
5.26 + return __new_int(i >> j);
5.27 +}
5.28 +
5.29 __attr __fn_native_int_int_le(__attr __self, __attr self, __attr other)
5.30 {
5.31 /* self and other interpreted as int */
6.1 --- a/templates/native/int.h Mon Apr 03 00:13:46 2017 +0200
6.2 +++ b/templates/native/int.h Mon Apr 03 01:36:47 2017 +0200
6.3 @@ -37,6 +37,9 @@
6.4 __attr __fn_native_int_int_or(__attr __self, __attr _data, __attr other);
6.5 __attr __fn_native_int_int_xor(__attr __self, __attr _data, __attr other);
6.6
6.7 +__attr __fn_native_int_int_lshift(__attr __self, __attr _data, __attr other);
6.8 +__attr __fn_native_int_int_rshift(__attr __self, __attr _data, __attr other);
6.9 +
6.10 __attr __fn_native_int_int_eq(__attr __self, __attr _data, __attr other);
6.11 __attr __fn_native_int_int_ge(__attr __self, __attr _data, __attr other);
6.12 __attr __fn_native_int_int_gt(__attr __self, __attr _data, __attr other);
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/tests/bitwise.py Mon Apr 03 01:36:47 2017 +0200
7.3 @@ -0,0 +1,7 @@
7.4 +def encode(value):
7.5 + return ((value & 8) << 4) | ((value & 4) << 3) | ((value & 2) << 2) | ((value & 1) << 1)
7.6 +
7.7 +print encode(15) # 170
7.8 +print encode(8) # 128
7.9 +print encode(2) # 8
7.10 +print encode(0) # 0