Skip to content

Incorrect implementation of arithmetic right shift for negative numbers #2

Description

@Imran-imtiaz48

In your shiftRight function, you are trying to perform a right bit shift for both positive and negative integers. However, the logic used for negative numbers is mathematically incorrect.

if @x >= 0 
    set @y = CAST(@x / @pow AS INT)
else
    set @y = CAST(~@x / @pow AS INT)


For negative numbers, you are shifting by using ~@x, which is a bitwise NOT, not a signed right shift. This does not produce the correct result that a normal arithmetic shift would produce. The right shift for negatives should preserve the sign bit, but your version instead inverts and divides, creating incorrect decoded coordinates.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions