[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Fwd: how this C code works?



Try this: really weird code to swap values of two int-type variables
without overflows in C or Perl (and maybe other insignificant
languages like Java and Python also... as if anyone cared):

        i^=j^=i^=j

C isn't too bad sometimes... just that Perl is better ;-)  I would
never have shifted from C to Perl if there were even one thing which I
could do in C and not in Perl... haven't found that missing bit yet.

-- Raju

>>>>> "Diwakar" == Diwakar  <- tamil.com" <diwakaran@xxxxxxxxx>> writes:

    Diwakar> this came from a friend of mine. can somebody crack this?
    Diwakar> - Diwakar This came as the Unix fortune - can someone
    Diwakar> decipher? Thanks, Alan -

    Diwakar> #define BITCOUNT(x) (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F)
    Diwakar> % 255) #define BX_(x) ((x) - (((x)>>1)&0x77777777) \ -
    Diwakar> (((x)>>2)&0x33333333) \ - (((x)>>3)&0x11111111))


    Diwakar> -- really weird C code to count the number of bits in a
    Diwakar> word
-- 
Raju Mathur          raju@xxxxxxxxxxxxx           http://kandalaya.org/