kalyan
kalyan

Reputation: 55

What is the use in & and && operator in the following program?

I saw the following code in one website to check odd or even number without using "if". But, I am not able to understand the coding and how it works. Please, can you explain the functional part of the code.

#include<stdio.h>
#include<conio.h>
int main()
{
    int no;
    clrscr();
    printf("Enter a number");
    scanf("%d",&no);
    (no & 1 && printf("odd")) || printf("even");
    return 0;
    getch();
}

Upvotes: 4

Views: 1384

Answers (8)

glglgl
glglgl

Reputation: 91017

  1. &no, address operator

    scanf("%d",&no);
    

    This &no means the address of the variable no. It is used to have scanf() put the result there.

  2. &, bitwise AND

    no & 1

    This is the value of no, bitwise ANDed with the value 1. Essentially, this gives the value of the lowest bit. If that bit is set, the value is odd, otherwise even.

  3. && and ||, short-circuit operators

    ((expression from above) && printf("odd")) || printf("even");
    

This expression is supposed to output odd if the value is odd and even if the value is even.

&& and || are so-called "short-circuit operators". So the following happens:

  • If the expression is false (== 0), the first part evaluates to (0 && ...), which evaluates to 0 without evaluating the 2nd part. So we have 0 || printf("even"), which is equivalent to printf("even").
  • If, however, the expression is true (!= 0), the first part evaluates to (1 && ...), where 1 && is redundant. So essentially you have printf("odd") || printf("even"). This relies on printf("odd") returning a nonzero value, so that printf("even") is suppressed.

Thus, better solutions would be

(no & 1) ? printf("odd") : printf("even");
printf((no & 1) ? "odd" : "even");
if (no & 1) {
    printf("odd");
} else {
    printf("even");
}

Upvotes: 3

aalku
aalku

Reputation: 2878

So you want to understand (no & 1 && printf("odd")) || printf("even");.

  • & means bitwise and so no & 1 means get first bit from 1.
  • && means logical and but it uses short circuit so it won't evaluate second expression if first is true.
  • || means logical or but it uses short circuit so it won't evaluate second expression if first is false.

So it will print odd if (no & 1) == 1 or, in other words, if no % 2 == 1; else it will evaluate the last printf so it will print even.

Upvotes: 2

nuke_infer
nuke_infer

Reputation: 63

&& is logical and.
true && true is true and everything else is false.
& is bitwise and.

It can be explained as binary: Code:

  00000110
& 00000100
----------------
  00000100
================

For more information : http://www.cprogramming.com/tutorial...operators.html Read about other bitwise operators.

Upvotes: 1

haccks
haccks

Reputation: 105992

& is used for bit-wise AND and && is used for logical AND.

Now the logic is that a number is even if its least significant bit is 0 and is odd if LSB is 1.
(no & 1) checks whether LSB is 0 or 1, i.e, by anding it will give 0 if LSB is 0 and 1 if LSB is 1.
If it is 0 then the right expression of && is not evaluated because of the short circuit behavior of the operator and hence right sub-expression of || prints "even". If no & 1 is 1 then right sub-expression of && is evaluated and prints "odd".

Upvotes: 4

Bill the Lizard
Bill the Lizard

Reputation: 405675

The no & 1 is a bitwise AND. The result of that operation will be 0 if no is even and 1 if no is odd. This is because only the least significant bit of 1 is set.

The && in (no & 1 && printf("odd")) is a boolean AND, and the expression relies on the short-circuit evaluation of that operator. If no & 1 evaluates to false (when no is even), the statement will not print. When no & 1 evaluates to true (when no is odd), the printf("odd") statement will be evaluated.

It's in the case where no & 1 evaluates to false that then entire && expression evaluates to false and the second printf statement is evaluated.

Upvotes: 3

anonymous
anonymous

Reputation: 1317

Take the first bit of "no" and if 0 skip && operator and proceed to evaluate printf("even"). If 1, go evaluate printf("odd")

Upvotes: 1

timrau
timrau

Reputation: 23058

no & 1 gets the least significant bit of no. Thus, no & 1 gets 1 if no is odd.

If no & 1 == 0, then the right hand side of && is skipped, (no & 1 && printf("odd")) is evaluated as FALSE, and printf("even") is evaluated.

If no & 1 != 0, then the right hand side of && is evaluated and prints "odd" on console. (no & 1 && printf("odd")) is evaluated as TRUE if the printf() successes, and then the right hand side of || is skipped.

Upvotes: 4

user3071328
user3071328

Reputation: 17

&no is the address of variable no. && is the logical AND. || is OR.

Upvotes: 0

Related Questions