将位转换为字节序列

发布于 2024-10-18 03:00:27 字数 172 浏览 1 评论 0原文

给定一个 4 位大小的 Python 整数,如何通过按位算术而不是字符串处理将其转换为 4 个字节大小的整数,其中原始中的每一位对应一个字节,即该位重复了 8 次?

例如:0b1011 应变为 0b11111111000000001111111111111111

Given a Python integer which is within the size of 4 bits, how does one transform it – with bitwise arithmetic instead of string processing – into an integer within the size of 4 bytes, for which each bit in the original corresponds to a byte which is the bit repeated 8 times?

For example: 0b1011 should become 0b11111111000000001111111111111111

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-10-25 03:00:27

向 ncoghlan 致歉:

expanded_bits = [
    0b00000000000000000000000000000000,
    0b00000000000000000000000011111111,
    0b00000000000000001111111100000000,
    0b00000000000000001111111111111111,
    0b00000000111111110000000000000000,
    0b00000000111111110000000011111111,
    0b00000000111111111111111100000000,
    0b00000000111111111111111111111111,
    0b11111111000000000000000000000000,
    0b11111111000000000000000011111111,
    0b11111111000000001111111100000000,
    0b11111111000000001111111111111111,
    0b11111111111111110000000000000000,
    0b11111111111111110000000011111111,
    0b11111111111111111111111100000000,
    0b11111111111111111111111111111111,
    ]

然后只需使用要转换的半字节索引此列表即可:

>>> bin(expanded_bits[0b1011])
"0b11111111000000001111111111111111"

With apologies to ncoghlan:

expanded_bits = [
    0b00000000000000000000000000000000,
    0b00000000000000000000000011111111,
    0b00000000000000001111111100000000,
    0b00000000000000001111111111111111,
    0b00000000111111110000000000000000,
    0b00000000111111110000000011111111,
    0b00000000111111111111111100000000,
    0b00000000111111111111111111111111,
    0b11111111000000000000000000000000,
    0b11111111000000000000000011111111,
    0b11111111000000001111111100000000,
    0b11111111000000001111111111111111,
    0b11111111111111110000000000000000,
    0b11111111111111110000000011111111,
    0b11111111111111111111111100000000,
    0b11111111111111111111111111111111,
    ]

Then just index this list with the nibble you want to transform:

>>> bin(expanded_bits[0b1011])
"0b11111111000000001111111111111111"
命比纸薄 2024-10-25 03:00:27

我只想做一个循环:

x = 0b1011
y = 0
for i in range(4):
    if x & (1 << i):
        y |= (255 << (i * 8))
print "%x" % y

I'd just do a loop:

x = 0b1011
y = 0
for i in range(4):
    if x & (1 << i):
        y |= (255 << (i * 8))
print "%x" % y
万劫不复 2024-10-25 03:00:27

以下递归解决方案仅使用加法、左/右移位运算符和按位 &整数运算符:

def xform_rec(n):
    if n == 0:
        return 0
    else:
        if 0 == n & 0b1:
            return xform_rec(n >> 1) << 8
        else:
            return 0b11111111 + (xform_rec(n >> 1) << 8)

或者,作为单行:

def xform_rec(n):
    return 0 if n == 0 else (0 if 0 == n & 0b1 else 0b11111111) + (xform_rec(n >> 1) << 8)

示例:

>>> print bin(xform_rec(0b1011))
0b11111111000000001111111111111111
>>> print bin(xform_rec(0b0000))
0b0
>>> print bin(xform_rec(0b1111))
0b11111111111111111111111111111111)

The following recursive solution uses only addition, left/right shift operators and bitwise & operator with integers:

def xform_rec(n):
    if n == 0:
        return 0
    else:
        if 0 == n & 0b1:
            return xform_rec(n >> 1) << 8
        else:
            return 0b11111111 + (xform_rec(n >> 1) << 8)

Or, as a one-liner:

def xform_rec(n):
    return 0 if n == 0 else (0 if 0 == n & 0b1 else 0b11111111) + (xform_rec(n >> 1) << 8)

Examples:

>>> print bin(xform_rec(0b1011))
0b11111111000000001111111111111111
>>> print bin(xform_rec(0b0000))
0b0
>>> print bin(xform_rec(0b1111))
0b11111111111111111111111111111111)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文