John Cochran skrev:
Some time it's strange that one can't by taken by word and believed ...
I'm working with genetic algorithms, and have implemented it in C. in
order to work one need to perform crossover and bitflip operations on
encoded data.
crossover - take two values represented by binary string and, "cast a
coin" and exchange part of those strings -> bitmask operation
bitflip - cast a coin and flip bit in that place.
reason one:
since this is not forum for genetic algorithms I didn't bother to
explain earlier, and it would take more than this little peace to
explain in details what I'm doing
reason two:
because in the I have been shouted and treat malicious then I have
posted something that wasn't related to ansi c. and underlying problem
isn't so I just posted only what are my intentions to do.
OK, I'm snipping everything that looks like you're getting upset and/or
insulting or thinking you're being insulted.
If I understand you correctly, you need to do bit operations on something
larger than what you can get using integers and you think that by using
floating point numbers, you'll somehow succeed.
Problem: Bit operations on floating point numbers ARE NOT DEFINED AND WILL NOT WORK.
Period. End of discussion. Case closed.
Additionally, doing said operations on floating point numbers will not get you
more bits than what you can get by using integral types. To demonstrate, execute
the following short program.
#include <stdio.h>
int main(void)
{
printf("short = %d\n", (int)sizeof(short));
printf("int = %d\n", (int)sizeof(int));
printf("long = %d\n", (int)sizeof(long));
printf("long long = %d\n", (int)sizeof(long long));
printf("float = %d\n", (int)sizeof(float));
printf("double = %d\n", (int)sizeof(double));
printf("long double = %d\n", (int)sizeof(long double));
return 0;
}
Note: Some implementations of C will not support long long or long double. If so,
just ommit those lines that mention it.
The numbers generated by this simple program is simply the number of bytes each
type takes up. Multiply by 8 to get the number of bits available for manipulation.
I would not be surprised if the output of the above program is something like
2
4
4
8
4
8
10
As you can see, none of the above numbers is really large and is likely to be
unsuitable for genetic algorithms assuming you need more than about 64 bits.
So since none of the primitive data types available is suitable for you, you'll
have to use a composite data type and make functions to manipulate them. I'd
suggest that you use as your basic component an array in integral values with the
actual integral type dependent on the native word size of your computer. The type
"unsigned int" comes to mind as a reasonable start. Then write some functions
designed to create/destroy/manipulate these arrays.
For example:
(Error checking omitted for clarity. Code untested....)
#include <stdlib.h>
typedef struct {
size_t len;
unsigned int *array;
} BIT_ARRAY;
BIT_ARRAY *create_ba(size_t size)
{
BIT_ARRAY *p;
p = malloc(sizeof(*p));
p->len = size;
p->array = calloc(sizeof(unsigned int), p->len);
return p;
}
void create_ba(BIT_ARRAY *p)
{
free(p->array);
free(p);
}
BIT_ARRAY *and_ba(BIT_ARRAY *a, BIT_ARRAY *b)
{
size_t len,x;
BIT_ARRAY *p;
len = (a->len < b->len) ? a->len : b->len;
p = create_ba(len);
for(x=0; x<len; ++x) {
p->array[x] = a->array[x] & b->array[x];
}
return p;
}
etc. etc. etc.
Sorry, but this is the best that you can do. And if someone on this group says
that what you're doing doesn't make sense, it doesn't mean that they think you're
and idiot. It at worst means that you're ignorant about the language. Just remember:
Ignorance is curable, stupidity isn't.
It's OK to be ignorant if you're willing to be educated. If you're not willing to
be educated, then I have to assume that you're not ignorant, but instead you're
stupid. Don't be stupid.