Question

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

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

Solution

The result of this script:

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

is

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

PHP converts hexadecimal numbers larger than 31 bits into floats, as an integer is signed, and can therefore only hold 31 positive bits.

Hexadecimal numbers are unsigned, so the conversion makes sense.

The first "or" operation converts the float into an integer, as it doesn't make sense to perform an "or" on a float. So PHP converts the float to an int for the or, the result is an int, but the next hexadecimal conversion is a float, and the values are not the same.

To convert the float to a integer in a bitwise fashion, OR it with 0x0:

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

results in

int(-1)
bool(false)

-Adam

OTHER TIPS

Test it by writing var_dump((0x0FFFFFFF | 0xF0FFFFFF))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top