Php bitwise file function

Can someone explain to me how this function works?

    $size=100 //kb
    if (filesize(file) > ($size << 10))
     echo "file too big";

How does it work $size << 10? How can I use MB instead of KB?

+3
source share
5 answers

The expression $size << 10shifts the bit pattern to the left 10 times, effectively multiplying by 1024; In other words, this $size * 1024.

Each time you do a left shift, you double the value. See also bitwise operators .

If you want to $sizemean the size in MB, you multiply by another 1024, i.e.

if ($filesize($file) > $size * 1024 * 1024) {
    echo "file too big";
}

Or:

if ($filesize($file) > $size << 20) {
    echo "file too big";
}
+5
source

, . << , , , :

  7 << 2
= 111 << 2 (7 = 111 in base two)
= 11100
= 28 (11100 = 28 in base ten)

, 1024 = 2 10 10, 10 , 1024.

+2

$size << 10 $size * pow(2, 10). 2 1024, . - pow(2, 20); $size << 20.

+1

filesize . $size KB. ($size << 10) , .

0

.

100 << 10 100 10. 102400

This is the same as multiplying by 1024.

It converts 100 kbytes to bytes, which returns filesize().

0
source

All Articles