allworknoplay Posted February 16, 2009 Share Posted February 16, 2009 Sorry for this dumb math question, but what is the answer to this? $i = 16 << 4; Is $i = 256? I know that the left shift operator multiplies an integer by the powers of two and I have never been great at math... Link to comment https://forums.phpfreaks.com/topic/145457-left-shift-operator/ Share on other sites More sharing options...
drisate Posted February 16, 2009 Share Posted February 16, 2009 lol you just need to echo $i bro and you will find out hehe echo 16 << 4; Link to comment https://forums.phpfreaks.com/topic/145457-left-shift-operator/#findComment-763613 Share on other sites More sharing options...
allworknoplay Posted February 16, 2009 Author Share Posted February 16, 2009 lol you just need to echo $i bro and you will find out hehe echo 16 << 4; hahaha, I should have tried that, yes it is 256....I just want to make sure I get my facts straight, I'm basically saying, 16 to the power of 2, four times? 16 * 2 = 32 32 * 2 = 64 64 * 2 = 128 128 * 2 = 256 Link to comment https://forums.phpfreaks.com/topic/145457-left-shift-operator/#findComment-763616 Share on other sites More sharing options...
drisate Posted February 16, 2009 Share Posted February 16, 2009 Yeah :-) And you can also use the pow(x,y) function to do that Link to comment https://forums.phpfreaks.com/topic/145457-left-shift-operator/#findComment-763619 Share on other sites More sharing options...
allworknoplay Posted February 16, 2009 Author Share Posted February 16, 2009 Yeah :-) And you can also use the pow(x,y) function to do that When I use that function, I get 65536 instead. "pow(16x4)" VS "16 << 4" So the first one, I think is doing 16x16x16x16 which gives me 65536... The second one is 256... Link to comment https://forums.phpfreaks.com/topic/145457-left-shift-operator/#findComment-763626 Share on other sites More sharing options...
corbin Posted February 16, 2009 Share Posted February 16, 2009 Binary operators operate at a binary level (big surprise). a << x = a * 2^x On a binary level, let's say we have 5: 101 Now << 1 would add a 0 onto the end: 5 << 1 = 1010 = 10 (dec) See how it works? 53 << 7 = 110101 0000000 = 7296 Link to comment https://forums.phpfreaks.com/topic/145457-left-shift-operator/#findComment-763669 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.