Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set the bits of UInt data with __setitem__. #552

Open
iSunfield opened this issue Apr 14, 2024 · 1 comment
Open

Cannot set the bits of UInt data with __setitem__. #552

iSunfield opened this issue Apr 14, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@iSunfield
Copy link

iSunfield commented Apr 14, 2024

I tried to implement code to extend the UInt[N] class to set a specific bit of UInt[N] type data, but it seems that the setitem argument, self, is given a copy of the UInt[N] data as an argument, so the setitem function I could not set a specific bit of UInt[N] type data. After checking with the following simple code, it seems that the cause is that the address of the A variable and the address of self in setitem are different. Is there a way to use setitem to set bits of data of type UInt[N]?

@extend
class UInt:
    def __setitem__(self,Slice,val):
        print(f"self of A address:{__ptr__(self)} , Slice:{Slice.start}:{Slice.stop}:{Slice.step} , Set value:{val}")
        self = UInt[N](int(val))

A = UInt[64](0)
B = UInt[32](1)

A[0:32] = B
print(f"A address:{__ptr__(A)}, Result:{A}")

self of A address:0x7ffe93eecbe8 , Slice:0:32:None , Set value:1
A address:0x7ffe93eecbb8, Result:0
@inumanag inumanag added the enhancement New feature or request label Sep 23, 2024
@inumanag
Copy link
Contributor

Hi @iSunfield

Ints are passed by value (not by reference), hence __setitem__ will receive a copy. Right now only correct way to do this is to do value |= value[5:8] if you have setitem implemented. You can also try passing __ptr__(val) to a function (then you get the address of the value) but that's very C-ish and quite ugly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants