Complex, time-consuming isTrue's



  • Some of the code I've written has to make use of a horrid function to do some binary manipulation on a number (there's no choice in the matter). So I thought, why not use that to put together an isTrue?

    int t(int x){return (x&(x|~x)&~x)?x&(x^~x):((((((x&(~((1<<(1<<4))-1))
    )>>(1<<4))|((x&((1<<(1<<4))-1))<<((1<<5)-(1<<4))))&(~((1<<(1<<4))-1))
    )>>(1<<4))|(((((x&(~((1<<(1<<4))-1)))>>(1<<4))|((x&((1<<(1<<4))-1))
    <<((1<<5)-(1<<4))))&((1<<(1<<4))-1))<<((1<<5)-(1<<4))))?x|~x:x&~x;}
    

    Do excuse it being called "t" and not "isTrue", that's the name I gave it in testing. See if you can work out what it's up to, just by looking at it.



  • Elementary mistake, I'm afraid. HTML's silly like that.

    int t(int x){return (x&(x|~x)&~x)?x&(x^~x):((((((x&(~((1<<(1<<4))-1)))>>(1                                        <<4))|((x&((1<<(1<<4))-1))<<((1<<5)-(1<<4))))&(~((1<<(1<<4))-1)))>>(1<<4))
    |(((((x&(~((1<<(1<<4))-1)))>>(1<<4))|((x&((1<<(1<<4))-1))<<((1<<5)-(1<<4))
    ))&((1<<(1<<4))-1))<<((1<<5)-(1<<4))))?x|~x:x&~x;}
    


  • @Two9A said:

    Elementary mistake, I'm afraid. HTML's silly like that.

    int t(int x){return (x&(x|~x)&~x)?x&(x^~x):((((((x&(~((1<<(1<<4))-1)))>>(1                                        <<4))|((x&((1<<(1<<4))-1))<<((1<<5)-(1<<4))))&(~((1<<(1<<4))-1)))>>(1<<4))
    |(((((x&(~((1<<(1<<4))-1)))>>(1<<4))|((x&((1<<(1<<4))-1))<<((1<<5)-(1<<4))
    ))&((1<<(1<<4))-1))<<((1<<5)-(1<<4))))?x|~x:x&~x;}

    Is this part of an IOCCC entry?

    Anyway, I reckon it's a form of isTrue, which returns an int with all
    bits set (i.e. -1, or 0xffffffff assuming 32 bit ints, which your code
    does) for true, and 0 for false.  My reasoning and simlification of the
    expression is at http://www.andrewr.co.uk/other/tdwtf-obfuscated-isTrue.txt

    Me? Bored? Well, it might be a possibility... ;-)



  • The code's in fact inspired by a function I have in my code to rotate a 32-bit value to the right; shift right, and stuff the bits that drop out back into the value on the left. So it's doing a rotate by 16 right, then rotating that value 16 bits right; which as you've correctly surmised leaves simply (x).

    I might hack it up some more, and possibly make it part of an IOCCC entry. It's not likely to win anything, other than the "Silliest use of binary operators" prize perhaps. (They have those, right?)

    Apologies for the formatting of the post, by the way. Somehow, the forum software kept a whole load of spaces where I asked for a newline. Love the forum software.



  • I suppose hacking up some ASM code that uses the ROR and ROL instructions would be purpose-defeative?


Log in to reply