+ 7

Why this code is printing 0 and 1?

https://code.sololearn.com/c51IJrxzZTR5/?ref=app

27th Apr 2019, 2:05 PM
Yamin Mansuri
Yamin Mansuri - avatar
14 ответов
+ 7
@Honfu that is true. Made some example code to show, what is happening https://code.sololearn.com/cICtb50w1si1 https://en.cppreference.com/w/c/language/operator_precedence
27th Apr 2019, 9:02 PM
sneeze
sneeze - avatar
+ 9
Hm, maybe 1<<-1 is not possible and returns 0, like other operations (type conversions) sometimes do? Excited to hear the solution! EDIT: Ha, no, minus seems to just change the direction of shifting. So 1 << -1 seems to work like 1>>1 which is 0.
27th Apr 2019, 2:28 PM
HonFu
HonFu - avatar
+ 8
Werg Serium thanks bro Now I understand
28th Apr 2019, 3:15 PM
Yamin Mansuri
Yamin Mansuri - avatar
+ 8
sneeze thanks bro now I understand your code
28th Apr 2019, 3:15 PM
Yamin Mansuri
Yamin Mansuri - avatar
+ 7
printf("%d\t",1<<2);// 0001 shift two to left 0410 (decimal 4) Why this line is printing 4??
28th Apr 2019, 2:06 PM
Yamin Mansuri
Yamin Mansuri - avatar
+ 6
sneeze Sorry bro I didn't Understand. Is "<<" this is conditional operator?
28th Apr 2019, 8:56 AM
Yamin Mansuri
Yamin Mansuri - avatar
+ 5
I learn something new everyday. Thanks sneeze for that lesson about left/right shift by negative values.
28th Apr 2019, 1:17 AM
Sonic
Sonic - avatar
+ 5
sneeze I didn't understand😭 bro
28th Apr 2019, 9:26 AM
Yamin Mansuri
Yamin Mansuri - avatar
+ 3
Conditional operator < smaller than > Greater than Mathenatical operator << Shift left >> Shift right
28th Apr 2019, 9:03 AM
sneeze
sneeze - avatar
+ 1
<< is not a conditional operator. Did you want to compare ? Please elaborate more what do you want to achieve ?
28th Apr 2019, 1:30 PM
sneeze
sneeze - avatar
+ 1
That's because first occurs 2-3, and after it will do 1<<-1 this is why first print 0
28th Apr 2019, 2:41 PM
Werg Serium
Werg Serium - avatar
+ 1
If you use parentesis it will occurs first 1<<2 and after will do 4-3 like you did in the second print.
28th Apr 2019, 2:42 PM
Werg Serium
Werg Serium - avatar
+ 1
Remember 1<<something is doing 1*2^something.
28th Apr 2019, 2:44 PM
Werg Serium
Werg Serium - avatar
+ 1
Ok. Excuses printf("%d\t",1<<2);// 0001 shift two to left 0410 (decimal 4) was a typo error should be printf("%d\t",1<<2);// 0001 shift two to left 0100 (decimal 4) I have corrected it in my code now
28th Apr 2019, 3:51 PM
sneeze
sneeze - avatar