PHP moving bits into code?

Can someone help me understand this ancient code?

$a = 00040000000002;
$n = sscanf($a,"%02x%02x%02x%02x%02x%02x%02x",$r[7],$r[6],$r[5],$r[4],$r[3],$r[2],$r[1]);

$ptemp = $r[1] + (($r[2] & 0x1F) << 8);
$l[$i] = (($r[2] & 0xE0) >> 5) + ($r[3] << 3);
$m[$i] = $r[4] + (($r[5] & 0x03) << 8);
$h[$i] = (($r[5] & 0xFC) >> 2) + (($r[6] & 0x03) << 6);
$dist[$i] = (($r[6] & 0xFC) >> 2) + (($r[7] & 0x0F) << 6);
$fruittoday[$i] = ($r[7] & 0xF0) >> 4;

I understand sscanf, but I'm not sure what is happening with & 0x1f << 8, etc.

Any ideas?

+3
source share
1 answer

They use Bitwise operators to check if certain bits are turned on.

In the example you indicated, they confirm that the first 5 bits are 1, and then shift all bits to the left by 8 places. (Although it seems that in this case they start with 0 (00000000), checking if 0x1F (00011111) is turned on, which will still be 0, and then shift them left by 8 places (placed in 2 ^ 8th position)

php . , . , , ( ), ).

+3

Source: https://habr.com/ru/post/1781472/


All Articles