进制: 与、或、异或操作

2018-09-27  本文已影响15人  极光火狐狸

源码: bitwise.py

# -.- coding:utf-8 -.-
import unittest


class TestBitwise(unittest.TestCase):

    """
    与、或、异或操作:
        与:       &
        或:       |
        异或:     ^
    """

    def setUp(self):
        self.a = 60                         # 00111100
        self.b = 13                         # 00001101

    def test_and_operator(self):
        value = self.a & self.b             # 00001100     提取相同的
        self.assertEqual(value, 12)

        binary_value = bin(value)
        self.assertEqual(binary_value, "0b1100")

    def test_or_operator(self):
        value = self.a | self.b             # 00111101      提取全部
        self.assertEqual(value, 61)

        binary_value = bin(value)
        self.assertEqual(binary_value, "0b111101")

    def test_xor_operator(self):
        value = self.a ^ self.b             # 00110001      提取不同的
        self.assertEqual(value, 49)

        binary_value = bin(value)
        self.assertEqual(binary_value, "0b110001")

 
 

测试: main.py

import unittest


TEST_MODULE = [
    "bitwise",
]


if __name__ == '__main__':
    suite = unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULE)
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suite)

上一篇下一篇

猜你喜欢

热点阅读