1994-07-06 - Counting bits

Header Data

From: rishab@dxm.ernet.in
To: cypherpunks@toad.com
Message Hash: 6ef85e09ac98a0c034cbb2b442dd69eb3cc8f2e75b06a4739a3c146ca79bed48
Message ID: <gate.Pk12oc1w165w@dxm.ernet.in>
Reply To: N/A
UTC Datetime: 1994-07-06 14:03:20 UTC
Raw Date: Wed, 6 Jul 94 07:03:20 PDT

Raw message

From: rishab@dxm.ernet.in
Date: Wed, 6 Jul 94 07:03:20 PDT
To: cypherpunks@toad.com
Subject: Counting bits
Message-ID: <gate.Pk12oc1w165w@dxm.ernet.in>
MIME-Version: 1.0
Content-Type: text/plain


"Timothy L. Nali" <tn0s+@andrew.cmu.edu>:
> > Eli Brandt@jarthur.cs.hm 
> > int byte_ones(int a)
> > // hope this is correct...
> > {
> >         a = (a & 0x55) + (a & 0xAA)/2;          // 0x55 == 01010101b
> [...]
> 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
                                    ^^^^^^^^^
Let me guess: you're one of the lucky users of the RBO (Reverse Bit Order)
SuperDecryptor from the NSA, where the LSB is the one at the extreme left?

Or did you mean ((a & 0xAA) >> 1)   ;)

My personal preference for byte operands is unsigned char - this ensures
that right shifts are not sign-extended by the most brain dead compiler, and
might use only one byte for the parameter.

-----------------------------------------------------------------------------
Rishab Aiyer Ghosh             "Clean the air! clean the sky! wash the wind!
rishab@dxm.ernet.in                   take stone from stone and wash them..."
Voice/Fax/Data +91 11 6853410  
Voicemail +91 11 3760335                 H 34C Saket, New Delhi 110017, INDIA  





Thread