0
PORTB = 0b11001011;
PORTC = 0b00111011;
if(PORTB & PORTC)
    {
     //do something
    }

//will this "if" comes out to be true?how compiler will check it?

PORTB = 0b11001011;
PORTC = 0b00111011;
if(PORTB && PORTC)
    {
    //do something
    }

//how this "&&" be compared? if i write "if(PORTB)" than how compiler wil evaluate it?

shafeeq
  • 141
  • 1
  • 3
  • 14

1 Answers1

0

& does bit and, so

PORTB & PORTC == 0b00001011

converting this to a boolean yields true (non-zero)

&& does boolean and, so PORTB is true (non-zero), and PORTC is true, so

PORTB && PORTC

yields true


So is there a difference? Yes. && uses short-circuiting, while & doesn't. Consider the following:

0 & some_complicated_expression
0 && some_complicated_expression

In the first case, the complicated expression will be evaluated. However, since && uses short-circuiting, the complicated expression is not evaluated in the second case (the expression yields false, irrespective of the result of the complicated expression).

Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61
  • @huester so if i write simply evaluate a binary number to decimal and if it is not zero then true else false.is this? for e.g ob00000011 = 3 so it is true?? in your example for first case if complicated expression comes out to be true then?? – shafeeq Jun 19 '13 at 09:20
  • @shafeeq yes exactly. It doesn't matter to what the complicated expression evaluates, because `0 & X == 0`, no matter what the value of `X` is. – Vincent van der Weele Jun 19 '13 at 09:26
  • u hanvt tell me that how a binary number is evaluated for true or false?? – shafeeq Jun 19 '13 at 09:34
  • @shafeeq `0 => false` any thing else is `true` – Vincent van der Weele Jun 19 '13 at 10:17