473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

strange behaviour of ^ (basic question)

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
2 1009
^ does a bitwise exclusive or. It has nothing to do with powers. -
Tony

Oct 25 '05 #2
^ 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested...
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
3
by: Andrew Mayo | last post by:
(note: reason for posting here; browser helper object is written in C++; C++ developers tend to know the intricacies of message handling; this looks like a Windows messaging issue) Microsoft...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
10
by: bear | last post by:
hi all, I have a program whose speed is so strange to me. It is maily used to calculate a output image so from four images s0,s1,s2,s3 where so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no...
5
by: soeren | last post by:
Hello, two days ago I stumbled across a very strange problem that came up when we were printing tiny double numbers as strings and trying to read them on another place. This is part of an object...
5
by: Ian | last post by:
Hi everyone, I have found some bizarre (to me...!) behaviour of the Form_Activate function. I have a form which has a button control used to close the form and a subform with a datasheet view...
8
by: FBM | last post by:
Hi there, I am puzzled with the behavior of my code.. I am working on a networking stuff, and debugging with eclipse (GNU gdb 6.6-debian).. The problem I am experiencing is the following: ...
2
sgeklor
by: sgeklor | last post by:
Hi guys, I have a panel on a form and at runtime I create some controls on the panel. Then, also during runtime I want to clear the panel of all of its controls. The basic way to do this is with...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.