0

I don't really get why a * 51 & 52 evaluates to false for 9, 14, 19, 24 in the code beneath. I understand that the interval is 5 but why is 51 and 52 used and what numbers would I use for e.g. an interval of 6?

for(               // loop :)
      b=a='';        // b - result , a - numeric variable
      a++<36;        // 
      b+=a*51&52  // if "a" is not 9 or 14 or 19 or 24
                  ?  //  return a random number or 4
         (
           a^15      // if "a" is not 15
              ?      // genetate a random number from 0 to 15
           8^Math.random()*
           (a^20?16:4)  // unless "a" is 20, in which case a random number from 8 to 11
              :
           4            //  otherwise 4
           ).toString(16)
                  :
         '-'            //  in other cases (if "a" is 9,14,19,24) insert "-"
      );
return b

Update 25/08/2014 at 15:35: Sorry, maybe my question was a bit unclear. I wanted a logical or mathematical explanation why the bitwise comparison only evaluates to false for 9, 14, 19, 24. I'm aware of what the bitwise operator does and how it works but I really don't get the logic of the used pattern above. The code is actually taken from a gist to generate random v4 UUIDs (see https://gist.github.com/LeverOne/1308368) and I would name it size optimised code rather than obfuscated code.

Thomas
  • 11
  • 1
  • 4
    This is just obfuscated code for the heck of it. Write real code. – Niels Keurentjes Aug 25 '14 at 08:23
  • 1
    This is the second question I saw having an obfuscated code today! First was this:- http://stackoverflow.com/questions/25481490/why-is-1-a-and-1111-b-in-javascript – Rahul Tripathi Aug 25 '14 at 08:26
  • @NielsKeurentjes Isn't obfuscated code real code? Does it work? Yes, so it's real code. – Teejay Aug 25 '14 at 09:12
  • If code needs this many long comments it's pointless by definition unless in a highly performance critical location like a system kernel. The code would even be shorter if not obfuscated, and potentially faster because an optimizer can actually understand patterns in it. – Niels Keurentjes Aug 25 '14 at 09:39
  • @NielsKeurentjes If you look at the link posted above, you'll see that Thomas is trying to understand the tricks underlying obfuscators. – Teejay Aug 25 '14 at 09:46

2 Answers2

1

Actually, a*51&52 evaluates to 0 that is false (and not true) for 9, 14, 19, 24.

If you open Windows Calculator in Programmer mode and you type 9*51 And 52 you will see it yourself.

There's for sure also a mathematical reason for this, you can try to investigate more...

Teejay
  • 7,210
  • 10
  • 45
  • 76
0

Well it's something as easy as:

51_10 is 110011_2
52_10 is 110100_2
9*52_10 = 459_10 is 111001011_2

If you take a*51 and 52 and do binary and

00000110100  //52
00111001011  //459  = 51 * 9
01011001010  //714  = 51 * 14
01111001001  //969  = 51 * 19
10011001000  //1224 = 51 * 24

you will get

00000000000

every time. 0 = false.

Reference : wiki

Margus
  • 19,694
  • 14
  • 55
  • 103