>> I googled the dinference between bitwise and logical operators, but i could not understand exactl what it is. Can you simply explain ? 

Recommended Answers

All 7 Replies

Can you post what you have found and your questions about what you don't understand?

The logical operator returns a true/false value like == or <
bitwise changes the value of a variable like a + or * operator

>>hello everyone,

Whats the difference when your using bitwise operators and logical operators in a conditional statements?

Bitwise Example:
if ((val==1)|(val==2)) printf("%d\n",val);
else printf("False");

Logical Example:
if ((val==1)||(val==2)) printf("%d\n",val);
else printf("False");

Which do you think is better and why is that? Thanks again for the replies ^_^

>>i found this/ 

Where are the definitions of the two types of operators you were asking about?

someone correct me if I'm wrong: both bitwise and logical operators are the representation of the boolean function with the same names(better read on that first as I suspect the fact that you haven't) the diference is that the bitwise ones operate on bits(it doesn't matter if your data type has 8,16,32 or 64 bits, the operation is applied to every bit of the operands e.g 1|1 =1 10|01 =11, 100|1 = 101 and so on), where as the logical operators operate on logical atoms( the whole atom is considered as a bit, the interpreted values are false for 0 and true for non-zero values)

^ basically right, except that nowhere does Java define any mapping between 0 and 1s vs the boolean values false/true.
Bitwize operators work on each bit of an int value, the logical operators work on individual boolean true/false values, but there's no connection between these two versions.

Ohh, When i asked this question, it was almost midnight in my country :) I think this is the reason why i did not understand the difference. Now I am so regretful for asking this simple question. Thank you all guys.

Java uses the (|) operator for two things:
bitwise OR
non-short circuit binayr OR - this one is hard to find documented.

Try the following code:

   int x = 0;
   if((x != 0)|| (1/x > 0)) {};   // short circuit version: first false exits
   if((x != 0)| (1/x > 0)) {};    // non shortcircuit - both expressions evaluated

It is recommended that the second version not be used.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.