Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Right shifting an high I256 does not work #1840

Closed
chikko80 opened this issue Nov 7, 2022 · 2 comments
Closed

Right shifting an high I256 does not work #1840

chikko80 opened this issue Nov 7, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@chikko80
Copy link

chikko80 commented Nov 7, 2022

Version
ethers = 1.0.0 / 0.17.0 (same problem on old version)

Platform
M1 Mac, as well as x64 linux

Description
Bug? in bit-shifting an negative I256

[short summary of the bug]
When I try to right shift an I256 with 128 i get the wrong result - The result that is shows is 340282366920938463463374607431768090784 but the correct result is -120672

I tried this code:

use ethers::types::I256;

  fn main() {
      let negativ_number =
        I. 256::from_dec_str("-41062328663447644311028663865158126991809745").unwrap();
      dbg!(negativ_number >> 128);
  {

I expected to see this happen:
Screen Shot 2022-11-07 at 9 58 26 PM
-120672

Instead, this happened: [explanation]

340282366920938463463374607431768090784

@chikko80 chikko80 added the bug Something isn't working label Nov 7, 2022
@mattsse
Copy link
Collaborator

mattsse commented Nov 7, 2022

cc @dbelv

@chikko80
Copy link
Author

chikko80 commented Nov 7, 2022

answered by @prestwich

use function asr()
the rightshift operator is implemented as a bitwise shift. asr() is the arithmetic right shift

@chikko80 chikko80 closed this as completed Nov 7, 2022
@dbelv dbelv mentioned this issue Nov 14, 2022
3 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants