Hacker tips

From miki
Revision as of 12:17, 24 January 2020 by Mip (talk | contribs) (→‎Endian swap)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Hex

See Hex.

Bit manipulation

Endian swap

For 32-bit:

#pseudo code
v = (v <<< 24) & 0xff00ff00 | (v <<< 8) & 0x00ff00ff

#pseudo code - variant using 2x same 32-bit constant
M = 0xff00ff00
v = (v <<< 24) & M | ((v & M) <<< 8)

#pseudo code - variant using 2x smaller 32-bit constant
M = 0x00ff00ff
v = ((v & M) <<< 24) | (v <<< 8) & M

This only requires 2 rotations, 2 bitwise and and 1 bitwise or.