Why is this statement true in PHP?
✔ Recommended Answer
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)
Source: stackoverflow.com
Answered By: Adam Davis
The result of the script you provided is:
scssint(268435455)
float(4043309055)
int(-1)
float(4294967295)
bool(true)
The var_dump()
function is used to output the values of the expressions to the console.
var_dump((0x0FFFFFFF));
- The value0x0FFFFFFF
represents the maximum value of a 32-bit unsigned integer in hexadecimal format. Thevar_dump()
function outputs an integer value of268435455
to the console.var_dump((0xF0FFFFFF));
- The value0xF0FFFFFF
represents a 32-bit integer with the most significant byte set to0xF0
. Since the value is greater than the maximum integer value in PHP, it is represented as a float. Thevar_dump()
function outputs a float value of4043309055
to the console.var_dump((0x0FFFFFFF | 0xF0FFFFFF));
- The bitwise OR operation between0x0FFFFFFF
and0xF0FFFFFF
results in0xFFFFFFFF
, which represents the maximum value of a 32-bit unsigned integer. Thevar_dump()
function outputs an integer value of-1
to the console.var_dump((0xFFFFFFFF));
- The value0xFFFFFFFF
represents the maximum value of a 32-bit unsigned integer in hexadecimal format. Thevar_dump()
function outputs a float value of4294967295
to the console.var_dump(((0x0FFFFFFF | 0xF0FFFFFF)) != (0xFFFFFFFF));
- The expression evaluates totrue
because the bitwise OR operation between0x0FFFFFFF
and0xF0FFFFFF
results in0xFFFFFFFF
, which is not equal to the maximum value of a 32-bit unsigned integer. Thevar_dump()
function outputs a boolean value oftrue
to the console.
Comments
Post a Comment