Dr. Dobbs Dev. Update 1/5 July 94 & Schneier

Eli Brandt ebrandt at jarthur.cs.hmc.edu
Sun Jul 3 11:24:03 PDT 1994


> Note that some compilers might not be smart enough to use logical shift
> ops and instead use expensive division ops.  Just to be safe...
> 
> int byte_ones(int a)
> {
>          a = (a & 0x55) + ((a & 0xAA) << 1);          // 0x55 == 01010101b
>          a = (a & 0x33) + ((a & 0xCC) << 2);          // 0x33 == 00110011b
>          a = (a & 0x0F) + ((a & 0xF0) << 4);          // 0x0F == 00001111b
>          return a;
> }

One advantage of writing it as division is that it's hard to accidentally
reverse, as above.  :-)  I was just trying to cut down on parens...

   Eli   ebrandt at hmc.edu







More information about the cypherpunks-legacy mailing list