ps:本文主要介紹位運算的數學性質,和oi沒有太大關聯.
\([p]\):艾弗森記號.對於命題\(p\),當\(p\)成立時,\([p]\)為\(1\),否則為\(0\).
\(x_i\):\(x\)在二進位制下的第\(i\)位數.
對於任意的非負整數\(x\),眾所周知,其可以表示為:
\[x=\sum_^n 10^i b_i
\]其中,\(n=\lfloor \log_ x\rfloor,b_i\in\(i=1,2,\dots,n)\).我們把該式中的所有\(10\)都換成\(2\),則有:
\[x=\sum_^n 2^i b_i
\]其中,\(n=\lfloor \log_2 x\rfloor,b_i\in\(i=1,2,\dots,n)\).我們將數字
\[\overline \dots b_1 b_0}_
\]稱為\(x\)的二進位制(分解),這種分解是惟一的.為方便,我們在本文中簡記\(x_i=b_i\),為\(x\)在二進位制下的第\(i\)位數.
我們來考慮\(x,\lfloor \frac x2\rfloor,2x\)二進位制之間的關係.因
\[x=\sum_^n 2^i x_i
\]故有
\[\left\lfloor \frac x2\right\rfloor=\left\lfloor \frac^n2^i x_i}2\right\rfloor=\left\lfloor \frac^n 2^i x_i}2 + \frac2\right\rfloor
\]顯然左邊的加數整除\(2\).又\(x_0\in\\),故\(\frac2=0\).所以
\[\left\lfloor \frac x2\right\rfloor=\sum_^n 2^x_i=\sum_^ 2^i x_
\]這相當於把整個二進位制往右移\(1\)位,並把不為整數的部分截掉,比如,\(11=1011_\),則
\[\left\lfloor\frac2\right\rfloor=\left\lfloor \frac}2\right\rfloor=101_=5
\]再來考慮\(2x\).
因\[x=\sum_^n 2^i x_i\]故
\[2x=\sum_^n 2^ x_i=\sum_^ 2^i x_
\]這相當於把整個二進位制往左移\(1\)位,並在低位補全零,比如,\(6=110_\),則
\[2\times 6=2\times}=1100_=12
\]更一般地,有
\[\left\lfloor\frac\right\rfloor=\sum_^2^i x_\\
2^kx=\sum_^2^i x_
\]其中\(k\in\mathbb^+\).我們把這兩種運算分別稱為右移(right shift)和左移(left shift),分別記為
\[x\gg k=\left\lfloor\frac\right\rfloor\\
x\ll k=2^kx
\]特別地,當\(k>n\)時,\(x\gg k=0\).右移的意義是將二進位制往右移\(k\)位,並將不為整數的部分截掉;左移的意義是講二進位制往左移\(k\)位,並在低位補全零.我們很容易得到:
\[x=(x\gg1)\ll1 + (x\bmod 2)
\]這就是二進位制的遞推式.亦即:
\[x=2\left\lfloor\frac x2\right\rfloor+(x\bmod 2)
\]這樣就可以快速地求出乙個非負整數的二進位制表示了.
對於兩個非負整數\(x,y\),設
\[x=\sum_^n 2^ix_i,\\
y=\sum_^m2^iy_i,
\]定義
\[x\ \text\ y=x\land y=\sum_^}2^i[x_i=1\land y_i=1]=\sum_^} 2^i x_iy_i=\sum_^}2^i\left\lfloor\frac2\right\rfloor
\]為\(x\)與\(y\)的與運算(and operation).而
\[x\ \text\ y=x\lor y=\sum_^} 2^i[x_i=1\lor y_i=1]=\sum_^} 2^i \left\lceil\frac2\right\rceil
\]為\(x\)與\(y\)的或運算(or operation).顯然有
\[x\land y\le\max\\\
x\lor y\ge\min\
\]乙個數的取反運算(not operation)定義為
\[\lnot x=\sum_^n 2^i[x_i=0]
\]相當於把值為\(1\)的位改成\(0\),把值為\(0\)的位改成\(1\).一般地,有
\[x+\lnot x=2^-1,\\
\lnot\lnot x=x.
\]這是我們要重點討論的位運算.其定義為
\[x\ \text\ y=x\oplus y=\sum_^}2^i[x_i\ne y_i]=\sum_^}2^i(x_i+y_i\mod 2)
\]顯然有\(|x-y|\le x\oplus y\le x+y\).容易驗證,異或運算(exclusive or operation,xor operation)具有以下性質:
\(1.\)交換性:\(x\oplus y=y\oplus x\).
\(2.\)結合性:\(x\oplus y\oplus z=(x\oplus y)\oplus z=x\oplus (y\oplus z)\).
\(3.\)冪零性:\(x\oplus x=0\).
\(4.\)還原性:\(x\oplus y\oplus x=y\).特別地,有\(x\oplus 0=0\oplus x=x\).
\(4'.\)轉換性:\(w=x\oplus y\oplus z\rightarrow x=w\oplus y\oplus z\).
\(5.\)與其它位運算的關係:\(x\oplus y=(\lnot x\land y)\lor(x\land\lnot y)\).
我們把形如只含有以下三種運算的方程叫做位運算方程(組)(bit operation equations):
\(1.\)位移常數
\(2.\)異或
\(3.\)取反
顯然,這幾種運算是可逆的.先來一道簡單題.考慮位運算方程組
\[\begin
x\oplus y=z\\
\lnot z=4\\
x\gg 2=1
\end
\]根據第三個方程,易知
\[x=1\ll 2=4.
\]根據第二個方程,有
\[z=\lnot 4=3.
\]因此,
\[y=z\oplus x=3\oplus 4=7.
\]所以原方程組的解為
\[\begin
x=4,\\
y=7,\\
z=3.
\end
\]再來考慮位運算方程組:
\[\begin
x\oplus y\oplus z=w\\
(\lnot x)\oplus y=6\\
x\gg 2=1\\
z\oplus (\lnot y)=9
\end
\]顯然有\(x=4\).進一步帶入第二個方程有
\[y=6\oplus(\lnot x)=6\oplus 3=5.
\]故得
\[z=9\oplus (\lnot y)=9\oplus 2=11.
\]於是
\[w=x\oplus y\oplus z=4\oplus 5\oplus 11=10.
\]因此原方程組的解為
\[\begin
x=4,\\
y=5,\\
z=11,\\
w=10.
\end
\]
二進位制與位運算
目錄常見的位運算應用 計算機中,為了方便計算,整數一般在計算機中都以補碼的形式儲存 補碼與原碼的轉換 為了演示方便,以下位運算均以8位的byte進行操作,且計算機中位運算都是以補碼的形式參與位運算 按位與同為1則位1,否則為0 按位或全0為0,否則為1 按位非直接取反,遇1則0,遇0則1 按位異或相...
二進位制位運算
與運算,6 3 2 或運算 6 3 7 異或運算 6 3 5 反碼 6 7 左移 3 2 12 3 2 2 12 右移 3 1 1 3 2 1 無符號右移 3 1 1 3 2 1 按位與,當兩位同時為1時才為1如 1 1 1 1 0 0 0 1 0 6 3相當於 0110 0011 0010 2 按...
二進位制位運算
一 按位與 位全為1,結果才為1 0 0 0 0 1 0 1 0 0 1 1 1 特殊用法 清零。如果想將乙個單元清零,就是使其全部二進位制位為0,只要與乙個各位都為零的數值相與,結果為零 取乙個數中的指定位。如x 10101110要取x的低4位,用x 00001111 00001110即可 二 按...