LooieENG Posted November 22, 2009 Share Posted November 22, 2009 I've looked everywhere and I just can't get my head around it Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 22, 2009 Share Posted November 22, 2009 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) Quote Link to comment Share on other sites More sharing options...
LooieENG Posted November 22, 2009 Author Share Posted November 22, 2009 Okay, thanks. I think I understand now (luckily I learned about binary about a month ago in a computing course I'm doing). What I don't understand though, is if you do 4 << 2 What's the significance of the 4 and the 2? Quote Link to comment Share on other sites More sharing options...
corbin Posted November 22, 2009 Share Posted November 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 22, 2009 Share Posted November 22, 2009 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). Only when you're within the range of integers. If you do PHP_INT_MAX << 1 you'll get -2. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 22, 2009 Share Posted November 22, 2009 Oh... I didn't think about that. ;p Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 22, 2009 Share Posted November 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 23, 2009 Share Posted November 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 23, 2009 Share Posted November 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 23, 2009 Share Posted November 23, 2009 Hrmmm, yeah I should read about it again lol. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.