Ticket #25912: add_bitwise_operators.diff

File add_bitwise_operators.diff, 1.9 KB (added by anabelensc, 9 years ago)
  • django/db/models/expressions.py

    diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
    index fecd393..f075fa3 100644
    a b class Combinable(object):  
    3232    # usage.
    3333    BITAND = '&'
    3434    BITOR = '|'
     35    BITLEFTSHIFT = '<<'
     36    BITRIGHTSHIFT = '>>'
    3537
    3638    def _combine(self, other, connector, reversed, node=None):
    3739        if not hasattr(other, 'resolve_expression'):
    class Combinable(object):  
    7880    def bitand(self, other):
    7981        return self._combine(other, self.BITAND, False)
    8082
     83    def bitleftshift(self, other):
     84        return self._combine(other, self.BITLEFTSHIFT, False)
     85
     86    def bitrightshift(self, other):
     87        return self._combine(other, self.BITRIGHTSHIFT, False)
     88
    8189    def __or__(self, other):
    8290        raise NotImplementedError(
    8391            "Use .bitand() and .bitor() for bitwise logical operations."
  • tests/expressions/tests.py

    diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
    index f688927..9a1d930 100644
    a b class ExpressionOperatorTests(TestCase):  
    604604        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 40)
    605605        self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
    606606
     607    def test_lefthand_bitwise_binary_left_shift_operator(self):
     608        Number.objects.filter(pk=self.n.pk).update(integer=F('integer').bitleftshift(2))
     609        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 168)
     610
     611    def test_lefthand_bitwise_binary_right_shift_operator(self):
     612        Number.objects.filter(pk=self.n.pk).update(integer=F('integer').bitrightshift(2))
     613        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 10)
     614
    607615    @skipUnlessDBFeature('supports_bitwise_or')
    608616    def test_lefthand_bitwise_or(self):
    609617        # LH Bitwise or on integers
Back to Top