Results 1 to 7 of 7

Thread: How does this work $a <<= 2 ???

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default How does this work $a <<= 2 ???

    Can someone explain to me why this returns the value of $a * 4?

    PHP Code:
    $a 4;
    echo 
    $a <<= 2
    I don't understand this syntax <<= 2.

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    You don't need the = , $a << 2; would suffice.

    It's called a left shift, with $a >> 2 being a right shift:

    I think it's the equivalent of a LSL (Logical Shift Left) I learnt whilst learning about bits and bytes.

    I'm not sure you know much about binary but it's a base 2 system, whereas we use a base 10 system (denary). As you go left, the number doubles

    1 0 1 0 1 0 1 1 - From the right -> left, 1, 2, 4, 8, 16, 32, 64, 128

    Where there's a 1 you count the number, where there's a 0 you don't.

    1 + 2 + 8 + 32 + 128 = 171

    Now let's do a shift left...

    1 0 1 0 1 0 1 1 0 = 342 (which is 171 * 2)

    This should help you with understanding this left shift.

    saying

    $a << 2;

    Is like saying do that left shift, twice, so if $a = 4

    4 * 2 = 8 <----- One shift
    8 * 2 = 16 <------ Two shifts

    For more on bitwise operators go here

    Hope that was helpful to you.

  3. The Following User Says Thank You to Schmoopy For This Useful Post:

    JasonDFR (03-16-2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Yeah, that was helpful. I still don't totally get it, as I haven't had experience with binary. How important do you think it is for a php programmer to understand well binary?

  5. #4
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP programmers don't use a lot of binary operators, although there is one concept that you should understand how to implement and use.

    Bit flags
    Example:
    PHP Code:
    <?php
    define
    ('WORD_TRIM'1);
    define('WORD_ROT'2);
    define('WORD_SLASHES'4);
    //8
    //16
    //32

    function word($str$flags 0){
        if(
    $flags WORD_TRIM){
            
    $str trim($str);
        }
        if(
    $flags WORD_ROT){
            
    $str str_rot13($str);
        }
        if(
    $flags WORD_SLASHES){
            
    $str addslashes($str);
        }
        return 
    $str;
    }
    var_dump(word('   Hello "World"'));
    var_dump(word('   Hello "World"'WORD_TRIM WORD_ROT WORD_SLASHES));

  6. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Again, I'm like halfway there with that Tim. When you have a minute could you explain exactly what is going on with the bitwise operators?

    Thanks a lot!

    J

  7. #6
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    The & operator is checking to see the the same bits are set on the right and left of it. It returns a non-zero number when true (used for bit flags).

    Example:
    PHP Code:
    var_dump(4); //returns 4, because it was set in both instances
    //4: 00000100
    //5: 00000101 
    The | is used for combining bits.

    Example:
    PHP Code:
    <?php
    var_dump
    (8); //Shows 13
    //5: 00000101
    //4: 00000100
    //8: 00001000
    //T: 00001101 - 13
    Full reference is here:
    http://php.net/manual/en/language.operators.bitwise.php

  8. #7
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    Try to google for logical operators and logical gates. The basics are AND, OR, NOT and XOR. Usually a bit-shift is use for multiplication/division by a number of the power of 2 because it is faster than the usual multiple/divide.

    This is quite a usual practice in C/C++ but personally I just prefer to use bool variables or an array of it for code readability. Hashing functions use lot of it too because of some mathematics mumbo-jumbo which for the life of me I couldn't understand.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •