快速计算更改最左边的位
鉴于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所追求的函数:
输出:
函数执行以下操作:
x
和y
的XOR结果,其中将包括最重要的位,它们与之不同最重要的位是1log2
的floor
,并提高2
到该功能是否设置为1X
和该号码,翻转相关位The function you're after:
Output:
The function does the following:
x
andy
, which will include the most significant bit where they differ as the most significant bit to be 1floor
of thelog2
of that value, and raising2
to that power, to get a number that only has that bit set to 1x
and that number, flipping the relevant bit