python--- 难缠符号,二进制,位移,取反,异或,按位与,按位或,奇偶判断,硬盘容量,真假

python— 难缠符号

一、先得从二进制讲起

root@kali:~/python/laowangpy# ipython
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
Type "copyright", "credits" or "license" for more information.

IPython 4.0.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: print 'hello'
hello

In [2]: a = 1

In [3]: bin(a)
Out[3]: '0b1'

In [4]: bin(4)
Out[4]: '0b100'

In [5]: bin(3)
Out[5]: '0b11'

In [6]: bin(2)
Out[6]: '0b10'

In [7]: bin(5)
Out[7]: '0b101'

In [8]: 

二、难缠符号
bin
1、难缠符号之一 >> <<
位移

In [8]: 3 >> 1
Out[8]: 1

In [9]: bin(3)
Out[9]: '0b11'

In [10]: bin(1)
Out[10]: '0b1'

In [11]: 
In [11]: 31 >>1
Out[11]: 15

In [12]: bin(31)
Out[12]: '0b11111'

In [13]: bin(15)
Out[13]: '0b1111'

In [14]: 
In [14]: 1<< 1
Out[14]: 2

In [15]: bin(2)
Out[15]: '0b10'

In [16]: bin(1)
Out[16]: '0b1'

In [17]: 2 << 1
Out[17]: 4

In [18]: bin(4)
Out[18]: '0b100'

In [19]: 4 << 1
Out[19]: 8

In [20]: bin(8)
Out[20]: '0b1000'

In [21]: 

左移公式
x<

In [26]: 2 << 3
Out[26]: 16

In [27]: 3 << 3
Out[27]: 24

In [28]: 4 << 3
Out[28]: 32

In [29]: 5 << 3
Out[29]: 40

In [30]: 3*(2**3)
Out[30]: 24

In [31]: 4*(2**3)
Out[31]: 32

In [32]: 5*(2**3)
Out[32]: 40

In [33]: 

2、难缠符号之二 &
按位与

1100
1000  &
---------
1000

1&1=1  1&0=0  0&1=0  0&0=0
In [33]: 10&20
Out[33]: 0

In [34]: bin(10)
Out[34]: '0b1010'

In [35]: bin(20)
Out[35]: '0b10100'

In [36]: 

01010
10100 &
---------
00000
In [36]: 40&32
Out[36]: 32

In [37]: bin(40)
Out[37]: '0b101000'

In [38]: bin(32)
Out[38]: '0b100000'

In [39]: 

101000
100000 &
---------
100000
In [43]: int(0b10000)
Out[43]: 16

In [44]: int(0b100000)
Out[44]: 32

In [45]: 

3、难缠符号之三 |
按位或

1|1=1  1|0=1  0|1=1 0|0=0
In [46]: 145 | 300
Out[46]: 445

In [47]: bin(145)
Out[47]: '0b10010001'

In [48]: bin(300)
Out[48]: '0b100101100'

In [49]: 

In [49]: bin(445)
Out[49]: '0b110111101'

In [50]: 

010010001
100101100 |
-------------
110111101

4、难缠符号之四 ^
按位异或

5、难缠符号之四 ^
按位取反

三、其他
1、&之奇偶判定

root@kali:~/python/laowangpy# python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 % 2
1
>>> 4%2
0
>>> 5&1
1
>>> 4&1
0
>>> bin(4)
'0b100'
>>> bin(1)
'0b1'

2、计算硬盘容量

>>> 11866 >> 10
11
>>> 11866 / (2**10)
11
>>> 2**10
1024
>>> 

3、真真假假不知晓

>>> True & True
True
>>> True & False
False
>>> False & False
False
>>> False & False & True
False
>>> False & False & True & True
False
>>> True & True & True & True
True
>>> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐为波

看着给就好了,学习写作有点累!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值