Jump to content

Can anyone explain Shift Left (<<)?


LooieENG

Recommended Posts

It's a bitwise operator, and you have to think about the data as a series of binary bits (rather than its ordinary datatype).

 

e.g a number like 43,

$a = 43;

which in binary is 101011, (32 + 8 + 2 + 1)

with leading zeroes giving 00000000000000000000000000101011 (to 32-bits if it's an integer on a 32-bit platform).

what shift left does is shifts every bit to the left, adding a new 0 value bit as the right-most bit, and losing the existing left-most bit.

If we shift left just once, we get

00000000000000000000000001010110

which is the binary value for 86 (64 + 16 + 4 + 2)

 

Link to comment
Share on other sites

Errr....  What's not to get?  You're shifting 4 left two times.

 

1002 << 210 = 10002 << 110 = 100002 << 010

 

In other words, for x << y, x is the number and y is how many times to shift.

 

By the way, it's worth noting that x << y is the same as x * 2^y.  (Example: 4 << 2 = 16 = 4 * 2^2.)  Likewise, x >> y is the same as floor(x / 2^y).

 

 

 

Also worth noting is that:

 

It's a bitwise operator, and you have to think about the data as a series of binary bits (rather than its ordinary datatype).

 

I think he meant to think about the data as represented in binary instead of decimal.  Technically the datatype stays the same, just the way we think about it is different.  $x = 15; and $x = 0xF; are both integers for example, but one is base 10 and the other is base 16.

Link to comment
Share on other sites

And here goes an explanation for why the result will be -2:

 

Say that you have four bit integers (PHP doesn't, but it saves me from typing a lot of 1's and 0's). When you are storing integers as signed, the maximum number will then be: 0111. When you shift 1 to the left you'll get 1110. The x86 architecture uses 2's complement for storing negative integers. Basically to switch sign (+/-) you flip all the bits and add one. So you get 0001 when you flip and 0010 when you add one. 0010 is +210, but we switched the sign so we had -210.

 

There is also something called 1's complement, and that's basically the flip only, but this results in having both positive and negative zero. Both of these are mathematically the same so someone came up with 2's complement instead.

 

Thought it might be useful to OP to have this information as well.

Link to comment
Share on other sites

Ooo!  Once you noted that it would go to -1 I got why.  I did not, however, ever realize why there was negative/positive 0.  Seems so duh now, but never thought about it lol.

 

So I guess it would also hold true that:

 

x << y will always = 0 if y > 31?

 

 

Oh, that's odd.

 

I see that:

 

x << y = x * 2^((y%31)-1)

 

What in the world?  Does it take the digits from the front and add them to the end when things start to overflow?  For example, in your 4 digit example, would 0010 << 5 turn into 0100?

 

 

 

Blerh, I need to go read up on bitwise operators lol.

Link to comment
Share on other sites

I did not, however, ever realize why there was negative/positive 0.

 

Only using 1's complement. The reason why we use 2's complement is to get rid of that though.

 

So I guess it would also hold true that:

 

x << y will always = 0 if y > 31?

 

No, not really, but if you do x >> y you are guaranteed to get 0 if y >= n-1 and x >= 0 on an n-bit system. I'm using a 64-bit version, PHP_INT_MAX = 2^63 = 9223372036854775807 for me. This means that x >> 63 = 0 for all x >= 0 and x >> 63 = -1 for all x < 0.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.