Computer Science Canada

And, or, xor, etc. on integers?

Author:  Briggs [ Wed Feb 25, 2009 5:58 pm ]
Post subject:  And, or, xor, etc. on integers?

(I'm using Dev-C++ 4.9.9.2 with g++, before anybody asks.)

For some reason, (int xor int) will xor the binary values and return one result, but if I try doing (int and int) or (int or int) it gives me 0 and 1 instead.

For example:

9 xor 5 gives me 12, which is correct.
9 or 5 gives me 1, when it should give me 13.
9 and 5 gives me 1, which is correct, but 9 and 6 also gives me 1 when it should give me 0.

I know this has to do with the fact that in boolean algebra, any nonzero values are treated as 1, but I have no idea how to make it so that "and" and "or" also compare the binary values of integers. Please help?

Author:  DanielG [ Wed Feb 25, 2009 6:21 pm ]
Post subject:  RE:And, or, xor, etc. on integers?

you have to use the binary operators (& for and instead of the regular && and | for or instead of the regular ||)

Author:  Tony [ Wed Feb 25, 2009 11:20 pm ]
Post subject:  RE:And, or, xor, etc. on integers?

as Daniel pointed out.

&& is a boolean and, returning true/false (1/0)

while & is a binary and, which is what you are looking for.


: