javascript運算符
JavaScript按位運算符 (JavaScript Bitwise Operators)
A lot of times you come across some strange operators where you're knocking your head to understand what is going on in the code. Almost all the programming languages have bitwise operators. It is essential for a language to have these since they work at the binary level. Let's first understand what bitwise operators are and why they are the way they are for any general language or framework then we can dive into how to use them in JavaScript.
很多時候,您遇到了一些奇怪的運算符,您在這里敲了一下頭,以了解代碼中發生了什么。 幾乎所有的編程語言都具有按位運算符 。 語言對這些語言至關重要,因為它們在二進制級別起作用。 首先讓我們了解按位運算符是什么以及為什么它們對于任何通用語言或框架都是如此,然后我們將深入研究如何在JavaScript中使用它們。
In the number system, we have more than one way of representing numbers. Most of the time we are using the decimal numbers which have a base-10 but there are other types of numbers such as hexadecimal, binary, octal, etc. Bitwise operators interact with the numbers at the binary level. Since any machine understands only binary (a mere combination of the 0's and 1's), the way a machine understands what we're doing in a program is by converting into its own language of the 0's and 1's. That's why the most fundamental way of playing with numbers is by breaking them down at the binary level. When you store a variable or carry out even basic arithmetic operations, in reality, these operations are being done by first converting the base -10 numbers to binary numbers. Even variables stored in memory are referenced through binary numbers and memory addresses are themselves a hexadecimal value. So understanding Bitwise is a great way to understand how a machine is interpreting things and anything has done using them is always faster than what you'd achieve through some other operation.
在數字系統中,我們有多種表示數字的方法。 大多數時候,我們使用十進制數(以10為底),但還有其他類型的數字,例如十六進制,二進制,八進制等。 按位運算符在二進制級別與數字交互。 由于任何機器都只能理解二進制(0和1的組合),因此機器理解我們在程序中所做的工作的方式是將其轉換為自己的0和1語言。 這就是為什么使用數字的最基本方法是在二進制級別分解數字。 實際上,當您存儲變量或執行基本算術運算時,這些運算是通過首先將以-10為底的數字轉換為二進制數來完成的。 甚至存儲在內存中的變量也通過二進制數來引用,而內存地址本身就是一個十六進制值。 因此,了解Bitwise是了解機器如何解釋事物以及使用這些事物所做的任何事情的好方法,它總是比您通過其他操作獲得的結果更快。
Bitwise operators help us to carry out bit masking operations and play with the numbers at the binary level. We will look at the following most common bitwise operators in JavaScript.
按位運算符可幫助我們執行位屏蔽操作,并在二進制級別處理數字。 我們將研究以下JavaScript中最常見的按位運算符 。
JavaScript中按位運算符的列表 (List of Bitwise Operators in JavaScript )
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Bitwise Left Shift |
>> | Bitwise Right Shift |
操作員 | 描述 |
---|---|
和 | 按位與 |
| | 按位或 |
^ | 按位異或 |
? | 按位非 |
<< | 按位左移 |
>> | 按位右移 |
Note:
注意:
JavaScript stores numbers as 64 bits floating-point numbers, but all bitwise operations are performed on 32 bits binary numbers.
JavaScript將數字存儲為64位浮點數,但是所有按位運算都是對32位二進制數執行的。
Before a bitwise operation is performed, JavaScript converts numbers to 32 bits signed integers.
在執行按位運算之前,JavaScript將數字轉換為32位帶符號整數。
After the bitwise operation is performed, the result is converted back to 64 bits of JavaScript numbers.
執行按位運算后,結果將轉換回64位JavaScript數字。
Let's first see what they mean by carrying out some operations. The bitwise AND first converts the two numbers into their binary and then does the AND operation (logical multiplication) from the end bit by bit.
首先,通過執行一些操作來了解它們的含義。 按位與首先將兩個數字轉換為二進制,然后從末尾開始逐位執行與運算(邏輯乘法)。
Let's say we have two numbers; 5 and 1.
Binary of 5: 1001
Binary of 1: 0001
If we take AND of every digit from the right side we get 0001 which is 1 in decimal notation. We can simply use & to get the same result, ie, the Bitwise AND between two numbers.
如果我們從右邊取每個數字的AND ,我們將得到0001 ,即十進制表示法中的1 。 我們可以簡單地使用&來獲得相同的結果,即兩個數字之間的按位與 。
To tryout, this code follow the following rules,
要進行試用,此代碼應遵循以下規則,
Open the chrome dev console to try out the examples by right clicking on the browser → selecting inspect → selecting console or simply pree f12.
打開chrome開發人員控制臺,通過右鍵單擊瀏覽器→選擇檢查 →選擇控制臺或簡單地按f12來嘗試示例。

