[Nottingham] A Little Coding Gem

nlug at lists.grepular.com nlug at lists.grepular.com
Thu Nov 15 19:55:03 UTC 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 15/11/12 18:08, Martin wrote:

> Fortune has just served up a little beauty for me just now:
> 
> 
> n = ((n >>  1) & 0x55555555) | ((n <<  1) & 0xaaaaaaaa); n = ((n >>
> 2) & 0x33333333) | ((n <<  2) & 0xcccccccc); n = ((n >>  4) &
> 0x0f0f0f0f) | ((n <<  4) & 0xf0f0f0f0); n = ((n >>  8) &
> 0x00ff00ff) | ((n <<  8) & 0xff00ff00); n = ((n >> 16) &
> 0x0000ffff) | ((n << 16) & 0xffff0000);
> 
> -- Yet another mystical 'C' gem. ...
> 
> Anyone like to guess or even work out what it does? :-> (Evil grin
> :-) )

Seems to flip the order of bits in a 4 byte int. Eg:

  00000000 10000000 00000000 00001001

Becomes:

  10010000 00000000 00000001 00000000

Here's the C++ code I wrote to figure it out:

  http://pastebin.com/Bc5ngBgp

Be kind, I've not been programming C/C++ for very long. ;)

I can't think of a use case for this, but I'm sure that they exist.
What was the code being used for?

- -- 
Mike Cardwell  https://grepular.com/     http://cardwellit.com/
OpenPGP Key    35BC AF1D 3AA2 1F84 3DC3  B0CF 70A5 F512 0018 461F
XMPP OTR Key   8924 B06A 7917 AAF3 DBB1  BF1B 295C 3C78 3EF1 46B4
-----BEGIN PGP SIGNATURE-----

iQGGBAEBCgBwBQJQpUlCMBSAAAAAACAAB3ByZWZlcnJlZC1lbWFpbC1lbmNvZGlu
Z0BwZ3AuY29tcGdwbWltZTgUgAAAAAAVABpwa2EtYWRkcmVzc0BnbnVwZy5vcmdt
aWtlLmNhcmR3ZWxsQGdyZXB1bGFyLmNvbQAKCRCdJiMBwdHnBL1wB/4sJg2MerZG
sITPa+b96h4vrLHqDwoDFvbJByo+s44Rh5+185IZ5D3knd2/NmikTo7GnFVM0K5C
H/czPlXKFg7uPKzEFVtpOHq5S+6eGCS2PEthIrBjOKj0zhto6JtateSLpLigDeJ1
5U/dA09wODgcoc/MpVU3syrjPmz3VoDBvAkbIpzSyDGr8Yj2IMruSxJqTSJD79tv
/Xz0JzCer2ZVAuKSNCKPVtpIVOJEK7rAV1pyM9DzHvmVHtKnSWkCdhMcTC4NxBrI
piVoXbFDL3Ob85vKK27uQ0RqpJVRQwSwxr8It7o1Cd5YoMgf3LIKrR4Y7msUMO40
cBvStv5x41/z
=MquY
-----END PGP SIGNATURE-----



More information about the Nottingham mailing list