0

I was looking in the google "Bookmark bubble library" source code when I saw this function

/**
* Creates a version number from 4 integer pieces between 0 and 127 (inclusive).
* @param {*=} opt_a The major version.
* @param {*=} opt_b The minor version.
* @param {*=} opt_c The revision number.
* @param {*=} opt_d The build number.
* @return {number} A representation of the version.
* @private
*/
google.bookmarkbubble.Bubble.prototype.getVersion_ = function(opt_a, opt_b,opt_c, opt_d) {
// We want to allow implicit conversion of any type to number while avoiding
// compiler warnings about the type.
return /** @type {number} */ (opt_a) << 21 |
  /** @type {number} */ (opt_b) << 14 |
  /** @type {number} */ (opt_c) << 7 |
  /** @type {number} */ (opt_d);
};

I don't understand the double sign '<<' and the single '|'

if anybody understand, can he please increase my javascript knowledge and tell me how this "return" work ?

Thanks

3

1 Answer 1

0

Here is some simple JavaScript to see what is going on (using .toString(2) returns the binary representation of a number):

console.log((8).toString(2));
> 1000
console.log((8>>1).toString(2));
> 0100 (shift right one space)
console.log((8<<1).toString(2));
> 10000 (shift left one space)
console.log((8|9|5).toString(2));
> 1101 (combines the 3 numbers like this:

  1000
  1001
  0101
  ====
  1101 if any digit is 1 then return a 1 in that digit)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.