Can someone explain to me exactly what this bit of solidity logic is doing? I know that it takes a random number (r) and manipulates it to produce 2 unique bytes32 variables. In this way you can use the same random number to create 2 unique values. However I want to be able to create 4 unique values but since I do not understand what this logic is doing I do not know how to create another 2 variables. I think it has to do with bit shifting (?)

bytes32 value1 = bytes32( uint(r) & (2**64-1 << (64 * 3))) >> (64 * 3);
bytes32 value2 = bytes32( uint(r) & (2**64-1 << (64 * 2))) >> (64 * 2);

Aug 26, 2023, 9:47 PM
Its kind off a long explanation but to modify the code to return a different value while still entering the same value r, you can change the bitwise operations in the code. This way;


bytes32 value1 = bytes32(uint(r) & (2**64-1 << (64 * 3))) >> (64 * 3);
bytes32 value2 = bytes32(uint(r) & (2**64-1 << (64 * 2))) >> (64 * 2);

// Modify the bitwise operations to return different values
bytes32 newValue1 = bytes32(uint(r) & (2**64-1 << (64 * 4))) >> (64 * 4);
bytes32 newValue2 = bytes32(uint(r) & (2**64-1 << (64 * 5))) >> (64 * 5);


In this modified code, newValue1 and newValue2 will have different values compared to value1 and value2. You can adjust the shift values (64 * 4) and (64 * 5) according to your requirements.
Aug 27, 2023, 9:21 AM
Hey I tried this but get an error. It seems (64 * 3) is the limit and going higher than 3 causes the complier to error. This was the main reason why I asked what is happening in the expression because the complier doesn't give much info and I think there is a certain reason why those initial values were used.
Aug 29, 2023, 1:46 AM

© 2024 Draquery.com All rights reserved.