位运算符

简介: 【6月更文挑战第28天】位运算符。

在了解了位运算符之后编写实例进行验证,在basis目录下新建文件,命名为bit.py,学习Python位运算符的操作,在PyCharm中输入代码。
operand_a = 5 # 5(DEC) == 0101(BIN)
operand_b = 2 # 2(DEC) == 0010(BIN)
operand_c = 0 # 0(DEC) == 0000(BIN)

按位与运算

operand_c = operand_a & operand_b
print("{} & {} = {}".format(operand_a, operand_b, operand_c))

按位或运算

operand_c = operand_a | operand_b
print("{} | {} = {}".format(operand_a, operand_b, operand_c))

按位取反运算

operand_c = ~operand_a
print("~{} = {}".format(operand_a, operand_c))

左移

operand_c = operand_a << 1
print("{} << 1 = {}".format(operand_a, operand_c))

右移

operand_c = operand_a >> 1
print("{} >> 1 = {}".format(operand_a, operand_c))

按位异或运算

operand_c = operand_a ^ operand_b
print("{} ^ {} = {}".format(operand_a, operand_b, operand_c))

目录
相关文章
|
9月前
|
C语言
基本的算术运算符
基本的算术运算符
88 1
|
3月前
算术运算符
【11月更文挑战第24天】
39 4
|
9月前
|
C++
C++程序中的算术运算符
C++程序中的算术运算符
95 2
|
7月前
|
存储 算法 安全
day10:算术运算符
【7月更文挑战第10天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
42 3
|
8月前
|
存储 编译器 C++
C++中的位运算符
C++中的位运算符
47 0
|
9月前
|
编译器 C++
c++算术运算符
c++算术运算符
43 0
|
9月前
位运算符
位运算符。
51 0
|
9月前
|
存储
位运算符和位操作符
位运算符和位操作符
36 0
深入理解算数运算符
深入理解算数运算符
68 0