Content
@
0 reply
0 recast
2 reactions
emo.eth
@emo.eth
is there a “magic number” solution to flipping a (uint32) number’s endianness? or is masking and shifting each byte always the best way? cc @optimizoor
2 replies
1 recast
2 reactions
frangio
@frangio
According to Hacker's Delight, it doesn't look like there's an arithmetic solution. It proposes using a rotating shift that we can emulate in the EVM by first duplicating the number to 64 bytes using MUL. This should be better than masking and shifting each byte separately. In principle this should cost 41 gas.
1 reply
0 recast
2 reactions
emo.eth
@emo.eth
oooh this looks promising, lemme see what the opcodes work out to
1 reply
0 recast
0 reaction
frangio
@frangio
I think it is: PUSH5 0x0100000001 MUL DUP1 PUSH1 0x08 SHR PUSH4 0xff00ff00 AND SWAP1 PUSH1 0x18 SHR PUSH3 0xff00ff AND OR
1 reply
0 recast
2 reactions