Page 1 of 1

How would you do it?

Posted: Sat Jun 02, 2007 9:07 pm
by Zacariaz
(C++ preferably)
This is pretty complicated for me to explain; maybe ill fail, but ill give it a try.

i have a number of bitsets, all the same size.
I want to OR them all and store the result in another bitset, still the same size.
I know for a fact that that OR and XOR will give the same result. AND will yield zero.
e.g. no two bitsets will have an active bit at the same position.

Anyway, it is easy to just make a function to do the task, but i keep thinking that there must be some clever trick.

First i thought about something like an union of pointers, then i thought again banged my head against the floor. :roll:
Somehow i got the idea that if i could get a variable to point to more than one place in memory it would give me the result i needed.
Be aware, sometimes weird thoughts happens.

some code:

Code: Select all

std::bitset<64> b1, b2, b3,b4;
std::bitset<64> b_or_all
void OR_THEM() {
  b_or_all = b1 | b2 | b3 | b4;
}
I know this is some crappy code (or atleast i think it is), but it should explain it.

The thing is that "b_or_all" will have to be up to date at all times, and b1, b2, ... will be altered constantly, and so i will have to call "OR_THEM" constantly. that bothers me, so i ask you (the experts) how it can be done different, and it wouldnt hurt if i didnt need to worry about calling some function all the time.


This isnt one of my better posts and it deffinently reveals a huge gap in my programmig skills, but i hope you can help anyway.

Posted: Sun Jun 03, 2007 8:10 am
by Zacariaz
ok, since nobody have answered i must aume that either you have a hard time understanding me or you cant figure out an answer. Thats life, but my guess would be that i would have to mess around with some templates, however i have yet to find anthing on tha net that i fully understand, maybe you can help with that?

Re: How would you do it?

Posted: Sun Jun 03, 2007 2:06 pm
by Brendan
Hi,

There are only 2 alternatives that I'm aware of...

The first alternative is to recalculate "b_or_all" every time any of the other bitsets change (like you've been doing). This is a good idea if the other bitsets don't change often and "b_or_all" is read frequently.

The alternative option is to only recalculate "b_or_all" when it's needed. When any of the other bitsets change it doesn't matter, but when you need to read "b_or_all" you'd need to call a "get_b_or_all" function that does something like "return b1 | b2 | b3 | b4;". This could possibly also be a macro if your compiler won't inline it. This is a good idea if the other bitsets change often and "b_or_all" isn't read frequently.

The last "alternative" (which technically isn't an alternative because it doesn't solve your original problem as stated) is to redesign your code so it doesn't need "b_or_all" - i.e. change the algorithm/s used to something else. For example, perhaps it'd make more sense to use a single "unsigned char myStates[64]" instead of "b_or_all, b1, b2, b3 and b4". Of course without knowing anything about what you're using the bitsets for it's almost impossible to suggest anything that's likely to be useful.


Cheers,

Brendan

Posted: Sun Jun 03, 2007 2:20 pm
by Zacariaz
ok, so it seems there is no real alternative, as the bitsets will be altered alot, and "or_them_all" will be read continuesly.

anyway, thanks for the reply...

Posted: Mon Jun 04, 2007 11:39 am
by Candy
Zacariaz wrote:ok, so it seems there is no real alternative, as the bitsets will be altered alot, and "or_them_all" will be read continuesly.

anyway, thanks for the reply...
If they're mutually exclusive, why not make a special class that wraps all of them? One that overrides operator[] with the type that you want to check and so on, proxy if you want to modify it... The vector<bool> idea all over again?

Posted: Mon Jun 04, 2007 12:29 pm
by Zacariaz
That is definently not a bad idea, allthough i would prefer to use bitset.
And again, my progamming skills are somewhat limited, but you did give me something to think about.