tags:

views:

323

answers:

6

I have an array of unsigned numbers, called dataArray (actually in the program I am trying to figure out, they are entered as hexadecimal numbers, but I don't think that's important). I have another variable of type unsigned char, called c.

What does this do?

unsigned int dataArray[]={1,2,3,4,5};
unsigned char c;
x=c^ (dataArray)[i];

I have read that the caret is a reference to c, but what does it mean when the array name is in parentheses? It seems that x is just set to the (i-1)th element in dataArray, but under what conditions is that not so?

Thanks.

+11  A: 

The parenthesis don't impact the semantics here. x is computed as the bitwise exclusive-or of c (undefined here) and dataArray[i].

johnny
So what would be in x?
Malfist
It'd be c XOR dataArray[i]. Can't be more clear than that.
Eduard - Gabriel Munteanu
How do you XOR an array?
Malfist
It's the element at index i being XOR'ed with C, not the whole array.
rmeador
A: 

If this is really C++, then the '^' is the XOR bitwise operator. The parentheses are extra and do nothing in this example.

Robert
+1  A: 

The ^ character as it is used here is a bitwise XOR (see wikipedia). The parentheses don't to anything here, as the operator [] has higher precedence than bitwise-xor.

gimpf
A: 

The ^ is not overloaded, as far as I know. If it is entered and compiles properly, it is going to attempt a XOR operation, though in this case it's probably going to be messing with data you don't want it to...

Zachery Delafosse
A: 

Ok, cool. That makes sense in the program I'm using. I had thought the caret was a pointer, but it's an XOR and then later on in the line there's an & (bitwise AND). The program is for sending and receiving signals from electronics, so bit-level logic is to be expected. Thanks very much for the fast replies.

It is preferred to edit one's question or write a comment, instead of writing an "answer" which isn't one. Nothing bad happens, but keep in mind, that those answers are seldom viewed in chronological order, but all of them indeed stay mostly on their own.
gimpf
+1  A: 

In some derived language, the caret is used to declare a managed pointer. It's the so-called C++/CLI language made by microsoft and is used in .Net. It doesn't have that meaning in normal C++. In expressions, the caret means, as already explained by others, the bit-wise XOR, anyway.

Johannes Schaub - litb