You can carry out these operations to verify the results. You simply need to change the decimal numbers to their binary equivalent and perform bit by bit operation and convert the binary so obtained back to decimal.
您可以執行這些操作來驗證結果。 您只需要將十進制數更改為等效的二進制數,然后逐位執行操作,然后將如此獲得的二進制數轉換回十進制數即可。
The Bitwise OR does logical addition of two binary numbers.
按位或對兩個二進制數進行邏輯加法運算。
The Bitwise XOR gives 1 if the bits are different and 0 otherwise.
如果這些位不同, 則按位XOR給出1,否則返回0。
The Bitwise NOT ~ simply inverses all bits of the binary representation of the number. Wherever you have a 0, you'll have a 1 and vice versa.
按位NOT?只是將數字的二進制表示形式的所有位求反。 無論您有0哪里,您都會有1,反之亦然。
Notice, how NOT ~ of a number gives us the negative of that number incremented by 1? This can be logically verified concerning how the computer stores a negative number.
請注意,數字的非?怎么給我們該數字的負數加1? 可以從邏輯上驗證有關計算機如何存儲負數的信息。
<< or the left shift operator pushes all the bits to the right and the leftmost one falls off.
<<或左移位運算符將所有位向右推,最左邊的一位掉落。
Binary of 5: 0101 5<<1=> 1010 which is the binary representation of 10.
5的二進制: 0101 5 << 1 => 1010 ,它是10的二進制表示形式。
Similarly, the right shift >> operator pushes all the bits to the left and rightmost falls off.
類似地, 右移>>運算符將所有位推到最左邊,最右邊下降。
Binary of 5: 0101 5>>1=> 0010 which is the binary representation of 2.
5的二進制數: 0101 5 >> 1 => 0010 ,它是2的二進制表示形式。
Let's verify these,
讓我們驗證一下,

What if you left shift a certain number a b times? Or right shift a b times?
如果您左移了某個數字ab次該怎么辦? 還是右移AB次?
In general, we can say a<<b is a*(2^b). If a=5, b=2 then 5*(2^2) which gives 5*4=20.
通常,我們可以說a << b是a *(2 ^ b) 。 如果a = 5 , b = 2然后5 *(2 ^ 2)給出5 * 4 = 20 。
Similarly, we can say a>>b is a/(2^b). If a=5, b=2 then 5/(2^2) which gives 5/4=1.
類似地,我們可以說a >> b是a /(2 ^ b) 。 如果a = 5,b = 2然后5 /(2 ^ 2)給出5/4 = 1 。
Okay now let's write a simple function that accepts two numbers and performs all the bitwise operations we have discussed so far.
好吧,現在讓我們編寫一個簡單的函數,該函數接受兩個數字并執行到目前為止討論的所有按位運算。
function bitwiseOperations(a=2,b=3){
console.log('${a} AND ${b} : ',a&b);
console.log('${a} OR ${b} : ',a|b);
console.log('${a} XOR ${b} : ',a^b);
console.log('NOT ${a} : ',~a);
console.log('NOT ${b} : ',~b);
console.log('Left Shift ${a} : ',a<<1);
console.log('${a} left shift ${b} : ',a>>b);
console.log('${a} right shift ${b} : ',a<<b);
}
Let's look at the application of the bitwise operator. Let's say we have an array in which every number is repeated twice except for one number that occurs only once. We need to find that unique number. There can be different approaches to this problem but an efficient method would be to XOR all elements of the array. Let's see this with an example,
讓我們看一下按位運算符的應用。 假設我們有一個數組,其中每個數字重復兩次,但一個數字僅出現一次。 我們需要找到該唯一編號。 可以使用不同的方法來解決此問題,但有效的方法是對數組的所有元素進行XOR 。 讓我們來看一個例子,
Say we have an array with the elements: 1,5,6,2,5,6,3,2,3
假設我們有一個包含以下元素的數組: 1,5,6,2,5,6,3,2,3
Clearly, the unique number is 1.
顯然,唯一數字是1 。
If we XOR all these numbers together due to the same bits in the repeated numbers, they will give 0.
如果由于重復數字中的相同位而將所有這些數字異或 ,它們將給出0 。
5^5=6^6=3^3=2^2=0.
5 ^ 5 = 6 ^ 6 = 3 ^ 3 = 2 ^ 2 = 0 。
And, when we XOR 1 and 0, we'll get 1. This way we can easily find the unique number.
而且,當我們對1和0進行 XOR運算時,將得到1 。 這樣,我們可以輕松找到唯一編號。
Let's implement this solution,
讓我們實施這個解決方案,
function findUnique(arr){
var ans=0;
for(let i=0; i<arr.length; i++)
ans=ans^arr[i];
return ans;
}

翻譯自: https://www.includehelp.com/code-snippets/bitwise-operators-in-javascript.aspx
javascript運算符