Tell me how conditions are working? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

Tell me how conditions are working?

#include <stdio.h> int main() { if(7&8) printf ("Honesty"); if((~7&0x00f)==8); printf ("is the best policy \n"); return 0; }

2nd Mar 2022, 4:28 AM
BABLU TAUNWAL
BABLU TAUNWAL - avatar
11 ответов
+ 3
Learn about bitwise operations and you'll figure out the answer to your question. Keep in mind any non-zero result evaluates to true while zero evaluates to false. https://www.sololearn.com/learn/4070/?ref=app
2nd Mar 2022, 4:31 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
NonStop Coding It's mentioned in the bitwise operations and Simba knows this I'm pretty sure BABLU TAUNWAL there is also a semicolon immediately after the closing parenthesis for the if condition, making it pointless, so that the last printf() should always run regardless of the evaluated result of the condition.
2nd Mar 2022, 4:46 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
ChaoticDawg Ooh thanks I got it I did not notice the the semicolon so for the first if condition binary of 7 is 0111 binary of 8 is 1000 0111 & 1000 will be 0000 so the first condition will be false
2nd Mar 2022, 4:49 AM
NonStop CODING
NonStop CODING - avatar
+ 2
Also, The general formula to calculate the tilde operation ~i is ~i=-i-1 So, ~7 will return -8
2nd Mar 2022, 4:51 AM
Simba
Simba - avatar
+ 2
For the second if Binary of 7 is 00000111 ~7 will be one's complement ~7 = 11111000 which is -8 ( in decimal) 0x00f = 15 ( in decimal ) So, -8 & 15 will be equal to 8 Second if condition is true If(( ~7 & 0x00f) == 8) will be true /**********************************/ and for the first if condition binary of 7 is 0111 binary of 8 is 1000 0111 & 1000 will be 0000 so the first condition will be false
2nd Mar 2022, 5:03 AM
NonStop CODING
NonStop CODING - avatar
+ 1
And 0x00f is a hex number(base 16) which is 15 in decimal for those wondering
2nd Mar 2022, 4:38 AM
Simba
Simba - avatar
+ 1
NonStop CODING examples of one's complement should include the remaining bits of the byte or word. Consider all 8 bits of a signed char: 7 = 00000111 ~7 = 11111000 = 0xf8 Compare with -8 (two's complement): 8 = 00001000 -8 = ~8+1 = 11110111+1 = 11111000 = 0xf8 ~7 == -8
3rd Mar 2022, 7:36 AM
Brian
Brian - avatar
+ 1
Brian Yeah thanks for correction All the 8 bits ~7 & 15 will be 8 Not ~7 👍
3rd Mar 2022, 10:22 AM
NonStop CODING
NonStop CODING - avatar
4th Mar 2022, 6:10 PM
Tarik Khalil
Tarik Khalil - avatar
0
Simba ~ is one's complement one's complement of 7 is 0 but from where to solve ? left to right or right to left I think it's left to right
2nd Mar 2022, 4:43 AM
NonStop CODING
NonStop CODING - avatar
- 2
.
3rd Mar 2022, 9:00 PM
mateo schütz