ID:1959790
 
Resolved
The compiler optimized out certain bitwise operations: OR or XOR with 0, and AND with 0xFFFF. As a result, variables were not truncated properly to the range allowed by bitwise operations. E.g., (n & 0xFFFF) would not truncate n from 0x10000 to 0.
BYOND Version:508.1299, 509.1305
Operating System:Linux
Web Browser:Chrome 45.0.2454.101
Applies to:Dream Maker
Status: Resolved (509.1306)

This issue has been resolved.
Descriptive Problem Summary: The results of calculations such as x & 0xFFFF can be significantly higher than expected.

Numbered Steps to Reproduce Problem:
- Attempt to use bitwise AND on a variable and the literal value 0xFFFF
- Notice that the result is merely the value of the variable

Code Snippet (if applicable) to Reproduce Problem:
/world/New()
var/a = 0x10000
var/b = 0xFFFF
world.log << "[0xFFFF & 0x10000]" // output "0"
world.log << "[0xFFFF & a]" // output "65536"
world.log << "[ b & 0x10000]" // output "0"
world.log << "[ b & a]" // output "0"


Expected output of the above is four identical lines "0", given that all lines are identical if the values of variables are substituted for the variables.

Expected Results: Bitwise operations would return values with zeroed-out upper bits, so that the result is the same as if the operations were performed on 16-bit integers.

Actual Results: Bitwise AND applied to the literal value 0xFFFF and a variable returns the value of the variable.

Does the problem occur:
Every time? Or how often? Every time
In other games? N/A
In other user accounts? Unknown
On other computers? Unknown

When does the problem NOT occur? N/A

Did the problem NOT occur in any earlier versions? If so, what was the last version that worked? Unknown

Workarounds: Use % 0x10000 instead?



More cases, including with vars declared const:
/world/New()
var/a = 0x10000
var/b = 0xFFFF
var/const/ca = 0x10000
var/const/cb = 0xFFFF
world.log << "[0xFFFF & 0x10000]" // 0

world.log << "[0xFFFF & a]" // 65536
world.log << "[0xFFFF & ca]" // 0
world.log << "[ b & 0x10000]" // 0
world.log << "[ cb & 0x10000]" // 0

world.log << "[ a & b]" // 0
world.log << "[ a & cb]" // 65536
world.log << "[ca & b]" // 0
world.log << "[ca & cb]" // 0
Lummox JR resolved issue with message:
The compiler optimized out certain bitwise operations: OR or XOR with 0, and AND with 0xFFFF. As a result, variables were not truncated properly to the range allowed by bitwise operations. E.g., (n & 0xFFFF) would not truncate n from 0x10000 to 0.
As I'm editing it to include const vars doing the same. Nice!