+ 7
Why this code is printing 0 and 1?
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
+ 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.
+ 8
Werg Serium thanks bro
Now I understand
+ 8
sneeze thanks bro now I understand your code
+ 7
printf("%d\t",1<<2);// 0001 shift two to left 0410 (decimal 4)
Why this line is printing 4??
+ 6
sneeze Sorry bro I didn't Understand.
Is "<<" this is conditional operator?
+ 5
I learn something new everyday. Thanks sneeze for that lesson about left/right shift by negative values.
+ 5
sneeze I didn't understand😭 bro
+ 3
Conditional operator
< smaller than
> Greater than
Mathenatical operator
<< Shift left
>> Shift right
+ 1
<< is not a conditional operator. Did you want to compare ?
Please elaborate more what do you want to achieve ?
+ 1
That's because first occurs 2-3, and after it will do 1<<-1 this is why first print 0
+ 1
If you use parentesis it will occurs first 1<<2 and after will do 4-3 like you did in the second print.
+ 1
Remember 1<<something is doing 1*2^something.
+ 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