The Little Guy Posted September 16, 2011 Share Posted September 16, 2011 I was looking over some php code, and came across this: $c=($c*$j)<<2; I have never see << before, what does it mean/do? Quote Link to comment https://forums.phpfreaks.com/topic/247297-double-left-arrow/ Share on other sites More sharing options...
xyph Posted September 16, 2011 Share Posted September 16, 2011 It's a bitwise operator for shifting bits to the left http://php.net/manual/en/language.operators.bitwise.php Quote Link to comment https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270028 Share on other sites More sharing options...
ManiacDan Posted September 16, 2011 Share Posted September 16, 2011 << and >> are bitwise operators. They perform binary operations on integers. In particular, this line takes $c, multiplies it by $j, and then multiplies that result by 8. It's slightly faster to say $something << 3; than $something * 8; It's leftover from C programmers who used it a lot. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270029 Share on other sites More sharing options...
The Little Guy Posted September 16, 2011 Author Share Posted September 16, 2011 how are 3 and 8 the same thing? $something << 3; $something * 8; Quote Link to comment https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270041 Share on other sites More sharing options...
Pandemikk Posted September 16, 2011 Share Posted September 16, 2011 how are 3 and 8 the same thing? $something << 3; $something * 8; It's a bitwise operator for shifting bits to the left http://php.net/manual/en/language.operators.bitwise.php Read the article he linked. It was linked for a purpose. 3 and 8 are not the same. $x << 3 and $x * 8 are the same (essentially). Quote Link to comment https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270045 Share on other sites More sharing options...
KevinM1 Posted September 16, 2011 Share Posted September 16, 2011 3 in this case means shifting the binary representation of the number 3 positions to the left. That's the same as multiplying it by 2^3, or 8. Quote Link to comment https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270058 Share on other sites More sharing options...
xyph Posted September 16, 2011 Share Posted September 16, 2011 Here's what happens, step by step, if you're still confused <?php // OUtput in plain text header("Content-type: text/plain"); // Format is using 0 to pad the left side, 16 length, and binary $format = '%016b'; $c = 5; $j = 7; echo '$c*$j: '.sprintf($format,($c*$j))."\n"; echo '($c*$j)<<2: '.sprintf($format,(($c*$j)<<2))."\n"; echo 'Same as $c*$j with bits moved 2 to the left'; ?> $c*$j: 0000000000100011 ($c*$j)<<2: 0000000010001100 Same as $c*$j with bits moved 2 to the left Quote Link to comment https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270065 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.