Log in

View Full Version : How does this work $a <<= 2 ???



JasonDFR
03-16-2009, 09:08 AM
Can someone explain to me why this returns the value of $a * 4?


$a = 4;
echo $a <<= 2;

I don't understand this syntax <<= 2.

Schmoopy
03-16-2009, 10:47 AM
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 (http://uk3.php.net/manual/en/language.operators.bitwise.php)

Hope that was helpful to you.

JasonDFR
03-16-2009, 10:54 AM
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?

techietim
03-16-2009, 11:35 AM
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
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));

JasonDFR
03-16-2009, 11:49 AM
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

techietim
03-16-2009, 12:01 PM
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:


var_dump(5 & 4); //returns 4, because it was set in both instances
//4: 00000100
//5: 00000101


The | is used for combining bits.

Example:


<?php
var_dump(5 | 4 | 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

CrazyChop
03-17-2009, 05:05 PM
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.