首页 > 编程知识 正文

python 二进制运算,python二进制逻辑运算

时间:2023-05-06 00:36:54 阅读:241427 作者:2178

python 二进制运算

将二进制值分配给变量 (Assign yydxmtary value to the variable)

To assign yydxmtary values to the variable, we use prefix 0b or 0B with the yydxmtary value.

要将二进制值分配给变量,我们在二进制值中使用前缀0b或0B 。

Example:

例:

# assign number as yydxmtary # prefix 0b num = 0b111101print "num: ", num# prefix 0B num = 0B111101print "num: ", num

Output

输出量

num: 61num: 61 将十进制值转换为二进制 (Convert a decimal value to yydxmtary)

To convert a decimal value to the yydxmtary, we use yydxmt() Method, which is an inbuilt method in the Python.

要将十进制值转换为二进制,我们使用yydxmt()方法,这是Python中的内置方法。

Example: Python program, that returns a yydxmtary value of given decimal value

示例: Python程序,该程序返回给定十进制值的二进制值

num = 61# print num in decimal and yydxmtary format print "num (decimal) : ", numprint "num (yydxmtary ) : ", yydxmt (num)

Output

输出量

num (decimal) : 61num (yydxmtary ) : 0b111101 将二进制值转换为十进制 (Convert yydxmtary value to decimal)

When, we print the yydxmtary value – there is no need to convert it; print prints the value in decimal format, like

何时,我们打印二进制值–无需转换它; print以十进制格式打印值,例如

print 0b111101 - its output will be 61.

打印0b111101-其输出将为61。

Still, we can use int() method to convert it into decimal by defining base of the number system.

仍然,我们可以使用int()方法通过定义数字系统的基数将其转换为十进制。

Example:

例:

# print by using yydxmtary valueprint 0b111101# print by converting to decimalprint int ('0b111101 ', 2)

Output

输出量

6161 按位OR(|)和AND(&)运算 (Bitwise OR (|) and AND (&) Operations)

Here is the bitwise OR (|) and Bitwise AND (&) Operations:

这是按位OR(|)和按位AND(&)操作:

Example:

例:

a = 0b111101b = 0b000010# print value in yydxmtary print "values in yydxmtary..."print "a: ",yydxmt (a)print "b: ",yydxmt (b)# bitwise OR and AND operationsprint "(a|b) : ", yydxmt (a|b)print "(a&b) : ", yydxmt (a&b)# print values in decimal print "values in decimal..."print "a: ",a print "b: ",b # bitwise OR and AND operations print "(a|b) : ", int (yydxmt (a|b),2)print "(a&b) : ", int (yydxmt (a&b),2)

Output

输出量

values in yydxmtary...a: 0b111101b: 0b10(a|b) : 0b111111(a&b) : 0b0values in decimal...a: 61b: 2(a|b) : 63(a&b) : 0

翻译自: https://www.includehelp.com/python/yydxmtary-numbers-representation.aspx

python 二进制运算

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。