By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,445 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

strange behaviour of ^ (basic question)

P: n/a
I have lines in my code like:

const int depth = 4;

int node_total = (depth + 1)^2;

This is giving a very different value for node_total than the seemingly
equivalent

int node_total = (depth + 1) * (depth + 1);

Why? What is the difference?

For example depth = 4; seems to lead to node_total == 7;

in my earlier version: int node_total = (depth + 1) ^2;

In other words, my first guess is that ^ is behaving like + (although I
would need more values to check this.)

Can anyone explain what ^ is doing?

As an incidental point, it is hard to google-search such inquiries --
the google search engine doesn't treat technical notation like ^ or ++
well at all, and is unable to judge what is "similar" to the
search-engine text.

I wonder if the google people are working on that (or if there is a
user's trick I'm missing.)

Paul Epstein

Oct 25 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
^ does a bitwise exclusive or. It has nothing to do with powers. -
Tony

Oct 25 '05 #2

P: n/a
^ is bitwise-xor, x ^ 2 means x XOR 2, not x squared

4 + 1 = 5 (decimal) = 101 (binary)
2 (decimal) = 010 (binary)

101 xor 010 = 111 (binary) = 4 + 2 + 1 = 7 (decimal)

HTH,
Stu

<pa**********@att.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I have lines in my code like:

const int depth = 4;

int node_total = (depth + 1)^2;

This is giving a very different value for node_total than the seemingly
equivalent

int node_total = (depth + 1) * (depth + 1);

Why? What is the difference?

For example depth = 4; seems to lead to node_total == 7;

in my earlier version: int node_total = (depth + 1) ^2;

In other words, my first guess is that ^ is behaving like + (although I
would need more values to check this.)

Can anyone explain what ^ is doing?

As an incidental point, it is hard to google-search such inquiries --
the google search engine doesn't treat technical notation like ^ or ++
well at all, and is unable to judge what is "similar" to the
search-engine text.

I wonder if the google people are working on that (or if there is a
user's trick I'm missing.)

Paul Epstein

Oct 25 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.