0

What does

radius |= 0;

do?

Found in these blur functions here: http://www.quasimondo.com/BoxBlurForCanvas/FastBlur.js

gang
  • 1,758
  • 2
  • 23
  • 36
  • Isn't thatsomething that you could have easily found out by trying and looking at the result? – Tomalak Jul 02 '14 at 08:56
  • Then I would have had to try out every possible value, still not knowing what the intention of this was. – gang Jul 02 '14 at 09:00
  • No, you would have tried out three or four values and then you would have started to notice a certain pattern. – Tomalak Jul 02 '14 at 09:02

1 Answers1

3

Converts radius to integral value, since | is a bit-operator (which force a 32-bit integer value).

radius |= 0

would be equivalent to

radius = (castToInt32(radius > 0 ? Math.floor(radius) : Math.ceil(radius))) | 0

if such a function as castToInt32() existed. | 0 is an idiomatic JavaScript way to do the same thing. The operation itself is a no-op, since OR-ing each bit with 0 will return the same bit (1 | 0 == 1, 0 | 0 == 0), the benefit is the casting of the value.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Is this the most performant way to do it? – gang Jul 02 '14 at 09:04
  • [Yes](http://jsperf.com/cast-to-int32), or at least one of. On my computer, at least. (I believe these all do the same thing, but... I could have made a mistake.) Notice that the mathy way to do it is orders of magnitude slower, while all bit-manipulation ways are more or less the same. – Amadan Jul 02 '14 at 09:30