Jump to content

double left arrow


The Little Guy

Recommended Posts

<< 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

Link to comment
https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270029
Share on other sites

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).

Link to comment
https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270045
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/247297-double-left-arrow/#findComment-1270065
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.