Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 And, or, xor, etc. on integers?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Briggs




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
DanielG




PostPosted: 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 ||)
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: