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

Support bitwise operations for bytesX types #4305

Open
pcaversaccio opened this issue Oct 14, 2024 · 2 comments
Open

Support bitwise operations for bytesX types #4305

pcaversaccio opened this issue Oct 14, 2024 · 2 comments
Labels
bug - UX a bug related to UX enhancement

Comments

@pcaversaccio
Copy link
Collaborator

Summary

Add support for the bitwise unary operator ~, the bitwise binary operators &, |, and ^, and the shift operators >> and <<. An example use case can be packing:

@internal
def _pack_1_1(left: bytes1, right: bytes1) -> bytes2:
    left = left & convert((max_value(uint256) << 248), bytes1)
    right = right & convert((max_value(uint256) << 248), bytes1)
    return left | (right >> 8)
@pcaversaccio pcaversaccio added the needs triage needs triage label Oct 14, 2024
@charles-cooper charles-cooper added enhancement bug - UX a bug related to UX and removed needs triage needs triage labels Oct 15, 2024
@charles-cooper
Copy link
Member

i think &, | and ^ are fine. shift operators and inverter operator ~ might be restricted to bytes32. (i guess they also have sane implementations for other bytesM types, but need extra instructions to ensure the rotation/bit cleaning happens correctly).

@pcaversaccio
Copy link
Collaborator Author

pcaversaccio commented Oct 15, 2024

i think &, | and ^ are fine. shift operators and inverter operator ~ might be restricted to bytes32. (i guess they also have sane implementations for other bytesM types, but need extra instructions to ensure the rotation/bit cleaning happens correctly).

I'm fine with keeping the shift operators and the unary operator ~ to bytes32. The unary operator ~ for non-bytes32 types could be useful for some special use cases around math functions, where you want to get the max value of a type for example, i.e. ~empty(uint64) but since we also have max_value, the ergonomics is not improved by adding ~ to non-bytes32 types for this use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug - UX a bug related to UX enhancement
Projects
None yet
Development

No branches or pull requests

2 participants