What is the use of the & & && operator in the next program?

I saw the following code on one website to check for an odd or even number without using an "if". But I can not understand the encoding and how it works. Please 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();
}
+4
source share
8 answers
  • &noaddress operator

    scanf("%d",&no);
    

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

  • &bitwise AND

    no and 1

    This value nois bitwise ANDed with a value of 1. Essentially, this gives the value of the least significant bit. If this bit is set, the value is odd, otherwise even.

  • &&and ||, short circuit operators

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

odd, even, .

&& || . :

  • false (== 0), (0 && ...), 0 . , 0 || printf("even"), printf("even").
  • , , (!= 0), (1 && ...), 1 && . , printf("odd") || printf("even"). printf("odd"), , printf("even") .

,

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

& - && .

, 0 , LSB - 1.
(no & 1) , LSB 0 1, .. 0, LSB 0 1, LSB 1.
0, && - , , || "even". no & 1 1, && "odd".

+4

no & 1 no. , no & 1 1, no .

no & 1 == 0, && , (no & 1 && printf("odd")) FALSE printf("even").

no & 1 != 0, && "odd" . (no & 1 && printf("odd")) , printf(), || .

+4

no & 1 AND. 0, no 1, no . , 1.

&& in (no & 1 && printf("odd")) AND, . no & 1 false ( no ), . no & 1 true ( no ), printf("odd") .

, no & 1 false, && false printf.

+3

, (no & 1 && printf("odd")) || printf("even");.

  • & , no & 1 1.
  • && , , , .
  • || , , , false.

, (no & 1) == 1 , , no % 2 == 1; printf, .

+2

"", 0 skip && printf ( "" ). 1, printf ( "odd" )

+1

&& .
true && true - true, - false.
& .

: :

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

: http://www.cprogramming.com/tutorial...operators.html .

+1

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

0
source

Source: https://habr.com/ru/post/1527097/


All Articles