echo ($a&0x7f) + ($a&0x80?-128:0);
change , this mimics what should happen for a signed 8-bit value. When the MSB (bit 7) is zero, we simply have the value of these 7 bits. When the MSB is set to 1, we start with -128 (i.e., 1000000b == -128d ).
You can also use the fact that PHP uses integer values โโinside:
$intsize = 64; // at least, here it is... echo ($a<<($intsize-8))/(1<<($intsize-8));
so you shift the MSB byte to the MSB position for int, as php sees, i.e. you add 56 zero bits to the right. The section "deletes" these bits, preserving the sign of the value.
source share