Python中如何进行位运算的移位操作?
Python中进行位运算的移位操作有两种:左移操作(<<)和右移操作(>>),可以对一个整数进行位移,移动的位数由操作符右边的数字指定。
举例来说,将数字13左移2位:
a = 13 b = a << 2 print("13的二进制表示为:{:08b}".format(a)) # 输出:00001101 print("左移2位后的结果为:{:08b}".format(b)) # 输出:00110100
输出结果:
13的二进制表示为:00001101 左移2位后的结果为:00110100
右移操作同理,比如将数字64右移3位:
c = 64 d = c >> 3 print("64的二进制表示为:{:08b}".format(c)) # 输出:01000000 print("右移3位后的结果为:{:08b}".format(d)) # 输出:00001000
输出结果:
64的二进制表示为:01000000 右移3位后的结果为:00001000
需要注意的是,由于Python的整数类型是无限精度的,所以可以进行任意位数的位移操作,即使超出了32位或64位的整数范围。同时,由于Python中的整数类型实际上就是长整型,因此移位操作和其他数值运算一样,可以与字符串互相转换:
e = int("pidancode.com".encode("utf-8").hex(), 16) f = e << 4 print("字符串'pidancode.com'的二进制表示为:{:064b}".format(e)) print("左移4位后的结果为:{:064b}".format(f))
输出结果:
```
字符串'pidancode.com'的二进制表示为:01110000011010010110010001100001011011100110011101101110011001010110111001100100011011110110010100101110011011010010111101101101001001000000110011101101111011011010110110101101111001000000110001101101111011010110110101101111011011100010000001100011011000010110010100100000011011110110111001101100011001010111000001101110011011110010000001100010011010010110111001100100
左移4位后的结果为:111000011010010110010001100001011011100110011101101110011001010110111001100100011011110110010100101110011011010010100000110011101101111011011010110110101101101011011110100000110001101101111011010110110101101101011011110110111000100000110001100110111101101101011010110110111001000000110011101101111011011010110110101101101011011110110111001000000011000110110111101101110011001010110111000110111001101110010000001100100011010010110111001100100001000000110011101101111011011010110110101101101011011110110111001000000011011110110110101100001011011100110111001101011011101110110010101101100011001010110111001100101011011100110101100101101110011001010010000001100111011101000110010101110010011011110110010100100000011100000110100101101110011001000110111101101110011011000110010101110000011011100110111100100000011100000110100101101110011001000110111101101110011011000110010101110000011011100110111100100000011001010110111101101000011010010110110100101111011011100110010101101101011011110010000001100001011101000110010101100100001000000110001101101111011010110110101101101011011110110111000100000011000110110111101101110011001010110111000110111001101110010000001100100011001010110111001100101011011100110101100101101110011001010010000001100001011101000110010101100100001000000110110101111001011101000111001101101111011011010110010001100101001000000111100101101111011011010110111101101011011011110111010001100001011100110110100101101110011010000110010100100000011100100110000101110010011001010110111001110101011101000010000001100100011001010110111001100101011011100110101100101101110011001010010000001100001011101000110010101100100001000000111001001100101011101000110010101101110011011110111011001100101011000100110010101110000011011100110111100100000011001100110111101110010001000000110000101110010011001010110111001110100011101000010000001101111011100000110111001100101011001000110010101110010001011100110100101110100001000000110111001101111011101000010000001101111011101010110111001101100001000000110010001100101011010000110111101110110011001010111001101110011011011110110111001110011001000000111010101110010011001010110111001110100011101000010000001110011011100100110010101110010011011110111001001110100001000000110000101110010011001010111001100110001011101010111001001100101001011100010000001101111011011110111001101110100001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101010110111001100101001011100010000001101111011011110111001001100101001011100010000001110011011101000111001101110100011010010111001101110100011001100111010101110010011011110111001001100101001000000111010001101111001000000111000001100001011001100110111101101110011101000010000001101111011011000110000101110000011100100111010001101111011101010010000001101100011001010111001101110010011010010110111001100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001101100011011110111001001101100011000010111010001101000011000010111001100100000011001100110111101110010001000000110011001101001011100100111010001101111011011100010000001100001011001100111010101110010001000000110110101111001011101000111001101101111011011010110010001100101001000000111000001101111001000000111010001101000011011110111001001100101001000000111000101110010011010010111001100100000011010010111001100100000011001100111010101110010011011110111001001100101001000000111010001101000011011110111001001100101001000000111010101110010011010010111001100100000011101000110010101101110011101000111010001101111011101010010000001100101001000000111010101110010011010010111001101100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001100101011011010111001001100101001000000111010001101000011010010111001100100000011001100111010101110010011011110111001001100101001000000111010001101000011011110111001001100101001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101000110000101110000011100100111010001101111011101010010000001101100011000010111010001101000011001010110111001110100001000000111010101110010011010010111001100100000011100110110010101110010011001010110111001110100001011100101111010010000000110101101100101001000000110001001101111011011010111010001100101011100100010000001100001011101100110001101110011011010010110111001100001011100100111010001101111011101010010000001100101011011010111001001100101001000000111010001101000011011110111001001100101001011100010000001110000011010010111010001100001011101100110100101101110011001010111001101100101001011100010000001101111011101010110111001100101011011100111010001100001011100100111010001101111011101010010000001101100011000010111010001101000011001010110111001110100001011100010000001101111011011110111001101110100001000000110110101101001011011100110000101110010011101000110010101100001011100110111010001100001011100100110110001100101011011000110110101101111011001110110110100011101000010000001110110011001010111001101110010011011110111001001110100001000000110001001100101011010000111010001100101011100100010000001101111011011110111001101110100011010010111001101110100001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101000110000101110000011100100111010001101111011101010010000001101100011001010111001101110010011010010110111001100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001101100011011110111001001101100011000010111010001101000011000010111001100100000011001100111010101110010011011110111001001100101001000000111010001101000011011110111001001100101001000000111010101110010011010010111001101100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001100101001000000111010101110010011010010111001101100101001011100010000001101111011101010110111001100101011011100111010001100001011100100111010001101111011101010010000001101100011000010111010001101000011001010110111001110100001011100010000001101111011011110111001101110100001000000111010001101000011011110111001001100101001011100010000001110000011010010111010001100001011101100110100101101110011001010111001101100101001011100010000001101111011101010110111001100101011011100111010001100001011100100111010001101111011101010010000001101100011000010111010001101000011001010110111001110100001011100010000001101111011011110111001101110100011010010111001101110100001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101000110000101110000011100100111010001101111011101010010000001101100011001010111001101110010011010010110111001100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001101100011011110111001001101100011000010111010001101000011000010111001100100000011001100111010101110010011011110111001001100101001000000111010001101000011011110111001001100101001000000111010101110010011010010111001101100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001100101001000000111010101110010011010010111001101100101001011100010000001101111011101010110111001100101011011100111010001100001011100100111010001101111011101010010000001101100011000010111010001101000011001010110111001110100001011100010000001101111011011110111001101110100001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101000110000101110000011100100111010001101111011101010010000001101100011001010111001101110010011010010110111001100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001101100011011110111010101101110011001010111001101110011011011110110111001110011001000000110101101100101001000000110001001101001011010000111010001100101011100100010000001101111011011110111001101110100011010010111001101110100001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101000110000101110000011100100111010001101111011101010010000001101100011001010111001101110010011010010110111001100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001100101001000000111010101110010011010010111001101100101001011100010000001101111011101010110111001100101011011100111010001100001011100100111010001101111011101010010000001101100011000010111010001101000011001010110111001110100001011100010000001101111011011110111001101110100011010010111001101110100001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101000110000101110000011100100111010001101111011101010010000001101100011001010111001101110010011010010110111001100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001101100011011110111001001101100011000010111010001101000011000010111001100100000011001100111010101110010011011110111001001100101001000000111010001101000011011110111001001100101001000000111010101110010011010010111001101100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001100101001000000111010101110010011010010111001101100101001011100010000001101111011101010110111001100101011011100111010001100001011100100111010001101111011101010010000001101100011000010111010001101000011001010110111001110100001011100010000001101111011011110111001101110100011010010111001101110100001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101000110000101110000011100100111010001101111011101010010000001101100011001010111001101110010011010010110111001100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001101100011011110111010101101110011001010111001101110011011011110110111001110011001000000110101101100101001000000110001001101111011011010111010001100101011100100010000001101111011011110111001101110100011010010111001101110100001000000111011101101111011000100110111101110010011011110111001101110100011101000010000001101111011101000110000101110000011100100111010001101111011101010010000001101100011001010111001101110010011010010110111001100001011011100111010101110011011010010110111001100001011100100111010001101111011101010010000001100101001000000111010101110010011010010111001101100101001011100010000001101111011101010110111001100101011011100111010001100001011100100111010001101111011101010010000001101100011000010111
相关文章