-1

how to do bitwise xor in strings? can anyone help me with this...

#include <bits/stdc++.h>
using namespace std;

int main() {
    // your code goes here

    string a="1001";
    string b="1111";
    string c=a^b;
    cout << "c: " << c << "\n";
    return 0;
}

error:no match for ‘operator^’ (operand types are ‘std::__cxx11::string {aka std::__cxx11::basic_string}’ and ‘std::__cxx11::string {aka std::__cxx11::basic_string}’) string c=a^b;

songyuanyao
  • 147,421
  • 15
  • 261
  • 354
horliks
  • 13
  • 2
  • 3
    Are you sure you want to apply a bitwise xor to 2 strings? – byxor Mar 02 '17 at 05:51
  • std::string doesn't overload ^ but you can always write one. However, first consider what you want it to do. For instance what do you expect something like `'0' ^ '1'` which is a legal expression, to yield? – doug Mar 02 '17 at 05:52
  • 1
    What would such an operator do if the characters weren't just 0 and 1? – chris Mar 02 '17 at 05:53
  • 2
    @horliks You can try bitset STL container to manipulate bits. – Ayush Mar 02 '17 at 05:53
  • the result of xor operation is actually valid for numbers and not for characters. string can be seen as sequence of characters. so applying xor on corresponding characters of 2 strings might produce a character which would be invalid from string point of view. – sameerkn Mar 02 '17 at 05:56
  • I agree with @Ayush: `std::bitset` is probably the simplest choice here. For example: `std::cout << (std::bitset<4>(a) ^ std::bitset<4>(b));` (note: due to the precendence of `< – Jerry Coffin Mar 02 '17 at 06:02
  • [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Together you invite epic disasters. – user4581301 Mar 02 '17 at 06:18

3 Answers3

5

Consider to use std::bitset, which may be what you are looking for.

std::bitset<4> a("1001");
std::bitset<4> b("1111");
std::bitset<4> c = a ^ b;
cout << "c: " << c << "\n";

See it in ideone

They can be initialized from your bits strings, and have the operator^ overloading to do the XOR operation. Also there is a ostream& operator<<(ostream&, const bitset<N>&) for printing result into std::cout.

Chen OT
  • 2,945
  • 2
  • 19
  • 38
0

I think you're meaning to use binary fields instead of strings of characters. Check out this post if you want to use binary literals.

You're probably looking to do this:

int getBit(int n, int k)
{
    return (n & (1<<k)) != 0;
}
int main() 
{
  int a = 0b1001;
  int b = 0b1111;
  int c = a^b;
  for(int i = 3; i >= 0; i++)
       cout << getBit(c, i);

  return 0;
}
Community
  • 1
  • 1
JGroven
  • 603
  • 1
  • 9
  • 14
0

how to do bitwise xor in strings?

bitwise operators can be used only with integral types.

You'll have to extract the digits from the strings, convert them into integral types, perform the bitwise operations on them, and create a new string from them.

string a="1001";
string b="1111";
string c;

// With all the details spelled out.
for ( int i = 0; i < 4; ++i )
{
    char ac = a[i];
    char bc = b[i];

    // You should not use ac ^ bc even though char is an
    // integral type because the ^ will be performed on the
    // integral value used to encode the character.
    // Hence, you need to convert the char '0' to the number 0.

    int ai = ac - '0';
    int bi = bc - '0';
    int ci = ai ^ bi;
    char cc = ci + '0';
    c += cc;
}

// with all the details inlined
for ( int i = 0; i < 4; ++i )
{
    c += ((a[i] - '0') ^ (b[i] - '0')) + '0';
}
R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • Well, characters *are* an integral type, and in general, performing a straight xor between two strings does have its applications - think cryptography. Of course I'm quite sure that it's not what OP wants. – Matteo Italia Mar 02 '17 at 06:17
  • @Matteo chars are promoted to integral types automatically but aren't integral types IIRC. – doug Mar 02 '17 at 06:25
  • 1
    @doug, `char` is an integral type. From the C++11 standard 3.9.1/7: Types `bool`, `char`, `char16_t`, `char32_t`, `wchar_t`, and the signed and unsigned integer types are collectively called *integral* types. – R Sahu Mar 02 '17 at 06:42
  • @RSahu My error. Their ambiguity is whether they are signed or unsigned integral types. – doug Mar 02 '17 at 16:03