二進制與位運算(數學篇)


PS:本文主要介紹位運算的數學性質,和OI沒有太大關聯.

Part0:符號約定

\([p]\):艾弗森記號.對於命題\(p\),當\(p\)成立時,\([p]\)\(1\),否則為\(0\).

\(x_i\):\(x\)在二進制下的第\(i\)位數.

Part1:二進制

對於任意的非負整數\(x\),眾所周知,其可以表示為:

\[x=\sum_{i=0}^n 10^i b_i \]

其中,\(n=\lfloor \log_{10} x\rfloor,b_i\in\{0,1,2,\dots,9\}(i=1,2,\dots,n)\).我們把該式中的所有\(10\)都換成\(2\),則有:

\[x=\sum_{i=0}^n 2^i b_i \]

其中,\(n=\lfloor \log_2 x\rfloor,b_i\in\{0,1\}(i=1,2,\dots,n)\).我們將數位

\[\overline{b_n b_{n-1} \dots b_1 b_0}_{(2)} \]

稱為\(x\)二進制(分解),這種分解是惟一的.為方便,我們在本文中簡記\(x_i=b_i\),為\(x\)在二進制下的第\(i\)位數.

Part2:二進制遞推式與位移運算

我們來考慮\(x,\lfloor \frac x2\rfloor,2x\)二進制之間的關系.因

\[x=\sum_{i=0}^n 2^i x_i \]

故有

\[\left\lfloor \frac x2\right\rfloor=\left\lfloor \frac{\sum\limits_{i=0}^n2^i x_i}2\right\rfloor=\left\lfloor \frac{\sum\limits_{i=1}^n 2^i x_i}2 + \frac{x_0}2\right\rfloor \]

顯然左邊的加數整除\(2\).又\(x_0\in\{0,1\}\),故\(\frac{x_0}2=0\).所以

\[\left\lfloor \frac x2\right\rfloor=\sum_{i=1}^n 2^{i-1}x_i=\sum_{i=0}^{n-1} 2^i x_{i+1} \]

這相當於把整個二進制往右移\(1\)位,並把不為整數的部分截掉,比如,\(11=1011_{(2)}\),則

\[\left\lfloor\frac{11}2\right\rfloor=\left\lfloor \frac{1011_{(2)}}2\right\rfloor=101_{(2)}=5 \]

再來考慮\(2x\).

\[x=\sum_{i=0}^n 2^i x_i \]

\[2x=\sum_{i=0}^n 2^{i+1} x_i=\sum_{i=1}^{n+1} 2^i x_{i-1} \]

這相當於把整個二進制往左移\(1\)位,並在低位補全零,比如,\(6=110_{(2)}\),則

\[2\times 6=2\times{110_{(2)}}=1100_{(2)}=12 \]

更一般地,有

\[\left\lfloor\frac{x}{2^k}\right\rfloor=\sum_{i=0}^{n-k}2^i x_{i+k}\\ 2^kx=\sum_{i=k}^{n+k}2^i x_{i-k} \]

其中\(k\in\mathbb{N}^+\).我們把這兩種運算分別稱為右移(right shift)左移(left shift),分別記為

\[x\gg k=\left\lfloor\frac{x}{2^k}\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) \]

這樣就可以快速地求出一個非負整數的二進制表示了.

Part3:與運算和或運算

對於兩個非負整數\(x,y\),設

\[x=\sum_{i=0}^n 2^ix_i,\\ y=\sum_{i=0}^m2^iy_i, \]

定義

\[x\ \text{and}\ y=x\land y=\sum_{i=0}^{\min\{n,m\}}2^i[x_i=1\land y_i=1]=\sum_{i=0}^{\min\{n,m\}} 2^i x_iy_i=\sum_{i=0}^{\min\{n,m\}}2^i\left\lfloor\frac{x_i+y_i}2\right\rfloor \]

\(x\)\(y\)與運算(and operation).而

\[x\ \text{or}\ y=x\lor y=\sum_{i=0}^{\max\{n,m\}} 2^i[x_i=1\lor y_i=1]=\sum_{i=0}^{\max\{n,m\}} 2^i \left\lceil\frac{x_i+y_i}2\right\rceil \]

\(x\)\(y\)或運算(or operation).顯然有

\[x\land y\le\max\{x,y\}\\ x\lor y\ge\min\{x,y\} \]

Part4:取反

一個數的取反運算(not operation)定義為

\[\lnot x=\sum_{i=0}^n 2^i[x_i=0] \]

相當於把值為\(1\)的位改成\(0\),把值為\(0\)的位改成\(1\).一般地,有

\[x+\lnot x=2^{n+1}-1,\\ \lnot\lnot x=x. \]

Part5:異或

這是我們要重點討論的位運算.其定義為

\[x\ \text{xor}\ y=x\oplus y=\sum_{i=0}^{\max\{n,m\}}2^i[x_i\ne y_i]=\sum_{i=0}^{\max\{n,m\}}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)\).

Part6:位運算方程

我們把形如只含有以下三種運算的方程叫做位運算方程(組)(bit operation equations):

\(1.\)位移常數

\(2.\)異或

\(3.\)取反

顯然,這幾種運算是可逆的.先來一道簡單題.考慮位運算方程組

\[\begin{cases} x\oplus y=z\\ \lnot z=4\\ x\gg 2=1 \end{cases} \]

根據第三個方程,易知

\[x=1\ll 2=4. \]

根據第二個方程,有

\[z=\lnot 4=3. \]

因此,

\[y=z\oplus x=3\oplus 4=7. \]

所以原方程組的解為

\[\begin{cases} x=4,\\ y=7,\\ z=3. \end{cases} \]

再來考慮位運算方程組:

\[\begin{cases} x\oplus y\oplus z=w\\ (\lnot x)\oplus y=6\\ x\gg 2=1\\ z\oplus (\lnot y)=9 \end{cases} \]

顯然有\(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{cases} x=4,\\ y=5,\\ z=11,\\ w=10. \end{cases} \]

本文完


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM