Skip to content

Why is the supplied code behaving like explaint? #2131

Closed
@Dudeplayz

Description

@Dudeplayz

Hi,
I have the following function:

export function compute(pc : u32, opcode : u16) : u32{
  return pc + (((opcode & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0));
}

The function is called from JS site with the parameters compute(65, 63457).
The result of this function call is 65597. If I change the opcode to u32 the result is 61, which is the result I expect to get. For me, it looks like some type-overflow. I can also fix it by doing the following things:

return pc + (((opcode as u32 & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0));
return pc + (((opcode & 0x1f8 as u32) >> 3) - (opcode & 0x200 ? 0x40 : 0));
return pc + (((opcode & 0x1f8) >> 3 as u32) - (opcode & 0x200 ? 0x40 : 0));
return pc + (((opcode & 0x1f8) >> 3) as u32 - (opcode & 0x200 ? 0x40 : 0));

Modifying the right statement with as u32 does nothing.

The strange thing is if I do this:

export function compute() : u32{
  const pc : u32 = 65;
  const opcode : u16 = 63457
  return pc + (((opcode & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0));
}

... it also works.

So can someone explain to me what happens here?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions