1

I have two char arrays A and B. I want to perform bitwise xor on these two arrays. But the output shows a garbage value. Where am I going wrong?

I tried typecasting the output in char since array A and array B contains 0 and 1 in ASCII.But it didn't work.

#include<iostream>
#include<cstdlib>

using namespace std;

int main(){
    char A[4] = {0,1,1,0}; 
    char B[4] = {1,1,1,0};
    char XOR[4];
    cout<< " PRINTING "<<endl;
    for(int i =0; i<4; i++)
    {
        XOR[i] = (char)(A[i]^B[i]);
        cout<<(char)XOR[i];
    }
    cout<<endl;

}

Expected output is 1000, but the output I get is a garbage.

Claudio
  • 10,614
  • 4
  • 31
  • 71
  • 1
    Printable ASCII characters start at 32. You are trying to print ASCII characters 0 and 1. To output the values as integers, convert them to e.g. `int`. – Max Langhof May 17 '19 at 07:56
  • 1
    `XOR[i]`'s type is already `char`. Casting it to `char` isn't doing anything. – chris May 17 '19 at 07:57
  • I don't think the duplicate mark is appropriate in this case. – YesThatIsMyName May 17 '19 at 08:02
  • @YesThatIsMyName Can you explain why you think so? The solution is the exact same, and the duplicate also tries to print "abnormal" ASCII characters (above 127) and expects to get numerical output instead (in hex for some reason, which admittedly none of the answers look into). The underlying issue is still "`char` is printed as ASCII char, not integer". If you can find a better duplicate let me know though, I'm sure there are at least 10 more. And with the answers here we got yet another duplicate. – Max Langhof May 17 '19 at 08:13
  • @MaxLanghof Because he uses char doesn't mean it's right using chars ... char is just the wrong type for numbers. In my oppinion he just wasn't aware of this. Thats why my answer tells him to use integer arrays ... and this changes the whole topic and has nothing more to do with printing ASCII characters as integers. Before we really know why he is using char, we can't say its a duplicate. – YesThatIsMyName May 17 '19 at 08:35
  • @YesThatIsMyName I disagree. `char` is very much an integer type and no different than `short`, `int` etc. in these regards. The only part of the code that treats `char` as a printable character and not an integer is the particular `operator<<` that streams into `cout`. Using an `int` as you recommend means you quadruple your memory requirements for no gain (compared to just properly streaming `char`s as numbers). It's certainly not wrong to do bitwise `xor` on `char`, and `char` is not reserved exclusively for representing ASCII characters. – Max Langhof May 17 '19 at 08:43
  • @MaxLanghof Using chars with C++ is kind of uncommon. Also I can argument with execution time .. that is faster using integers on some C++ compilers than using chars. So there is some sort of gain, even if minimal. – YesThatIsMyName May 17 '19 at 08:48
  • @YesThatIsMyName If you want to argue execution time then a good compiler will emit exactly one xor for these four operations in the question if you operate on `char`: https://godbolt.org/z/Q-qmgQ. It can do that because it's only 32 bits in total. To `xor` four `int`s at once you would need vectorization support. But yes, _some_ (less common) target platforms are slower to operate on `char` than `int`, so there's no unique winner here. I don't see that as favoring `int`. – Max Langhof May 17 '19 at 08:53
  • @MaxLanghof Agree ;-) – YesThatIsMyName May 17 '19 at 08:54
  • @YesThatIsMyName _"Using chars with C++ is kind of uncommon"_ Untrue in my experience. And it doesn't change the fact that `char` is a plain integer type for all the standard cares. – Max Langhof May 17 '19 at 08:55
  • @MaxLanghof In my expecrience `uint8_t` is the type most commonly used for such things. – YesThatIsMyName May 17 '19 at 09:10
  • "*since array A and array B contains 0 and 1 in ASCII*". This is not the case. They just contain the numbers zero and one. – David Schwartz May 17 '19 at 11:08
  • Can you do this: `cout<<(XOR[i] ? '1' : '0');` – Eljay May 17 '19 at 11:58

2 Answers2

1

The streaming operators for char treat the data as characters, not as numbers. If you want to print them as numbers, you have to cast them to a number for printing:

cout<< static_cast<int>(XOR[i]);
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

One solution is to use integer arrays to make it work as you wish. A char is a type for "letters", not for numbers.

Here is one possible solution:

#include<iostream>
#include<cstdlib>

using namespace std;

int main(){
    int A[4] = {0,1,1,0}; 
    int B[4] = {1,1,1,0};
    int XOR[4];

    cout << " PRINTING "<< endl;

    for(int i=0; i<4; i++)
    {
        XOR[i] = A[i]^B[i];
        cout<<XOR[i];
    }
    cout<<endl;

}
YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30