How does it work | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 2

How does it work

cout<<(1<<1) ; //output is 2

1st Sep 2018, 5:13 AM
Omar
Omar - avatar
4 Antworten
+ 5
it's just shif-left(<<) operator which shifts the each bit to the left side. so in this example 1<<1 means shift the bits of 1 ( which is on the left hand side of operator<<)to left side only 1(which is on the right hand side of the operator<<)time so 1<<1 binary of 1 : 0001= decimal(1) now shifting to left only 1 time : 0010 = decimal (2) like wise if there is 1 << 2 then binary of 1 : 0001 shift 2 times left so 0010 = decimal(2) 0100 = decimal(4) so ans will be decimal(4) hope it helps....
1st Sep 2018, 5:24 AM
Tanay
Tanay - avatar
1st Sep 2018, 5:27 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 5
so basically when you do shift left once it multiply the number with 2 and likewise when you right shift a number once it divides the number by 2
1st Sep 2018, 5:28 AM
Tanay
Tanay - avatar
+ 2
<< is a left shift operator left side indicates the number right side indicates no. of bits to be shifted 1<<1 results in 1 bit shifting 1 position to the left, so it would be 10 which is binary for 2 left shift and right shift works on a binary level. So 2<<1 would be 10 << 1 = 100 = 4
1st Sep 2018, 6:09 AM
‎ ‏‏‎Anonymous Guy