在int中逐位迭代? [英] iterating bit-by-bit across int?
问题描述
我正在玩遗传算法,我想写一个
函数,通过遍历这些位来改变一个整数,并且关于
1 in 10时间,它应该将零切换为1,或者将1切换为零。
我不确定python整数中有多少位。图书馆
参考说至少32个。
布尔列表;然后我可以迭代它,并在那里更改值
。但在我这样做之前,有没有人有更好的主意?
I''m playing around with genetic algorithms and I want to write a
function that mutates an integer by iterating across the bits, and about
1 in 10 times, it should switch a zero to a one, or a one to a zero.
I''m not sure how many bits are inside a python integer. The library
reference says at least 32.
I''m thinking about writing a function that eats integers and poops out
lists of bools; and then I can iterate through that, and change values
in there. But before I do that, does anyone have a better idea?
推荐答案
Matthew Wilson< mw ***** @ sarcastic-horse.com> ;写道:
Matthew Wilson <mw*****@sarcastic-horse.com> writes:
我正在玩遗传算法,我想写一个
函数,通过遍历位来改变一个整数,并且关于
1次10次它应该将零切换为1,或者将1切换为零。
我不确定python整数中有多少位。图书馆
参考说至少32美元。
长期投资可以有你想要的任意数量。
我正在考虑写作一个吃整数和大便的函数
bool列表;然后我可以迭代它,并在那里改变值
。但在我这样做之前,有没有人有更好的想法?
I''m playing around with genetic algorithms and I want to write a
function that mutates an integer by iterating across the bits, and about
1 in 10 times, it should switch a zero to a one, or a one to a zero.
I''m not sure how many bits are inside a python integer. The library
reference says at least 32.
Long ints can have as many bits as you want.
I''m thinking about writing a function that eats integers and poops out
lists of bools; and then I can iterate through that, and change values
in there. But before I do that, does anyone have a better idea?
只需移动并屏蔽。未经测试的代码:
def bit_stream(n):
p = 1
而p < n:
位=(n& p)!= 0
如果rand()%10 == 0:
bit = not bit
p = p * 2
产量位
以上假设你要按顺序查看这些位,所以它
并没有尝试在数字内部更改它们,这意味着每次有点变化时都会使用新数字。如果你想同时看一下它们,你想要制作一个bool列表并翻转一个子集
是合理的。对于长整数的优化将使用
数组模块,将整数转换为数组,在数组中执行一堆
翻转,然后转换回来。 br />
Just shift and mask. Untested code:
def bit_stream(n):
p = 1
while p < n:
bit = (n & p) != 0
if rand() % 10 == 0:
bit = not bit
p = p * 2
yield bit
The above assumes you want to look at the bits sequentially, so it
doesn''t try to change them inside the number, which would mean consing
up a new number every time a bit changes. If you want to look at them
all at once, your idea of making a list of bools and flipping a subset
of them is reasonable. An optimization for long ints would be use the
array module, convert your integer to an array, do a bunch of bit
flips in the array, and convert back.
>我正在考虑编写一个吃整数的功能,然后选择
> I''m thinking about writing a function that eats integers and poops out
bools列表;然后我可以迭代它,并在那里改变值
。但在我这样做之前,有没有人有更好的想法?
lists of bools; and then I can iterate through that, and change values
in there. But before I do that, does anyone have a better idea?
对于速度,你应该使用shift和boolean ops - 像这样:
>
def mutate(seq,n = 32,prob = 0.05):
表示xrange(n)中的位:
if random.random()< ; = prob:
seq ^ = 1<<位
返回seq
问候,
Diez
For speed, you should use shift and boolean ops - like this:
def mutate(seq, n=32, prob=0.05):
for bit in xrange(n):
if random.random() <= prob:
seq ^= 1 << bit
return seq
Regards,
Diez
Matthew>我正在玩遗传算法,我想写一下
Matthew>通过遍历
Matthew>迭代整数的函数。比特,大约1到10次,它应该将零切换为
Matthew>一个,或一个零到零。
只需使用Python的按位运算,例如:
Matthew> I''m playing around with genetic algorithms and I want to write
Matthew> a function that mutates an integer by iterating across the
Matthew> bits, and about 1 in 10 times, it should switch a zero to a
Matthew> one, or a one to a zero.
Just use Python''s bitwise ops, for example:
x = 0x0FFFCCCC
hex(x)
''0xfffcccc''hex(x |(1<< 13))
x = 0x0FFFCCCC
hex(x) ''0xfffcccc'' hex(x | (1 << 13))
''0xfffeccc''
跳过
''0xfffeccc''
Skip
这篇关于在int中逐位迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!