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

Username:   Password: 
 RegisterRegister   
 && vs & and || vs |
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
raptors892004




PostPosted: Tue Nov 14, 2006 11:35 am   Post subject: && vs & and || vs |

I'm sorry if this may seem as kind of a dumb question but I always keep forgeting about this. What is the difference between && and & and || and | ? I know one checked all conditions regardless if one of them was broken but which one is it?

I want my program to check 3 values and if one of them is incorrect (does not equal a supplied default one) then it goes to execute different code.

Thanks for any replies in advance.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Nov 14, 2006 12:24 pm   Post subject: (No subject)

&& and || are logical. The others work on a bitwise basis.
raptors892004




PostPosted: Tue Nov 14, 2006 4:16 pm   Post subject: (No subject)

wtd wrote:
&& and || are logical. The others work on a bitwise basis.


So what is the difference between && and & (it would be the same for || and |)? That was my question (I may have not expressed it too clearly though).
Andy




PostPosted: Tue Nov 14, 2006 4:25 pm   Post subject: (No subject)

okay, bitwise or will or every single bit of two given binary strings. where as logical or will only give you 0 or 1
andy_tok




PostPosted: Tue Nov 14, 2006 4:54 pm   Post subject: (No subject)

&& and || are short-circuits while & and | are not.
Sometimes short-circuit is useful.
Exp. (x!=0)&&(x++>10)
if x is 0, then the increment will not occur
if you want x++ to be implemented in all situations then use (x!=0)&(x++>10)
Andy




PostPosted: Tue Nov 14, 2006 5:13 pm   Post subject: (No subject)

the short circuit aspect is a result of bitwise/logical characteristics. it's not by itself, a specific feature.
r.3volved




PostPosted: Tue Nov 14, 2006 6:55 pm   Post subject: (No subject)

Basically what they're telling you is to look up the difference between, and definitions for bitwise and logical operators. You won't understand what they're used for if you don't understand why they're used.

Links:
http://www.quepublishing.com/articles/article.asp?p=101368&seqNum=5&rl=1
http://www.janeg.ca/scjp/oper/bitwiseAndLogical.html
http://www.janeg.ca/scjp/oper/bitwise.html
http://www.janeg.ca/scjp/oper/logical.html
raptors892004




PostPosted: Tue Nov 14, 2006 10:10 pm   Post subject: (No subject)

Thank you very much for the help once again. Those websites were really useful Smile
Sponsor
Sponsor
Sponsor
sponsor
r.3volved




PostPosted: Tue Nov 14, 2006 10:34 pm   Post subject: (No subject)

Damn straight
I run shit Cool
*toot* (my horn) *toot*
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: