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(); }
&noaddress operator
&no
scanf("%d",&no);
This &nomeans the address of the variable no. It is used to scanf()put the result there.
no
scanf()
&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
&&and ||, short circuit operators
&&
||
((expression from above) && printf("odd")) || printf("even");
odd, even, .
odd
even
&& || . :
(0 && ...)
0 || printf("even")
printf("even")
(1 && ...)
1 &&
printf("odd") || printf("even")
printf("odd")
,
(no & 1) ? printf("odd") : printf("even"); printf((no & 1) ? "odd" : "even"); if (no & 1) { printf("odd"); } else { printf("even"); }
& - && .
, 0 , LSB - 1.(no & 1) , LSB 0 1, .. 0, LSB 0 1, LSB 1.0, && - , , || "even". no & 1 1, && "odd".
0
1
(no & 1)
"even"
no & 1
"odd"
no & 1 no. , no & 1 1, no .
no & 1 == 0, && , (no & 1 && printf("odd")) FALSE printf("even").
no & 1 == 0
(no & 1 && printf("odd"))
no & 1 != 0, && "odd" . (no & 1 && printf("odd")) , printf(), || .
no & 1 != 0
printf()
no & 1 AND. 0, no 1, no . , 1.
&& in (no & 1 && printf("odd")) AND, . no & 1 false ( no ), . no & 1 true ( no ), printf("odd") .
false
true
, no & 1 false, && false printf.
printf
, (no & 1 && printf("odd")) || printf("even");.
(no & 1 && printf("odd")) || printf("even");
, (no & 1) == 1 , , no % 2 == 1; printf, .
(no & 1) == 1
no % 2 == 1
"", 0 skip && printf ( "" ). 1, printf ( "odd" )
&& .true && true - true, - false.& .
: :
00000110 & 00000100 ---------------- 00000100 ================
: http://www.cprogramming.com/tutorial...operators.html .
& no is the address of the no variable. & & is a logical AND || this is OR.
Source: https://habr.com/ru/post/1527097/More articles:Python Comparing two string lists for similarities - pythonReplace brackets for their regular expression - pythonBash - if statement combined with mail - linuxOverriding @property installers with ARC for @property with 'copy' - iosListBoxItem style in or in ? - stylesmysql select one last row of each type - mysqlNo config.adapter.idAttribute specified for table "books" in Titanium Appcelerator - appceleratorEditing a phone call "Quick view form" - dynamics-crmHow to start and choose an application - androidhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1527102/chrome-insists-to-download-javascript-source-map-files-despite-disabled-setting-when-started-from-intellij-idea&usg=ALkJrhhQ_kssW2FG2cMaPqDMvbtuDa7j-QAll Articles