在python中打印数字的位表示

时间:2022-10-25 00:12:14

I want to print the bit representation of numbers onto console, so that I can see all operations that are being done on bits itself.

我想将数字的位表示形式打印到控制台,这样我就可以看到所有的操作都是在比特本身上完成的。

How can I possibly do it in python?

用python怎么做呢?

5 个解决方案

#1


58  

This kind of thing?

这种事情吗?

>>> ord('a')
97
>>> hex(ord('a'))
'0x61'
>>> bin(ord('a'))
'0b1100001'

#2


26  

In Python 2.6+:

在Python 2.6 +:

print bin(123)

Results in:

结果:

0b1111011

In python 2.x

在python 2. x

>>> binary = lambda n: n>0 and [n&1]+binary(n>>1) or []
>>> binary(123)
[1, 1, 0, 1, 1, 1, 1]

Note, example taken from: "Mark Dufour" at http://mail.python.org/pipermail/python-list/2003-December/240914.html

注意,这个例子来自:http://mail.python.org/pipermail/python-list/2003-December/240914.html

#3


20  

From Python 2.6 - with the string.format method:

来自Python 2.6——带有字符串。格式的方法:

"{0:b}".format(0x1234)

in particular, you might like to use padding, so that multiple prints of different numbers still line up:

特别地,您可能喜欢使用填充,以便不同数字的多个打印仍然保持一致:

"{0:16b}".format(0x1234)

and to have left padding with leading 0s rather than spaces:

并留下带前导的0而不是空格的填充:

"{0:016b}".format(0x1234)

From Python 3.6 - with f-strings:

Python 3.6 - f-string:

The same three examples, with f-strings, would be:

同样的三个f-string例子是:

f"{0x1234:b}"
f"{0x1234:16b}"
f"{0x1234:016b}"

#4


3  

The bin function

本函数

#5


1  

Slightly off-topic, but might be helpful. For better user-friendly printing I would use custom print function, define representation characters and group spacing for better readability. Here is an example function, it takes a list/array and the group width:

有点偏离主题,但可能会有帮助。为了更好的用户友好的打印,我将使用自定义打印函数,定义表示字符和组间距以更好的可读性。这是一个示例函数,它取列表/数组和组宽度:

def bprint(A, grp):
    for x in A:
        brp = "{:08b}".format(x)
        L=[]
        for i,b in enumerate(brp):
            if b=="1":
                L.append("k")
            else: 
                L.append("-")
            if (i+1)%grp ==0 :
                L.append(" ")

        print "".join(L) 

#run
A = [0,1,2,127,128,255]
bprint (A,4)

Output:

输出:

---- ----
---- ---k
---- --k-
-kkk kkkk
k--- ----
kkkk kkkk

#1


58  

This kind of thing?

这种事情吗?

>>> ord('a')
97
>>> hex(ord('a'))
'0x61'
>>> bin(ord('a'))
'0b1100001'

#2


26  

In Python 2.6+:

在Python 2.6 +:

print bin(123)

Results in:

结果:

0b1111011

In python 2.x

在python 2. x

>>> binary = lambda n: n>0 and [n&1]+binary(n>>1) or []
>>> binary(123)
[1, 1, 0, 1, 1, 1, 1]

Note, example taken from: "Mark Dufour" at http://mail.python.org/pipermail/python-list/2003-December/240914.html

注意,这个例子来自:http://mail.python.org/pipermail/python-list/2003-December/240914.html

#3


20  

From Python 2.6 - with the string.format method:

来自Python 2.6——带有字符串。格式的方法:

"{0:b}".format(0x1234)

in particular, you might like to use padding, so that multiple prints of different numbers still line up:

特别地,您可能喜欢使用填充,以便不同数字的多个打印仍然保持一致:

"{0:16b}".format(0x1234)

and to have left padding with leading 0s rather than spaces:

并留下带前导的0而不是空格的填充:

"{0:016b}".format(0x1234)

From Python 3.6 - with f-strings:

Python 3.6 - f-string:

The same three examples, with f-strings, would be:

同样的三个f-string例子是:

f"{0x1234:b}"
f"{0x1234:16b}"
f"{0x1234:016b}"

#4


3  

The bin function

本函数

#5


1  

Slightly off-topic, but might be helpful. For better user-friendly printing I would use custom print function, define representation characters and group spacing for better readability. Here is an example function, it takes a list/array and the group width:

有点偏离主题,但可能会有帮助。为了更好的用户友好的打印,我将使用自定义打印函数,定义表示字符和组间距以更好的可读性。这是一个示例函数,它取列表/数组和组宽度:

def bprint(A, grp):
    for x in A:
        brp = "{:08b}".format(x)
        L=[]
        for i,b in enumerate(brp):
            if b=="1":
                L.append("k")
            else: 
                L.append("-")
            if (i+1)%grp ==0 :
                L.append(" ")

        print "".join(L) 

#run
A = [0,1,2,127,128,255]
bprint (A,4)

Output:

输出:

---- ----
---- ---k
---- --k-
-kkk kkkk
k--- ----
kkkk kkkk