How to make PHP handle a uint-like number

I have a C # code that looks like this:

uint a = 0x9E3779B9;
a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));

After this code, a == 228 452 386

Now I am trying to translate this C # code into PHP, but in PHP the number does not overflow the same:

$a = 0x9E3779B9;
$a += ($url[$k+0] + ($url[$k+1] << 8) + ($url[$k+2] << 16) + ($url[$k+3] << 24));

After this code, $ a == 4,523,419,682

In both cases, "url" is treated as an array of ascii values. Returns the same results until $ a is added to the result of the second row. At this point, C # uint overflows to ~ 228 million. PHP becomes smart and comes up with the right answer.

But I want a crowded answer that C # gives. What should I do?

+3
source share
2 answers

$a &= 0xFFFFFFFF, 32- , +=.

(: http://www.ideone.com/6BR0U)

+5

, , , :

$a = 0x9e3779b9;
$url = 'info';

$a = ($a + current(unpack('L', $url))) & 0xffffffff;

printf("%u\n", $a); // 228452387

unpack() uint32 ( ). 32- , printf() .

0

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


All Articles