javascript運算符_JavaScript中的按位運算符

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 )

OperatorDescription
&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來嘗試示例。

Bitwise Operators in JavaScript (Example 1)

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.

按位或對兩個二進制數進行邏輯加法運算。

Bitwise Operators in JavaScript (Example 2)

The Bitwise XOR gives 1 if the bits are different and 0 otherwise.

如果這些位不同, 則按位XOR給出1,否則返回0。

Bitwise Operators in JavaScript (Example 3)

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,反之亦然。

Bitwise Operators in JavaScript (Example 4)

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,

讓我們驗證一下,

Bitwise Operators in JavaScript (Example 5)

What if you left shift a certain number a b times? Or right shift a b times?

如果您左移了某個數字ab次該怎么辦? 還是右移AB次?

Bitwise Operators in JavaScript (Example 6)

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 << ba *(2 ^ b) 。 如果a = 5b = 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 >> ba /(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);
}

Bitwise Operators in JavaScript (Example 7)

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.

而且,當我們對10進行 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;
}

Bitwise Operators in JavaScript (Example 8)

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

javascript運算符

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377950.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377950.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377950.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[置頂] Android的IPC訪問控制設計與實現

3.3.1 IPC鉤子函數設計與實現 IPC Binder是Android最重要的進程間通信機制&#xff0c;因此&#xff0c;必須在此實施強制訪問控制。 1. 修改secuirty.h 打開終端shell&#xff0c;輸入指令“cd /android4.0/kernel/goldfish/include/linux/vim security.h”&#xff0c;找到結…

TensorFlow在Anaconda環境下創建

一、我使用的是Anaconda自帶的Jupyter編譯器&#xff0c;詳細的安裝教程可以參考博文 二、之后打開Jupyter 三、進行測試 我的tensorflow使用的是2.0版本 import tensorflow.compat.v1 as tf tf.disable_v2_behavior()a tf.constant([1.0,2.0],name"a") b tf.co…

leetcode 654. 構造最大二叉樹 思考分析

題目 給定一個不含重復元素的整數數組。一個以此數組構建的最大二叉樹定義如下&#xff1a; 二叉樹的根是數組中的最大元素。 左子樹是通過數組中最大值左邊部分構造出的最大二叉樹。 右子樹是通過數組中最大值右邊部分構造出的最大二叉樹。 通過給定的數組構建最大二叉樹&am…

Memcache的命令以及狀態監控

輸入telnet 127.0.0.1 11211&#xff08;memcached默認端口為11211&#xff09; stats &#xff1a;使用stats命令查看當前memcache服務器的狀態 pidmemcache服務器的進程IDuptime服務器已經運行的秒數time服務器當前的unix時間戳versionmemcache版本pointer_size當前操作系統 …

flush python_帶有示例的Python File flush()方法

flush python文件flush()方法 (File flush() Method) flush() method is an inbuilt method in Python, it is used to clear/flush the internal buffer, it is best practice while working with fila handling in Python, the internal buffer can be cleared before writin…

c++ 請拋棄匈牙利命名法 - 變量命名代碼風格的建議。

我只針對c碼農們講&#xff0c;其他語言不了解不過應該大同小異。曾幾何時翻開21天學通c系列等腦殘入門書&#xff0c;都以匈牙利命名法示人&#xff08;DWORD dwXXX, int nXXX, string strXXX)。現在我可以負責任的告訴你&#xff0c;把類型名寫在前面屁用都沒有&#xff0c;對…

Pycharm更換anaconda環境空間

一、File—>Settings 或者直接快捷鍵 CtrlAltS 二、找到自己的項目—>Project Interpreter—>找到需要使用的anaconda環境空間 三、Add Local 四、G:\Anaconda3\envs\mask_rcnn\python.exe一般anaconda的envs文件夾下&#xff0c;找到你的環境空間名稱&#xff0c;…

android 應用demo截圖

ksoap2實現天氣預報 Frame 動畫 baidu map 轉載于:https://www.cnblogs.com/java20130726/archive/2011/11/28/3218328.html

leetcode 617. 合并二叉樹 思考分析

