美文网首页
进制: 位移操作

进制: 位移操作

作者: 极光火狐狸 | 来源:发表于2018-09-27 14:28 被阅读14次

    源码: shifting.py

    # -.- coding:utf-8 -.-
    from __future__ import print_function
    import unittest
    
    
    class TestShifting(unittest.TestCase):
    
        """
        位移操作:
            左移(增大):     <<
            右移(减小):     >>
        """
    
        def test_left_shifting_by_one(self):
            value = 1                                   # 00000001
            value <<= 1                                 # 00000010
            self.assertEqual(value, 2)
    
            binary_value = bin(value)                   # "0b10"
            self.assertEqual(binary_value, "0b10")
    
        def test_left_shifting_by_two(self):
            value = 1                                   # 00000001
            value <<= 2                                 # 00000100
            self.assertEqual(value, 4)
    
            binary_value = bin(value)                   # "0b100"
            self.assertEqual(binary_value, "0b100")
    
        def test_left_shifting_by_five(self):
            value = 1                                   # 00000001
            value <<= 5                                 # 00100000
            self.assertEqual(value, 32)
    
            binary_value = bin(value)                   # "0b100000"
            self.assertEqual(binary_value, "0b100000")
    
        def test_left_shifting_by_ten(self):
            value = 1                                   # 00000001
            value <<= 10                                # 00000100 00000000
            self.assertEqual(value, 1024)
    
            binary_value = bin(value)                   # "0b10000000000"
            self.assertEqual(binary_value, "0b10000000000")
    
        def test_right_shifting_can_not_less_than_zero(self):
            value = 1                                   # 00000001
            value >>= 10                                # 00000000
            self.assertEqual(value, 0)
    
            binary_value = bin(value)                   # "0b0"
            self.assertEqual(binary_value, "0b0")
    
        def test_right_shifting_by_one(self):
            value = 255                                 # 11111111
            value >>= 1                                 # 01111111
            self.assertEqual(value, 127)
    
            binary_value = bin(value)                   # "0b1111111"
            self.assertEqual(binary_value, "0b1111111")
    
        def test_right_shifting_by_two(self):
            value = 255                                 # 11111111
            value >>= 2                                 # 00111111
            self.assertEqual(value, 63)
    
            binary_value = bin(value)                   # "0b111111"
            self.assertEqual(binary_value, "0b111111")
    
        def test_right_shifting_by_five(self):
            value = 255                                 # 11111111
            value >>= 5                                 # 00000111
            self.assertEqual(value, 7)
    
            binary_value = bin(value)                   # "0b111"
            self.assertEqual(binary_value, "0b111")
    
    

     
     

    测试: main.py

    import unittest
    
    
    TEST_MODULE = [
        "shifting",
    ]
    
    
    if __name__ == '__main__':
        suite = unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULE)
        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(suite)
    
    

    相关文章

      网友评论

          本文标题:进制: 位移操作

          本文链接:https://www.haomeiwen.com/subject/ixyeoftx.html