-1

Yet another question what does this operator do? How would I write that in C#:

   data[id] = R >> 0;
data[id + 1] = G >> 0;
data[id + 2] = B >> 0;
user2864740
  • 60,010
  • 15
  • 145
  • 220
Danielok1993
  • 359
  • 1
  • 6
  • 14
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators Btw, in this case author just casted a number to an integer in such a tricky way. That is another reason to prefer readable `Math.floor()` over tricky. – zerkms Mar 04 '14 at 22:39
  • It is important to first do a search for the answers. Self study is the only way to learn. @zerkms, nice link :) thanks. – alexmac Mar 04 '14 at 22:41
  • To Extend @zerkms comment http://msdn.microsoft.com/en-us/library/xt18et0d.aspx – Jon P Mar 04 '14 at 22:42
  • 1
    @Jon P: it's about VB, not JS – zerkms Mar 04 '14 at 22:42
  • Oops Wrong .net reference.... now C# not VB – Jon P Mar 04 '14 at 22:44

1 Answers1

3

I assume you're talking about the >> operator. It's a right shift operator that first (if necessary) converts the left argument to an integer and then shifts right by the indicated number of bits. Shifting by 0 bits leaves the number unchanged, so R >> 0 is a cute way of forcing R to an integer. It works like Math.floor(R) for non-negative values.

In C#, I believe that you can do the same thing with a cast: (int) R, etc.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    "a cute way" :-D Code is for humans, not for robots – zerkms Mar 04 '14 at 22:41
  • 1
    @zerkms - Yep. Robots don't appreciate cuteness. This is a fairly common idiom in JavaScript (as is using other bitwise operators, like `R | 0`, to do the same thing). Note that `Math.floor()` is not equivalent if there's a possibility that the left side is negative. Besides, the bitwise operator trick is quite a bit faster than calling `Math.floor()`, which may be important if this is in a tight rendering loop. – Ted Hopp Mar 04 '14 at 22:44
  • `is a cute way of forcing R to an integer` Is there an example that is compilable for float, double or decimal for ex `Adouble1>>0` – L.B Mar 04 '14 at 22:46
  • 1
    @Ted Hopp: seems like we have different sense of beauty for the code. For me it's readable and maintainable code, which consists of operators are used according their original design aims, not their tricky side effects. – zerkms Mar 04 '14 at 22:46
  • @L.B - The bitwise operator coerces the left argument to an integer, and it will work for any data type that is coercible to an integer value. Remember that JS is not a strongly typed language. – Ted Hopp Mar 04 '14 at 22:47
  • 1
    @L.B: in JS [everything is a float](http://www.ecma-international.org/ecma-262/5.1/#sec-8.5) isn't it? – zerkms Mar 04 '14 at 22:47
  • @People question is also tagged as c#. `How would I write that in C#` Answer it in c#. – L.B Mar 04 '14 at 22:48
  • @L.B - Just added a sentence to my answer about that. – Ted Hopp Mar 04 '14 at 22:49
  • 2
    +1. Answers the question. I don't think whether or not it matches someones personal taste is really that important. – Alexei Levenkov Mar 04 '14 at 22:50
  • Just to clarify: even though I don't not agree with a single "cute" word here - I didn't downvote :-) – zerkms Mar 04 '14 at 22:51
  • 1
    @zerkms - Perhaps I should avoid irony in my answers. I wasn't using "cute" to necessarily denote approval. After all, one [definition](http://dictionary.reference.com/browse/cute) of "cute" is "affecting [to give the appearance of] cleverness". – Ted Hopp Mar 04 '14 at 22:57
  • @Ted Hopp: no-no, irony perfectly fits SO community imho :-) It's just a matter of personal taste + some language issues (obviously english is my second language) – zerkms Mar 04 '14 at 23:08
  • `x >> 0` does not *strictly* "convert to an integer", rather it applies `[ToInt32]` (the result of which is *an* integer, in a subset range of all JavaScript integers). – user2864740 Mar 04 '14 at 23:25
  • @user2864740 - Well, according to [the spec](http://www.ecma-international.org/ecma-262/5.1/#sec-9.5), `[ToInt32]` "converts its argument to one of 2^32 integer values", so I don't see a lot of difference between that and "convert to an integer" (unless you were simply pointing out that the range of resulting values is restricted). – Ted Hopp Mar 04 '14 at 23:44