This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What do these operators do?
I'm working with some javascript for html 5's canvas. I'm looking at some existing code and I've come across the following:
element.height >> 1
element.width >> 1
Its used as part of some arithmetic.
I am using prototype.js as well, if this helps.
>> is the bitwise shifting operator. So >> 1 basically shifts the binary representation of the number on the left by one to the right. This is equal to an integer division by 2.
So element.height >> 1 equals to Math.floor( element.height / 2)
It's a bitshift operator.
It's a sign-propagating right-shift; full explanation here: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators#.3E.3E_%28Sign-propagating_right_shift%29.
Shifting (bitshift) operator.
1 << 1
This shifts the bitpattern 00000001 to the left once (padding with 0s)- you get 00000010, which is 2.
1 << 2
shifts it by two, so you get 00000100, which is 4.
It comes useful when implementing binary protocols, where only 2 bits can mean something. Using shifting you can strip out the rest.
This is the sign-propagating right shift operator which shifts the digits of the binary representation of the first operand to the right by the number of places specified by the second operand, discarding any shifted off to the right. The copies of the leftmost bit are added on from the left, thereby preserving the sign of the number.
So in your case everything is shifted one place to the right.
Related
This question already has answers here:
Why does OR 0 round numbers in Javascript?
(3 answers)
Closed 5 years ago.
The Mozilla JavaScript Guide has this to say about the bitwise OR operator:
"Bitwise OR: Returns a zero in each bit position for which the corresponding bits of both operands are zeros."
However, when operand b is 1, the bitwise operator OR in the example below rounds up a. This is something I cannot wrap my head around. Also, seeing as a returns a non rounded number, it is also not clear to me how the bitwise operator truncates it down to two digits.
var a = Math.random()*100;
console.log(a);
console.log(a | 1);
Insightful explanations are very welcome.
The hidden factor here is bitwise operators cast the number to an integer before being applied. That is why the number gets rounded. The cast truncates any fractional part.
The bitwise OR itself then simply sets the first bit to 1. So may or may not increase the truncated number by 1 depending on whether it was odd or even after the truncation.
So it's not rounding it up in all cases - just 50% of the time.
I am trying to do a comparison between two numbers, however I only want that the n-th bit of one of the numbers to be equal a certain binary value
ex: assert (5==0b1XX) == true
since 5 is 0b101 and the 3rd MSB is 1
Is there anyway I can use a don't care (X) in javascript?
Use bitwise SHIFT >> along with bitwise AND & to achieve this.
// SHIFT off the first two bits, then check the first bit with AND
(0b101 >> 2 & 1) === 1
>> will shift bits to the right and discard any bits shifted off.
& will return a 1 when the corresponding bits are both 1.
Here is the MDN page on bitwise operators.
Here is a function you can use for any value in any position:
// returns true if 'target' has a 'value' at 'position'
function checkBit(target, position, value) {
return (target >> (position - 1) & 1) === value;
}
#hermbit is right. I'll take a shot at explaining it too.
You can shift the bit representation of a number by using the >> operator.
So if you have the number 5 0b101 and you shift it two places you get 0b001 (removed last two places and filled left side with 0s.
Then you can use the & to make a logical and of both numbers. In this case
0b001 & 0b001 is equal to 0b001, because it will return a number where the bits will be 1 only in the places where both numbers are 1.
I hope this clarifies things.
This question already has answers here:
What is the JavaScript >>> operator and how do you use it?
(7 answers)
Closed 5 years ago.
What is the meaning of the expression >>> in JavaScript? It is like type conversion, or what, and when it recommended to use?
I ran into that symbol (>>>) when I read this article and am a little confused.
Sorry, if my question is stupid, but I can not find any answers by Google search or other ways.
>>> is a bitwise operator.
>>> (Zero-fill right shift) This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the
right are discarded. Zero bits are shifted in from the left. The sign
bit becomes 0, so the result is always non-negative.
For non-negative numbers, zero-fill right shift and sign-propagating
right shift yield the same result. For example, 9 >>> 2 yields 2, the
same as 9 >> 2
From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
Why there are different results of bitwise left shift?
1 << 32; # 1
1 << 31 << 1; # 0
That's because of
Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.
of how the << operation is defined. See http://www.ecma-international.org/ecma-262/6.0/#sec-left-shift-operator-runtime-semantics-evaluation
So according to it - 32 & 0x1F equals 0
So 1 << 32 equals to 1 << 0 so is basically no op.
Whereas 2 consecutive shifts by 31 and 1 literally perform calculations
JavaScript defines a left-shift by 32 to do nothing, presumably because it smacks up against the 32-bit boundary. You cannot actually shift anything more than 31 bits across.
Your approach of first shifting 31 bits, then a final bit, works around JavaScript thinking that shifting so much doesn't make sense. Indeed, it's pointless to execute those calculations when you could just write = 0 in the first place.
The reason is that the shift count is considered modulo 32.
This itself happens because (my guess) this is how most common hardware for desktops/laptops works today (x86).
This itself happens because.... well, just because.
These shift limitations are indeed in some cases annoying... for example it would have been better in my opionion to have just one shift operator, working in both directions depending on the sign of the count (like ASH works for Common Lisp).
This question already has answers here:
What do these JavaScript bitwise operators do?
(3 answers)
Closed 8 years ago.
I've seen the following:
((2 * 45) + (2 * 124) + 100) >>> 3
Putting this in a console on its own reveals the value 54.
What is the purpose of >>> 3?
This is the Zero-fill right shift bitwise operator.
From the Mozilla Developer Network docs:
This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes 0, so the result is always non-negative.