473,836 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iterating bit-by-bit across int?

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?
Jul 18 '05
12 11762
Anton Vredegoor wrote:

la*****@my-deja.com (John Ladasky) wrote:
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?


Using your approach, you would first need to disassemble the integer,
then reassemble it. You can cut this bitwise cranking in half.
Define an integer, in which 10% of the bits is a "1". Then do an
exclusive-or operation between this integer and the one you wish to
mutate.


This creates a new problem which is interesting in itself. How to
create this integer?


I would think the simplest approach is (similar to a suggestion
already made about the original problem) to predefine a set of bitmasks,
each with only one bit set, then simply select one or more at random
and add (or OR) them together, then XOR the result with the original.

You could either iterate over the list of bitmasks, checking a
random result at each step to decide whether to include that bitmask,
or you could just random.shuffle( ) the list, then select a random
number of entries from the start of the list. Using the new sum()
(or a filter() with operator.add) would make the whole thing as
simple as a couple of function calls:

genome = 12345678
numMutations = random.randint( 0, 4)
mutant = genome ^ sum(random.shuf fle(bitmasks)[:numMutations])

# done!!

The algorithm used to select the number of mutations to make could
of course be more sophisticated than the above...

-Peter
Jul 18 '05 #11
Brian Kelley <bk*****@wi.mit .edu> wrote:
Paul Rubin wrote:

Long ints can have as many bits as you want.


Such as -1L which has an infinite number of bits.


Oh come on, that's just silly. If that were true, I could not type this at
a Python prompt because it would cause an overflow:

i = -1L

-1L, in fact, has exactly the same number of bits as 1L and, probably,
1048576L.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #12
Tim Roberts wrote:
Brian Kelley <bk*****@wi.mit .edu> wrote:

Paul Rubin wrote:
Long ints can have as many bits as you want.


Such as -1L which has an infinite number of bits.

Oh come on, that's just silly. If that were true, I could not type this at
a Python prompt because it would cause an overflow:

i = -1L


try

i = -1L
while i:
i = i >> 1

and then repeat the statement :) My only point is that you have to be
careful with long ints.

Jul 18 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1823
by: Patrick von Harsdorf | last post by:
I want to iterate over a collection and delete all unwanted entries. for item in collection: del item of course doesn´t do anything useful. Two things come to mind: a) iterating backwards over the collection using indexing, something like: for i in range(len(collection)-1, -1, -1): item = collection
4
4543
by: Fernando Rodríguez | last post by:
Hi, While iterating through a list I'd like to know not just the current element, but also its index. Is there a better way than this: i = 0 newList = for element in aList: newList.append((i, element)) i += 1
0
1266
by: ischenko | last post by:
Hi, I'm trying to find all modules that contain doctests and execute them (using DocTestSuite). The problem is how to iterate (programmatically) through the package's modules. >>> import M >>> inspect.getmembers(M, inspect.ismodule)
4
4302
by: Paxton | last post by:
At the risk of being told "If it ain't broke, don't fix it", my code works, but is ugly. Part of the admin site I'm working on at the moment includes a facility for users to enter Formulations (recipes for making cosmetics etc) in 3 stages: Stage 1: basic info such as title, method etc and number of Phases (steps in recipe). Stage 2: dynamically generated form containing the exact number of phases as textboxes, depending on the number...
7
4465
by: jose luis fernandez diaz | last post by:
Hi, Is this right any stl container (vector, deque, list, . . .)? typedef vector container; int main() { container<int> c1;
7
1695
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a colleague did, got an unexpected result, and asked me why. I think I can infer what is occurring, and I was able to find a simple work-around. But I thought I'd ask about it anyway. I've been pushing Python at work for use as a scripting...
6
6067
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I try to modify the objects in the array while iterating through it. I marked the bugs in this code: // Loop through all previously added accounts foreach(Account a in a1) // a1 is an ArrayList { // If name and context is the same if(a.Name ==...
2
2169
by: Nick | last post by:
Hi all, Just a quick question. I have a class that exposes a number of fields (which are themselves custom types) through public properties. At run time, I have an object whom I'd like to check, is of same type as one of these fields. Is there a method or technique for iterating through a class's public properties, such as maybe using something from the reflection namespace?
5
12169
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: for each element of the vector for each subsequent element of the vector if the belong together <some code to compare them> then merge the elements (add the 2nd to the 1st, then delete the 1st)
0
1053
by: SBMpy | last post by:
Instead of itereating through my half-million plus records, I'm looking for a way to query the records and return a subset of records. Q: Does dbfpy have that functionality? Q: Or is there a means of performing a query that returns a subset of records without iterating over every record? Currently I load all the records into an array: for rec2 in db2: rList.append(rec2)
0
9656
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10527
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10571
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9358
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.