-
Nov 26th, 2009, 09:33 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] XOR ^ operator not compiling...
Hi all,
this tiny little problem is frustrating the hell out of me. I've done this sort of operation many times in VB.NET with XOR without a problem, I believe the ^ operator in C# is XOR so I can't see why this is a problem(except that ^ is also considered a logical operator), but check it out. I simply want to XOR 2 bytes, and the compiler tells me that I cannot convert byte to int and that im missing a cast... how is this possible when i'm dealing only with bytes and the result of a XOR'd byte should be a byte.
code snippet
Code:
for(int i=0;i<bts.Length; i++)
bts[i] = bts[i] ^ bts2[i];
compiler error
Code:
Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) (CS0266) - .cs:80,14
which points to the "= bts[i]" part
Last edited by Phill64; Nov 26th, 2009 at 10:21 PM.
-
Nov 26th, 2009, 09:37 PM
#2
Thread Starter
Frenzied Member
Re: XOR ^ operator not compiling...
Sorry Nvm,
The solution was simply to use the shortcut assignment ^=
like so
Code:
for(int i=0;i<bts.Length; i++)
bts[i] ^= bts2[i];
Last edited by Phill64; Nov 26th, 2009 at 10:21 PM.
-
Nov 26th, 2009, 10:05 PM
#3
Re: [RESOLVED] XOR ^ operator not compiling...
It appears that the result of a bitwise XOR is an int, unless one of the operands is a long, in which case the result is also long.
Code:
byte b1 = 1;
byte b2 = 2;
var b3 = b1 ^ b2;
Console.WriteLine(b3.GetType().ToString());
-
Nov 26th, 2009, 10:17 PM
#4
Thread Starter
Frenzied Member
Re: [RESOLVED] XOR ^ operator not compiling...
Originally Posted by jmcilhinney
It appears that the result of a bitwise XOR is an int, unless one of the operands is a long, in which case the result is also long.
Code:
byte b1 = 1;
byte b2 = 2;
var b3 = b1 ^ b2;
Console.WriteLine(b3.GetType().ToString());
Which is odd isn't it? I think it must be something to do with ^ being listed as both a logical and bitwise operator in c# whereas XOR is only a bitwise operator in VB.NET though I still don't understand the purpose of this, I found some blogs etc that claim it's some sort of bug in C# though since I've never come accross a bug i nthe language before I find it hard to believe.
But, when trying this code, that theory sounds a bit more believable.. as you'll find this produces the same number which if they were intended to be different operations I would suspect a different result.. there's no reason for this result of 83 to be in int form.
Code:
byte b1 = 55;
byte b2 = 100;
int i1 = b1 ^ b2;
b1 ^= b2;
MessageBox.Show(b1.ToString() + " : " + i1.ToString());
very very odd...
Last edited by Phill64; Nov 26th, 2009 at 10:21 PM.
-
Nov 26th, 2009, 10:28 PM
#5
Re: [RESOLVED] XOR ^ operator not compiling...
Originally Posted by Phill64
XOR is only a bitwise operator in VB.NET
Nope.
Originally Posted by MSDN Library - Xor Operator (Visual Basic)
Performs a logical exclusion on two Boolean expressions, or a bitwise exclusion on two numeric expressions.
To call it a bug in the language is a bit of an assumption because I'll wager that it's by design. I'm sure that there was a valid reason that they made that design choice in C# but I couldn't tell you what it was. The "workaround" is to simply cast the result as the type you want if you need it to be that type.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|