views:

164

answers:

5

How can i do a Bitwise OR on strings?

A:
10001
01010
------
11011

Why on strings? The Bits can have length of 40-50.Maybe this could be problematic on int ? Any Ideas ?

+3  A: 

You should take a look at the C++ std::bitset class, which does exactly what you want.

anon
http://www.sgi.com/tech/stl/bitset.html
RickNotFred
+1  A: 

Why not just use a vector of int values? Doesn't the bitset still use a byte per bit?

You can also use a vector of bool values, but this is also implementation specific.

Depending on whether you need storage efficiency or speed (or the utility of container methods that a couple of these approaches lack) you might profile to decide which approach to use.

Alex Reynolds
No, it doesn't. Also, answers should not be questions.
anon
Isn't `bitset` implementation specific?
Alex Reynolds
@Alex No, it isn't.
anon
After a brief Google search, others seem to claim that bitset is not part of the STL standard, and that it does not follow STL container conventions. I'm sure it is a workable option, though.
Alex Reynolds
Others can claim away - it is part of the C++ Standard - see section 23.3.5 describing it.
anon
@Neil @Alex bitset is part of the C++ Standard but as far as I can tell there's no specification of how it's stored, using a byte (or even an int) per bit seems legit.
Andreas Brinck
@andreas The storage is not specified, but no-one would use the thing if it did not in fact store individual bits. That's one reason why they are fixed-sized.
anon
I have used vectors of `int` and `bool` values in the past, but the use of `bool` vectors is also implementation specific. So probably the best thing to do is profile different approaches, and use whichever is faster or more memory efficient for that platform.
Alex Reynolds
@Alex I think you are using a specialmeaning of the term "implementation specific". vector <bool> is also standardised (unfortunately, perhaps).
anon
+1  A: 

For each char:

char result = (a - '0') | (b - '0') + '0';

Where a and b are two chars with ascii character 0 or 1 in them.

Andreas Brinck
+7  A: 

I would say std::bitset is more than enough for your situation, but for more flexibility you can use boost::dynamic_bitset. Here is an example on std::bitset:

const size_t N = 64;
string a_str = "10001", b_str = "01010";
bitset<N> a(a_str), b(b_str);
bitset<N> c = a | b;

cout << c;
AraK
A: 

This is similar to Andreas Brinck's answer, only it returns a full output string and can compare strings of different (arbitrary) lengths.

Example in C# (not near c++ compiler right now), but it should be simple to convert it to a language of your choice.

public static string BitwiseOr(string input1, string input2)
{
    char[] inarr1 = (char[])input1.ToCharArray().Reverse().ToArray();
    char[] inarr2 = (char[])input2.ToCharArray().Reverse().ToArray();
    char[] outarr = new char[input1.Length > input2.Length ? input1.Length : input2.Length];

    for (int i = 0; i < outarr.Length ; i++)
    {
        char c1 = i < input1.Length ? inarr1[i] : '0';
        char c2 = i < input2.Length ? inarr2[i] : '0';
        outarr[i] = (char)((c1 - '0') | (c2 - '0') + '0');
    }

    return new string((char[])outarr.Reverse().ToArray());
}

Of course this is only valid if you really need it to be in a string, if not you should (as suggested in other answers) use a vector or similar data type.

Rekreativc
This has the disadvantage of not being C++.
anon