working of << operator??

Discussion in 'PHP' started by viron86, Feb 18, 2010.

  1. #1
    <?php
    $a = 1;
    $b = 3;
    //$c = 4;
    //$d = 8;
    //$e = 1.0;
    //$f = $c + $d * 2;
    //$g = $f % 20;
    //$h = $b - $a + $c + 2;
    //$i = $h << $c;
    //$j = $i * $e;
    //print $j;
    
    echo $a << $b;
    ?>
    PHP:
    can anyone tell me the function of << operator

    thanks
     
    viron86, Feb 18, 2010 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    Shift the bits of $a $b steps to the left (each step means "multiply by two")
    so 1*2 + 3*2 = 2+6
     
    crivion, Feb 18, 2010 IP
  3. superdav42

    superdav42 Active Member

    Messages:
    125
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I'm not sure what mathmatical formula you are using there but this makes more sense to me:
    $a * 2^$b = 1 * 2^3 = 1 * 8 = 8

    This function is really only used if you care about the binary representation of your number in this case $a is 1 and $a << $b = 1000

    It's usually used in combination with bitwise & and | for setting flags and hash functions and stuff in cases where you want to conserve memory.
     
    superdav42, Feb 18, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    joebert, Feb 18, 2010 IP