What is Operator ^+

J

Jack.Thomson.v3

Why does the following program works, and what is ^+ operator ?

int main()
{
int a = 10;
int b = 20;
int c;

c = a ^+ b;

printf("Value ===> %d", c);

return 0;
}
 
J

John Bode

Why does the following program works, and what is ^+ operator ?

int main()
{
int a = 10;
int b = 20;
int c;

c = a ^+ b;

printf("Value ===> %d", c);

return 0;

}

There is no ^+ operator. The statement is being parsed as

c = a ^ (+b);
 
J

Jack.Thomson.v3

There is no operator ^+ but there is an unary operator + (like there an
unary operator -) which you are using here.

Yours,

But the output is 30, it is acting same as the addition operator.
 
K

Kenny McCormack

There is no ^+ operator. The statement is being parsed as

c = a ^ (+b);

And ^ is XOR, which means "give me all the bits not in common between
the two operands". Since 10 is 8+2 and 20 is 16+4, this boils down to
being equivalent to addition.
 
J

Jean-Marc Bourguet

But the output is 30, it is acting same as the addition operator.

There is no bit in common between 10 and 20, so there is no carry to
propagate and indeed exclusive or behave like addition for these operands.
But that's quite pecular to these values.

Yours,
 
R

Richard Tobin

There is no operator ^+ but there is an unary operator + (like there an
unary operator -) which you are using here.
[/QUOTE]
But the output is 30, it is acting same as the addition operator.

Try some different numbers. Two equal ones for example.

-- Richard
 
E

Eric Sosman

Why does the following program works, and what is ^+ operator ?

int main()
{
int a = 10;
int b = 20;
int c;

c = a ^+ b;

printf("Value ===> %d", c);

return 0;
}

There is no ^+ operator, but there are ^ and unary +
operators. The statement is equivalent to

c = a ^ (+b);

.... which is equivalent to

c = a ^ b;
 
K

Kenneth Brody

Why does the following program works, and what is ^+ operator ?

int main()
{
int a = 10;
int b = 20;
int c;

c = a ^+ b;

printf("Value ===> %d", c);

return 0;
}

There is no "^+" operator. It's simply the binary "^" operator
followed by the unary "+" operator. It's equivalent to:

c = a ^ ( + b );

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Kenneth Brody

But the output is 30, it is acting same as the addition operator.

No, the output happens to give the same results as if you had used
addition, but that's only coincidental. It just happens to be that
both:

10 ^ 20
and
10 + 20

result in the value 30.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

rpgfan3233

Why does the following program works, and what is ^+ operator ?

int main()
{
        int a = 10;
        int b = 20;
        int c;

        c = a ^+ b;

        printf("Value ===> %d", c);

        return 0;

}

Just in case you don't understand, the '+' is NOT addition here. It
represents the sign of the number, i.e. "positive b", or 1 * b, if it
helps you to think of it that way. In other words:
c = a ^+ b;
c = a ^ (+b);
c = a ^ (1 * b);
c = a ^ b;

If you used "c = a ^- b," it would be similar, except that you would
be performing the bitwise XOR operation on the negative value of 'b'.
Obviously the sign of the variable doesn't correspond directly to the
sign of the value. After all, if b==-4 then it would be the same as
the following:
c = a ^+ (-4);
c = a ^ (-4);

If you used ^-, it would be the following:
c = a ^- (-4);
c = a ^ -(-4);
c = a ^ 4;

I hope this helps!
 
J

Jack.Thomson.v3

Just in case you don't understand, the '+' is NOT addition here. It
represents the sign of the number, i.e. "positive b", or 1 * b, if it
helps you to think of it that way. In other words:
c = a ^+ b;
c = a ^ (+b);
c = a ^ (1 * b);
c = a ^ b;

If you used "c = a ^- b," it would be similar, except that you would
be performing the bitwise XOR operation on the negative value of 'b'.
Obviously the sign of the variable doesn't correspond directly to the
sign of the value. After all, if b==-4 then it would be the same as
the following:
c = a ^+ (-4);
c = a ^ (-4);

If you used ^-, it would be the following:
c = a ^- (-4);
c = a ^ -(-4);
c = a ^ 4;

I hope this helps!



Thanks... All the answers helped...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top