UInt256 returns wrong value

Can anyone let me know why this number returns a wrong value?

number = UInt256(1) << 243
print(number)
$ mojo life.mojo
1434776518227074636666380005943348126619871175004951664972849610340958208

I think that is correct?

❯ python
Python 3.9.11 (main, Mar 29 2022, 14:04:34) 
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print(1<<243)
14134776518227074636666380005943348126619871175004951664972849610340958208

They are 2 different numbers:

1434776518227074636666380005943348126619871175004951664972849610340958208
14134776518227074636666380005943348126619871175004951664972849610340958208
1 Like

What mojo version / system are you running on?

I’m getting the same result:

# scratch.mojo
def main():
    var number = UInt256(1) << 243
    print(number)

❯ magic run mojo run scratch.mojo
14134776518227074636666380005943348126619871175004951664972849610340958208
❯ python
Python 3.9.11 (main, Mar 29 2022, 14:04:34) 
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 14134776518227074636666380005943348126619871175004951664972849610340958208 == 14134776518227074636666380005943348126619871175004951664972849610340958208
True
>>> 14134776518227074636666380005943348126619871175004951664972849610340958208 == 1 << 243
True
  • AlmaLinux 10
  • Mojo 25.4.0.dev2025061116 (26345f4f)

I think we simply didn’t update SIMD.write_to after introducing the larger integer types. Change 64 to max(64, dtype.bitwidth()) and it works.

2 Likes

Since it’s a very simple fix, are you willing to maybe contribute a patch?

I do not really understand the source code. Would you please commit this patch?

No worries. I will get it fixed soonish. Thanks for reporting!

5 Likes