+ 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; }
11 Answers
+ 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
+ 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.
+ 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
+ 2
Also, The general formula to calculate the tilde operationĀ ~iĀ isĀ ~i=-i-1
So, ~7 will return -8
+ 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
+ 1
And 0x00f is a hex number(base 16) which is 15 in decimal for those wondering
+ 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
+ 1
Brian
Yeah thanks for correction
All the 8 bits
~7 & 15 will be 8
Not ~7
š
+ 1
great answer is here :
https://www.sololearn.com/Discuss/2994377
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
- 2
.