快速计算更改最左边的位

发布于 2025-02-12 15:10:15 字数 918 浏览 2 评论 0原文

鉴于8位中的两个数字:

x = 0b11110111
y = 0b11001010

我想做的是比较x和y,然后根据y更改x最初的最左侧位。例如:

z = 0b11010111 (Because the leftmost different bit between x and y is in the third place, therefore, change the third bit in x based on y and other remain the same.)

我的代码是:

flag = True
for i in range(8):
    if flag and x[i] != y[i]: # Change only the left-most different bit.
        flag = False
    else:
        y[i] = x[i] # Otherwise,  remain the same.

这可以找到。

buit问题是如果我有很多对:

for (x, y) in nums:
    flag = True
    for i in range(8):
        if flag and x[i] != y[i]: # Change only the left-most different bit.
            flag = False
        else:
            y[i] = x[i] # Otherwise,  remain the same.

当nums大时,那么这个过程真的很慢。 那么如何改善问题的过程呢?

顺便说一句,这是深度学习任务的项目,因此它可以在GPU上运行,但是我不知道它是否可以与GPU平行。

Given the two number in 8-bit:

x = 0b11110111
y = 0b11001010

What I want to do is to compare x and y and change x only the first different leftmost bit based on y. For example:

z = 0b11010111 (Because the leftmost different bit between x and y is in the third place, therefore, change the third bit in x based on y and other remain the same.)

And my code is:

flag = True
for i in range(8):
    if flag and x[i] != y[i]: # Change only the left-most different bit.
        flag = False
    else:
        y[i] = x[i] # Otherwise,  remain the same.

This could work find.

Buit the problem is if I have many pairs like:

for (x, y) in nums:
    flag = True
    for i in range(8):
        if flag and x[i] != y[i]: # Change only the left-most different bit.
            flag = False
        else:
            y[i] = x[i] # Otherwise,  remain the same.

When nums is large, then this process will be really slow.
So how can I improve the process of the problem?

BTW, this is the project of the deep learning task, so it can run on GPU, but I don't know whether it can be paralleled by GPU or not.

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

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

发布评论

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

评论(1

对你再特殊 2025-02-19 15:10:15

您所追求的函数:

from math import floor, log2


def my_fun(x, y):
    return x ^ (2 ** floor(log2(x ^ y)))


z = my_fun(0b11110111, 0b11001010)
print(f'{z:b}')

输出:

11010111

函数执行以下操作:

  • 计算xy的XOR结果,其中将包括最重要的位,它们与之不同最重要的位是1
  • 计算该值的log2floor,并提高2到该功能是否设置为1
  • 返回XOR X和该号码,翻转相关位

The function you're after:

from math import floor, log2


def my_fun(x, y):
    return x ^ (2 ** floor(log2(x ^ y)))


z = my_fun(0b11110111, 0b11001010)
print(f'{z:b}')

Output:

11010111

The function does the following:

  • compute the XOR result of x and y, which will include the most significant bit where they differ as the most significant bit to be 1
  • compute the floor of the log2 of that value, and raising 2 to that power, to get a number that only has that bit set to 1
  • return the XOR of x and that number, flipping the relevant bit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文