0

I want to define a byte256 and I want to assign different sections of the byte to different variables, any literature or guidance is appreciated.

for further clarity I want to do something like

variable 0 = address 
variable 1 = address
variable 2 = value 
variable 3 = address
variable 5 = address
variable 4 = address

now Id want to do:

byte50:
    bit 0-10=  variable 0
    bit 10-20=  variable 1
    bit 20-30=  variable 2
    bit 30-40=  variable 3
    bit 40-50=  variable 4

I realize the above may not be correct at all(I'm just trying to give you an idea of what I'm trying to achieve). I would like to know firstly whether its feasible and if so how I should go on about it.

I have no idea how to handle this so I'd appreciate any pointers

1
  • 1
    Generally speaking, you are looking for a left shift operator. E.g., let's imagine I want to encode two uint8 variables in an uint16 one. Let-s denote these variables x_1, x_2 and x_all, respectively. x_all = x_1 << 8 + x_2 (It is equivalent to x_all = x_1 * (2**8) + x_2 (N.B. an address in Ethereum is a 160-bit (20 bytes) length variable). In this question you can find something similar with riight shift operator ethereum.stackexchange.com/questions/61826/…
    – Briomkez
    Commented Jan 24, 2019 at 10:57

1 Answer 1

0

You can find libraries useful for that. Search about memory optimization in Solidity. I’ll do for you as well and when found I’ll post the reference here.

It is a well known approach, not particularly efficient if managed in Solidity, easy to realize using assembly.

Found: read this https://link.medium.com/9cq5nlXqKT

In short it is suggested to write one function which access in writing and one which access in reading using a simple assembly in order to avoid to mask and shift. It is easy to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.