473,386 Members | 1,734 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,386 software developers and data experts.

The ^ operator

What exactly does the ^ operator do? I've seen, for example, that
3^4=7, 3^5=8. but 3^3=0. Is it just adding them together if they are
not equal, and if they are equal, outputs 0, or what? Thanks!

Sep 26 '05 #1
3 1279
Tuvas wrote:
What exactly does the ^ operator do? I've seen, for example, that
3^4=7, 3^5=8. but 3^3=0. Is it just adding them together if they are
not equal, and if they are equal, outputs 0, or what? Thanks!


It performs an "exclusive-OR" (XOR) operation on the binary data
corresponding to those values. Where the boolean AND operation returns
a 1 where both inputs are 1 and a 0 otherwise, and boolean OR returns a
1 if either input is 1 but 0 otherwise, the XOR operator returns a 1 if
the two inputs are different (i.e. one is 0 and the other is 1) but a 0
if they are the same.

When done on the binary value, each corresponding bit is compared using
the above logic. For example, 3 is 0011 in binary, and 4 is 0100, so:

011 binary for 3
^ 100 binary for 4
---
= 111 (which is binary for 7).

(match up the binary digits vertically, so the first digit in the result
comes from the 0 and the 1 above it, in the two input values).

-Peter
Sep 26 '05 #2
Thanks alot! That sure makes lots of sense!

Sep 26 '05 #3
Tuvas wrote:
What exactly does the ^ operator do? I've seen, for example, that
3^4=7, 3^5=8. but 3^3=0. Is it just adding them together if they are
not equal, and if they are equal, outputs 0, or what? Thanks!


^ is the "bit XOR" operation. It treats its left and right operands as
binary numbers: the result has a one-bit in those bit positions where
one operand has a zero and the other has a one.

A B | A XOR B
----+--------
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0

If you want exponentiation, try **.
3**4 81 3**5 243 3**6 729 3**7 2187


regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.pycon.org

Sep 26 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.