題目 給定兩個二叉樹&#xff0c;想象當你將它們中的一個覆蓋到另一個上時&#xff0c;兩個二叉樹的一些節點便會重疊。 你需要將他們合并為一個新的二叉樹。合并的規則是如果兩個節點重疊&#xff0c;那么將他們的值相加作為節點合并后的新值&#xff0c;否則不為 NULL 的節點…

python 示例_帶有示例的Python File write()方法

python 示例文件write()方法 (File write() Method) write() method is an inbuilt method in Python, it is used to write the content in the file. write()方法是Python中的內置方法&#xff0c;用于將內容寫入文件中。 Syntax: 句法&#xff1a; file_object.write(text…

如何關掉Microsoft Office Click-to-Run服務

很煩&#xff0c;一開電腦就出現 一、打開任務管理器(CtrlShiftEsc) 服務—>打開服務 二、找到Microsoft Office Click-to-Run Service 右擊&#xff0c;選擇屬性 三、禁用即可

[2013-08-19] nohup的使用

前幾天自擺了一個烏龍。 由于項目中用到memcache&#xff1b;在linux機器上安裝了該服務后&#xff0c;啟動并且通過 & 設置到后臺進程&#xff1b; 由于要指定某些服務端口&#xff0c;然后發現經常服務被“莫名其妙”地關閉了。我以為是別人手動關掉了&#xff0c;或者說…

友盟—安卓巴士【Android開發原創教程大賽】

Android開發原創教程大賽正式拉開序幕了&#xff0c;由國內最專業的移動開發者服務及統計平臺-友盟提供豐厚的話費獎品哦&#xff0c;為所有愛寫教程的開發者提供的一份獎勵。 活動時間&#xff1a;2011年11月29日——2011年12月21日 征集期&#xff1a;2011年…

leetcode 700. 二叉搜索樹中的搜索 思考分析

目錄題目1、不考慮BST性質&#xff0c;直接遞歸遍歷2、回顧BST性質3、利用BST性質進行遍歷4、簡單的迭代方法題目 給定二叉搜索樹&#xff08;BST&#xff09;的根節點和一個值。 你需要在BST中找到節點值等于給定值的節點。 返回以該節點為根的子樹。 如果節點不存在&#xf…

一、環境準備

一、前提已安裝好anaconda 二、打開cmd命令窗口 conda activate激活base環境 conda create -n pytorch python3.6創建一個名稱為pytorch的環境空間&#xff0c;其中使用的python版本為3.6 conda env list查看當前都有哪些環境 conda activate pytorch 激活剛安裝的環境 pip l…

Java技能優化集錦

Java技能優化集錦 1 通用篇 “通用篇”討論的問題適合于大多數Java應用。 1.1 不用new關鍵詞創建類的實例 用new關鍵詞創建類的實例時&#xff0c;構造函數鏈中的所有構造函數都會被自動調用。但如果一個對象實現了Cloneable接口&#xff0c;我們可以調用它的clon…

java treemap_Java TreeMap size()方法與示例

java treemapTreeMap類的size()方法 (TreeMap Class size() method) size() method is available in java.util package. size()方法在java.util包中可用。 size() method is used to return the size of this TreeMap or in other words, we can say it returns the number of…

LeetCode 98. 驗證二叉搜索樹 思考分析

題目 給定一個二叉樹&#xff0c;判斷其是否是一個有效的二叉搜索樹。 假設一個二叉搜索樹具有如下特征&#xff1a; 節點的左子樹只包含小于當前節點的數。 節點的右子樹只包含大于當前節點的數。 所有左子樹和右子樹自身必須也是二叉搜索樹。 1、利用BST性質&#xff1a;中序…

二、VC++環境的安裝

一、打開PyCharm 在Terminal中激活pytorch環境&#xff0c;conda activate pytorch 安裝pycocotools工具&#xff0c;pip install pycocotools 二、報錯需安裝VC 進入官網 安裝完成之后重啟電腦 三、再次安裝pycocotools 打開Pycharm&#xff0c;安裝pycocotools工具&am…

Android 網絡狀態的監控

1 http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html 2 轉載于:https://www.cnblogs.com/bavariama/p/3268450.html