Why is this statement true in PHP?

I don't understand why this statement in PHP echos 'whaaa?' - (0x0F | 0xF0)

should 0xFF

n't be ?

if((0x0FFFFFFF | 0xF0FFFFFF) != 0xFFFFFFFF) echo 'whaaa?';

      

+2


source to share


2 answers


The result of this script:

var_dump((0x0FFFFFFF)); 
var_dump((0xF0FFFFFF)); 
var_dump((0x0FFFFFFF | 0xF0FFFFFF)); 
var_dump((0xFFFFFFFF)); 
var_dump(((0x0FFFFFFF | 0xF0FFFFFF)) != (0xFFFFFFFF));

      

is an

int(268435455)
float(4043309055)
int(-1)
float(4294967295)
bool(true)

      

PHP converts hex numbers over 31 bits to floats because the integer is signed and therefore can only contain 31 positive bits.

Hexadecimal numbers are unsigned, so the conversion makes sense.



The first "or" operation converts the float to an integer, since it makes no sense to do "or" on the float. So PHP converts float to int for or, the result is int, but the next hex conversion is float and the values ​​are not the same.

To convert float to integer sizable, OR with 0x0:

var_dump((0xFFFFFFFF | 0x0)); 
var_dump(((0x0FFFFFFF | 0xF0FFFFFF)) != (0xFFFFFFFF | 0x0));

      

leads to

int(-1)
bool(false)

      

-Adam

+1


source


Check it out by writing var_dump((0x0FFFFFFF | 0xF0FFFFFF))



+1


source







All Articles