-2

Can't seem to figure out what the "<<" operator is:

11<< 2 is 44
1<<1 is 2
10<<2 is 40
  • possible duplicate of [Absolute Beginner's Guide to Bit Shifting?](http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting) –  Dec 05 '14 at 22:39

2 Answers2

1

The shift operators, bitwise shift the value on their left by the number of bits on their right:

The << shifts left and adds zeros at the right end.

The >> shifts right and adds either 0s, if value is an unsigned type, or extends the top bit (to preserve the sign) if its a signed type.

Thus,2 << 4 is 32 and -8 >> 3 is -1.

Reference: http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/cxx_crib/shift.html

Dax
  • 23
  • 7
0

This is known as Bit Shifting in computing.

11 => 1011 So if you left shift as 11<<2, that means you are adding 2 zero bits at the end of this binary number. 1101100 (Binary) = 44

Example Explain

If you apply << for 1011, 2 zero bits are appended at the end of that binary value. Then the value would be 101100

So If you check the decimal value of 101100, 1*(32) + 0*(16) + 1*(8) + 1*(4) + 0*(2) + 0*(1) = 44

Short method

  • 11<<2 = 11* 2(power of 2) = 44

  • 1<<1 = 1* 2(power of 1) = 2

  • 10<<2 = 10* 2(power of 2) = 40

  • a << n = a*2(power of n)

G. Raven
  • 77
  • 8