--- title: 你所不知道的 C 語言:bitwise 操作 image: https://imgur.com/IpzUymh.png description: 複習數值系統,從應用案例引導學員掌握 bitwise 操作,從而邁向專業的程式開發 tags: DYKC, CLANG, C LANGUAGE --- # [你所不知道的 C 語言](https://hackmd.io/@sysprog/c-prog/): bitwise 操作 Copyright (**慣C**) 2020 [宅色夫](http://wiki.csie.ncku.edu.tw/User/jserv) ==[直播錄影](https://youtu.be/S800OmVnN5o)== ## 複習數值系統 * 短片: [10 進位、16 進位,和 60 進位從何而來?](https://www.youtube.com/watch?v=8J7sAYoG50A) * 複習 [數值系統篇](https://hackmd.io/@sysprog/c-numerics) * [解讀計算機編碼](https://hackmd.io/@sysprog/binary-representation) * [Binary and Number Representation](https://www.bottomupcs.com/chapter01.xhtml) :notes: 記得要按右下方的 Next > 以下圖例取自 [CS:APP 第 2 章重點提示和練習](https://hackmd.io/@sysprog/CSAPP-ch2) ![](https://i.imgur.com/H33tREX.png) 不是所有的位元組合都能表示合理的數字,存取某些位元組合在特定機器上可能會造成嚴重錯誤,此種組合稱作陷阱表示法 (trap representation)。除非使用位元運算或是違反標準其他規定 (如溢位),一般的運算不可能產生陷阱表示法。 C 語言標準明確允許實作自行決定在以下兩種狀況下是否是陷阱表示法: * 型態為有號整數且正負號及值位元為特定組合時 (三種格式各有一特殊組合) * 填充位元為某些組合時 :notes: [N1256 註腳# 44, 45](http://port70.net/~nsz/c/c99/n1256.html#note44) ![](https://i.imgur.com/DGJUVgk.png) 位元運算會忽略填充位元,因此(等級不低於 unsigned int 的)無號整數可安心使用。為求最大可攜性,位元運算不應該用在有號整數上。 在 C11 規格 6.2.6.2 Integer types 指出 > For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2^N−1^, so that objects of that type shall be capable of representing values from 0 to 2^N−1^ using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified. `uintN_t` 和 `intN_t` 保證沒有填充位元,`intN_t` 一定是二補數,而且 `intN_t` 不可能有陷阱表示法,堪稱是最安全的整數型態。實作可能不提供這些型態,不過一旦提供,即保證符合性質。 :notes: [N1256 7.18.1.1](http://port70.net/~nsz/c/c99/n1256.html#7.18.1.1) p1~p3 位移運算子(Shift operator): * 左移: `x << y` : x 左移 y 位元,左移出的位元會被丟棄,右側會補上 0 * 右移: `x >> y` : x 右移 y 位元,右移出的位元會被丟棄。 兩種位移: * 邏輯位移 (Logical shift) : 左側會補上 0 * 算術位移 (Arithmetic shift) : 補上號數 (sign bit) 也就是最高有效位元的值在左側 例: X = 10100010; Logical shift: `x >> 2` = `00101000` Arithmetic shift: `x >> 2` = `11101000` 注意位移運算的兩種未定義狀況 * 左移超過變數長度,其結果未定義; ```cpp int i=0xFFFFFFFF; i = i << 32; // 此結果未定義 ``` * 右移一個負數時,可能是邏輯位移或是算術位移,C 語言標準未定義; 算術位移的應用,若要判斷一個 int 型態的變數 `n` 是否為正數,可用 `n >> 31` 其等價於 `n >=0 ? 0: -1`. * 右移如果是一個負數時,會變成正或著是負值,要注意編譯器如何實作。編譯器甚至可以有編譯選項可改變此語意,gcc 的實作上是使用 arithmetic shift。 ![](https://i.imgur.com/HAhmxxQ.png) 有號數 x 到無號數 (U 結尾) 映射機制:非負數不變為 x,負數轉換為大的正數 (x + 2^w^,最靠近 0 的負數映射最大,最小的負數被映射為剛好在二補數表達之外的無符號數)。 * 例如 w = 4,二補數能表示的有號數範圍是 -8 [`1000`] ~ 7 [`0111`] 當無號數與有號數在 C 語言混合在單一表示式時,有號數會被轉換為無號數。在運算子操作時會有些意外的結果。有號數負值會因為轉換成 2 補數後,反而負值會大於正值。因此要注意混用時的運算狀況。 案例: | 變數 1 | 變數 2 | 比較結果 | 轉換後 | | ------------ | ----------------- | ---- | -------- | | 0 | 0U | == | unsigned | | -1 | 0 | < | signed | | -1 | 0U | > | unsigned | | 2147483647 | -2147483647-1 | > | signed | | 2147483647U | -2147483647-1 | < | unsigned | | -1 | -2 | > | signed | | (unsigned)-1 | -2 | > | unsigned | | 2147483647 | 2147483648U | < | unsigned | | 2147483647 | (int) 2147483648U | > | signed | 一個導致意外結果的案例: ```cpp int n = 10; for (int i = n - 1 ; i - sizeof(char) >= 0; i--) printff("i: 0x%x\n",i); ``` 這個例子會造成無窮迴圈,因為 `sizeof` 會回傳值是 unsigned int 型態,i 變數也會被轉換為 unsigned 的形式,無號數 0 再減 1 就會變為 `0xFFFFFFFF` 而產生無窮迴圈。 ![](https://i.imgur.com/G2AXbpz.png) 在有號整數上都可能產生陷阱表示法 補充資訊: [Writing TMin in C](http://csapp.cs.cmu.edu/3e/waside/waside-tmin.pdf) * Figure 1 列出 ISO C90 和 ISO C99 對於資料型態定義的落差 * 注意 Implications ![](https://i.imgur.com/7YvR10S.png) 延伸閱讀: [Signed and Unsigned Numbers](https://chi_gitbook.gitbooks.io/personal-note/content/signed_and_unsigned_numbers.html) 在 [Whirlwind Tour of ARM Assembly](http://www.coranac.com/tonc/text/asm.htm) 指出,若 n 是有號 32-bit 整數,那麼 `n >> 31` 相當於 `n >= 0 ? 0 : -1` :-1: [bitwise left shift operation invokes Undefined Behaviour when the left side operand has negative value](https://stackoverflow.com/questions/3784996/why-does-left-shift-operation-invoke-undefined-behaviour-when-the-left-side-opera) :-1: [Weird behavior of right shift operator (1 >> 32) ](https://stackoverflow.com/questions/3394259/weird-behavior-of-right-shift-operator-1-32/3394302#3394302) 若 n 是 32-bit 整數,那麼 `abs(n)` 等同於 `((n >> 31) ^ n) - (n >> 31)` * 當 n 是正數時: > `n >> 31` 是 0; `n ^ 0` 仍是 n; `n - 0` 仍是 n * 當 n 是負數時: > `n >> 31` 是 -1; `-1` 以 2 補數表示為 `0xFFFFFFFF`; `n ^ (-1)` 等同於 1 補數運算; 最後再減 `-1`,得到 2 補數運算的值 ![](https://i.imgur.com/cWiCbnq.png) 例如: unsigned int to unsigned short ## Bitwise Operator * [C Bitwise Operators Quiz](http://doc.callmematthi.eu/C_Bitwise_Operators.html) * [線上測驗](https://pconrad.github.io/old_pconrad_cs16/topics/bitOps/) (附上解答) * [Bitwise Practice](https://web.stanford.edu/class/archive/cs/cs107/cs107.1202/lab1/practice.html) ## 做中學 Linux 核心原始程式碼存在大量 bit(-wise) operations (簡稱 `bitops`),頗多乍看像是魔法的 C 程式碼就是 bitops 的組合。這裡舉個案例,觀察下方 ASCII 字元編號的二進位數值,可知道大寫 'A' 字元的編號是十進位 65,對應於二進位的 100_0001 (請略去底線,只是為了視覺方便),即 2 的 6 次方加 1,那麼小寫 'a' 字母是緊接在大寫 'Z' 字母之後嘛?不是!小寫 'a' 字母的編號是十進位 97,恰好跟大寫 'A' 字母相隔 32,即 2 的 5 次方。 ![](https://i.imgur.com/AipH6Qc.png) 為什麼 ASCII 採用不直覺的設計呢?我們繼續觀察: * 大寫 'G' => 71 => 0100_0111 * 小寫 'g' => 103 => 0110_0111 從二進位表示法清楚得知,'G' 和 'g' 其實就差 1 個位元,換言之,透過 XOR 0010_0000 (或 C 語言的 ^ 32),就可實作出「大寫字母轉小寫」和「小寫字母轉大寫」的效果。 小提示:在 GNU/Linux, FreeBSD, macOS 上輸入 "man ascii" 即可得到精美的 ASCII 表格,有八進位和十進位表示 (至於為何 UNIX 系統和 C 語言存在八進位表示法,稍後再談) ### Set a bit :::info where n is the bit number, and 0 is the least significant bit ::: ```cpp unsigned char a |= (1 << n); ``` Example: ``` a 1 0 0 0 0 0 0 0 a |= (1 << 1) = 1 0 0 0 0 0 1 0 a |= (1 << 3) = 1 0 0 0 1 0 0 0 a |= (1 << 5) = 1 0 1 0 0 0 0 0 ``` ### Clear a bit: ```cpp unsigned char b &= ~(1 << n); ``` - [ ] Example 1: ``` b 1 1 1 1 1 1 1 1 b &= ~(1 << 1) = 1 1 1 1 1 1 0 1 b &= ~(1 << 3) = 1 1 1 1 0 1 1 1 b &= ~(1 << 5) = 1 1 0 1 1 1 1 1 ``` - [ ] Example 2: Write a macro with two arguments a and pos, where a is a byte and the pos specifies a bit position of the byte. The macro should clear the particular bit position in the given byte. ```cpp #include <stdio.h> /* bit clear: a: int, pos: bit position to clear */ #define CLEARBIT(a, pos) (a &= ~(1 << pos)) int main() { /* 'z': decimal value 122 (=01111010) */ char a = 'z'; /* clearing the 5th bit */ char aOut = CLEARBIT(a, 5); /* aOut = 'Z': decimal value 90 (=01011010) */ printf("aOut=%c\n", aOut); return 0; } ``` ### Toggle a bit: ```cpp unsigned char c ^= (1 << n); ``` Example: ``` c 1 0 0 1 1 0 1 1 c ^= (1 << 1) = 1 0 0 1 1 0 0 1 c ^= (1 << 3) = 1 0 0 1 0 0 1 1 c ^= (1 << 5) = 1 0 1 1 1 0 1 1 ``` Test a bit: ```cpp unsigned char e = d & (1 << n); //d has the byte value. ``` ### The right/left most byte assuming 16 bit, 2-byte short integer: ```cpp unsigned char right = val & 0xff; /* right most (least significant) byte */ unsigned char left = (val >> 8) & 0xff; /* left most (most significant) byte */ ``` ### sign bit assuming 16 bit, 2-byte short integer, two's complement: ```cpp bool sign = val & 0x8000; // sign bit ``` ### Setting and Clearing a Bit The code below shows how to set or clear a bit of an integer. ```cpp #include <stdio.h> #include <stdbool.h> void binary(unsigned int n) { for (int i = 256; i > 0; i /= 2) { if (n & i) printf(" 1"); else printf(" 0"); } printf("\n"); } bool getBit(int n, int index) { return ((n & (1 << index)) > 0); } int setBit(int n, int index, bool b) { if (b) return (n | (1 << index)); int mask = ~(1 << index); return n & mask; } int main() { int num = 16, index; printf("Input\n"); for (int i = 7; i >= 0; i--) printf("%d ", getBit(num,i)); printf("\n"); /* set bit */ index = 6; printf("# Setting %d-th bit\n", index); num = setBit(num, index, true); for (int i = 7; i >= 0; i--) printf("%d ", getBit(num,i)); printf("\n"); /* unset (clear) bit */ index = 4; printf("# Unsetting (Clearing) %d-th bit\n", index); num = setBit(num, index, false); for (int i = 7; i >= 0; i--) printf("%d ", getBit(num,i)); printf("\n"); return 0; } ``` Output is: ``` Input 0 0 0 1 0 0 0 0 # Setting 6-th bit 0 1 0 1 0 0 0 0 # Unsetting (Clearing) 4-th bit 0 1 0 0 0 0 0 0 ``` 延伸閱讀: [Bitwise Operators in C](http://www2.mta.ac.il/~hbinsky/c%20content/Bits.pdf) * Uses of Bitwise Operations or Why to Study Bits * Compression * Set operations * Encryption * The Magic of XOR > `x ^ y == (~x & y) | (x & ~y)` --- ## bitwise 練習題 * [2018q1 考古題](https://hackmd.io/s/rJh9U4Guf) / [參考解答](https://hackmd.io/s/S1f0tSyTG) * [2018q3 考古題](https://hackmd.io/s/S1a9-YO_Q) / [參考解答](https://hackmd.io/s/By5KCaZam) * [2019q1 考古題1](https://hackmd.io/@sysprog/SyrZMGYr4) / [參考解答](https://hackmd.io/@chenishi/S1DHQ3KrE) / [延伸閱讀](https://medium.com/fcamels-notes/%E4%BD%BF%E7%94%A8%E4%BD%8D%E5%85%83%E8%A8%88%E7%AE%97%E5%8A%A0%E9%80%9F%E6%B1%82%E8%A7%A3-n-queen-d20442f34110) * [2019q1 考古題2](https://hackmd.io/@sysprog/H1Pik8M8E) / [參考解答](https://hackmd.io/@1IzBzEXXRsmj6-nLXZ9opw/B1vrcKcu4) --- ## 影像處理和 bitwise operation ![](https://i.imgur.com/Lw7Qcln.png) 可從 rasterization, equalizer, 和算術去思考。 * [Jitblt: Efficient Run-time Code Generation for Digital Compositing](http://vpri.org/pdf/tr2008002_jitblt.pdf) * [GitHub](https://github.com/damelang/jitblt) - [ ] pipeline-1 ![](https://i.imgur.com/OdTz4zy.png) - [ ] pipeline-2 ![](https://i.imgur.com/ZXwTw4J.png) * 新的發展: [Blend2D](https://blend2d.com/) * 2D vector graphics engine written in C++. * It features a built-in JIT compiler that generates high performance 2D pipelines at runtime, which are much faster than static pipelines used in today's 2D engines. Dynamic pipeline construction is the main difference between Blend2D and other 2D engines, and guarantees the highest possible throughtput by taking advantage of CPU extensions detected at runtime. * Blend2D provides also a deferred and asynchronous rendering, which makes it seamless to integrate with event-based environments such as node.js and next generation UI toolkits. 像素處理的程式碼片段: ```cpp unsigned short blend(unsigned short fg, unsigned short bg, unsigned char alpha) { // Split foreground into components unsigned fg_r = fg >> 11; unsigned fg_g = (fg >> 5) & ((1u << 6) - 1); unsigned fg_b = fg & ((1u << 5) - 1); // Split background into components unsigned bg_r = bg >> 11; unsigned bg_g = (bg >> 5) & ((1u << 6) - 1); unsigned bg_b = bg & ((1u << 5) - 1); // Alpha blend components unsigned out_r = (fg_r * alpha + bg_r * (255 - alpha)) / 255; unsigned out_g = (fg_g * alpha + bg_g * (255 - alpha)) / 255; unsigned out_b = (fg_b * alpha + bg_b * (255 - alpha)) / 255; // Pack result return (unsigned short) ((out_r << 11) | (out_g << 5) | (out_b); } ``` 其中 "Alpha blend components" 可透過改寫為以下: ```cpp unsigned out_r = fg_r * a + bg_r * (255 - alpha); unsigned out_g = fg_g * a + bg_g * (255 - alpha); unsigned out_b = fg_b * a + bg_b * (255 - alpha); out_r = (out_r + 1 + (out_r >> 8)) >> 8; out_g = (out_g + 1 + (out_g >> 8)) >> 8; out_b = (out_b + 1 + (out_b >> 8)) >> 8; ``` ### 案例分析 給定每個 pixel 為 32-bit 的 RGBA 的 bitmap,其轉換為黑白影像的函式為: ```cpp void rgba_to_bw(uint32_t *bitmap, int width, int height, long stride) { for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { uint32_t pixel = bitmap[col + row * stride / 4]; uint32_t a = (pixel >> 24) & 0xff; uint32_t r = (pixel >> 16) & 0xff; uint32_t g = (pixel >> 8) & 0xff; uint32_t b = pixel & 0xff; uint32_t bw = (uint32_t) (r * 0.299 + g * 0.587 + b * 0.114); bitmap[col + row * stride / 4] = (a << 24) + (bw << 16) + (bw << 8) + (bw); } } } ``` 人眼吸收綠色比其他顏色敏感,所以當影像變成灰階時,僅僅將紅色、綠色、藍色加總取平均,這是不夠的,常見的方法是將 red * 77, green * 151 + blue * 28,這三個除數的總和為 256,可使除法變簡單 提出效能改善的方案: * 建立表格加速浮點數操作 (L1 cache?) * 減少位移數量 - [ ] 考慮以下寫法 ```cpp bwPixel = table[rgbPixel & 0x00ffffff] + rgbPixel & 0xff000000; ``` :::info 16 MB; 表格太大 ::: - [ ] 如果先計算針對「乘上 0.299」一類的運算,先行計算後建立表格呢? ```cpp bw = (uint32_t) mul_299[r] + (uint32_t) mul_587[g] + (uint32_t) mul_144[b]; bitmap[col + row * strike / 4] = (a << 24) + (bw << 16) + (bw << 8) + bw; ``` :::info 降到 32 KB 以內; cache friendly ::: ### 實作程式碼 原始程式碼: [RGBAtoBW](https://github.com/charles620016/embedded-summer2015/tree/master/RGBAtoBW) BMP (BitMaP) 檔是是很早以前微軟所開發並使用在 Windows 系統上的圖型格式,通常不壓縮,不像 JPG, GIF, PNG 會有破壞性或非破壞性的壓縮。雖然 BMP 缺點是檔案非常大,不過因為沒有壓縮,即使不借助 OpenCV、ImageMagick 或 .NET Framework 等等,也可以很容易地直接用 Standard C Library 作影像處理。 BMP 主要有四個部份組成: 1. Bitmap File Header:Magic Number (’BM’)、file size、Offset to image data 2. Bitmap Info Header:image width and height、the number of bits per pixel、Compression type 3. Color Table (Palette) 4. Image data ![](https://hackpad-attachments.s3.amazonaws.com/charles620016.hackpad.com_U4inYKbSPUp_p.431773_1440518623822_Selection_001.bmp) - [ ] 原始版本 以下是使用一張 1920x1080 的 BMP 圖片,所得知的檔案資訊: ``` ==== Header ==== Signature = 4D42 FileSize = 8294456 DataOffset = 54 ==== Info ====== Info size = 40 Width = 1920 Height = 1080 BitsPerPixel = 32 Compression = 0 ================ RGBA to BW is in progress.... Save the picture successfully! Execution time of rgbaToBw() : 0.034494 ``` ![](https://hackpad-attachments.s3.amazonaws.com/charles620016.hackpad.com_U4inYKbSPUp_p.431773_1440765925898_01_after.bmp) :::info 執行時間:0.034494 sec ::: - [ ] Version 1: [Lookup Table](https://en.wikipedia.org/wiki/Lookup_table) (LUT) RGB 分別都是 8 bit,可以建立三個大小為 256 bytes 的 table,這樣就不用在每次轉 bw 過程中進行浮點數運算。 * 原本: `bw = (uint32_t) (r * 0.299 + g * 0.587 + b * 0.114);` * 查表: `bw = (uint32_t) (table_R[r] + table_G[g] + table_B[b]);` :::info 執行時間: `0.028148 sec` ::: - [ ] Version 2 使用 pointer 的 offset 取代原本的繁雜的 bitwise operation。 ```C uint32_t *pixel = bmp->data; r = (BYTE *) pixel + 2; g = (BYTE *) pixel + 1; b = (BYTE *) pixel; ``` :::info 執行時間: `0.020379 sec` ::: - [ ] Version 3 將上述 Version 1 和 Version 2合併在一起 :::info 執行時間: `0.018061 sec` ::: - [ ] Version 4 + Version 5 使用 [ARM NEON 指令集](https://developer.arm.com/technologies/neon) 加速,執行環境是 Raspberry Pi 2 (Cortex-A7 x4),這是[實作程式碼](https://github.com/brchiu/RGBAtoBW)。 用 Raspberry Pi 2 測試: ```shell CC = gcc-4.8 CFLAGS = -O0 -Wall -ftree-vectorize -mcpu=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard ``` * Original * Execution time of rgbaToBw(): 0.353600 * Version 1: using RGB table * Execution time of rgbaToBw(): 0.319600 * Version 2: using pointer arithmetic * Execution time of rgbaToBw(): 0.251800 * Version 3: versoin1 + versoin2` * Execution time of rgbaToBw(): 0.226800 * Version 4: NEON * Execution time of rgbaToBw(): 0.016000 * Version 5: NEON (loop unrolling + PLD) * Execution time of rgbaToBw(): 0.013200 ### 什麼是 NEON? NEON technology is an advanced **SIMD** (Single Instruction, Multiple Data) architecture for the ARM Cortex-A series processors. * Registers are considered as **vectors **of **elements **of the same **data type** * Data types can be: signed/unsigned 8-bit, 16-bit, 32-bit, 64-bit, single precision [floating point](http://www.arm.com/products/processors/technologies/vector-floating-point.php) * Instructions perform the same** operation **in all **lanes** ![](https://hackpad-attachments.s3.amazonaws.com/charles620016.hackpad.com_U4inYKbSPUp_p.431773_1440962497875_download.png) Register : * 16 x 32-bit general purpose ARM registers (R0-R15). * 32 x 64-bit NEON registers (D0-D31) OR viewed as 16x128-bit registers (Q0-Q15). 簡言之,有了 NEON instruction set,就可以「同時」操作許多個 8, 16 或 32-bit 的資料,在訊號處理、影像處理、視訊解碼等有很高的應用價值。 首先先看 Version 4 : 將 RGB三色的 weight 丟進 r3 - r5。 `vdup.8` (Vector Duplicate),分別複製到大小為 8 bit 的 NEON register d0 - d2 ``` mov r3, #77 mov r4, #151 mov r5, #28 vdup.8 d0, r3 vdup.8 d1, r4 vdup.8 d2, r5 ``` `vld4.8` (Vector Load),載入 pixel 的資料到 4 個 8-bit 的 NEON register d4-d7,其中那個 4 是 interleave,因為我們有 ARGB,所以 gap = 4,從下面兩張圖就可以看出 vld 的用法。 再來就是計算 weighted average啦。Vector Multiply 和 Vector Multiply Accumulate ``` @ (alpha,R,G,B) = (d7,d6,d5,d4) vld4.8 {d4-d7}, [r0]! vmull.u8 q10, d6, d0 vmlal.u8 q10, d5, d1 vmlal.u8 q10, d4, d2 ``` 將值除以256就是我們要的灰階值了。 :::info `vrshrn` (Vector Shift Right by immediate value) ::: ``` vrshrn.u16 d4, q10, #8 ``` 最後將結果儲存。 :::info `vst` (Vector Store) ::: ``` vst4.8 {d4-d7}, [r3]! ``` :::info 執行時間: `0.016000 sec` ::: 從上面瀏覽過一遍用到的 NEON instruction set,就可以發現我們都是一次對多個 NEON register 作操作,下面討論分析有比較圖就可以看出效能差距。 再來看 Version 5 :::info 執行時間: 0.013200 sec ::: ### 討論與分析 ![](https://hackpad-attachments.s3.amazonaws.com/embedded2015.hackpad.com_7sVmkgm4Hre_p.352236_1440865933042_Selection_006.bmp) 以上各版本執行時間都是 50 次迴圈平均下來的結果,測試檔為 1920x1080 32bit bmp 圖片。可以明顯看到 v2 效能表現比起 v1 來說 還好上許多,可見原始程式中「多次」的 bitwise operation 結果所帶來損耗比起浮點數運算還更多一些。若我們再將浮點數運算改成查表的話,最後時間能進步到 0.018061 sec,幾乎是原來的一半。 另外使用 [perf 效能分析工具](http://wiki.csie.ncku.edu.tw/embedded/perf-tutorial) 來觀察原始版本和 version 3 中 instruction 和 cycle 數量。 ![](https://hackpad-attachments.s3.amazonaws.com/embedded2015.hackpad.com_7sVmkgm4Hre_p.352236_1440865025697_Selection_005.bmp) 可以看到 version 3 的 instruction 和 cycle 大約都只有原來的一半,這結果也正好反應在上面的執行時間上。 ![](https://hackpad-attachments.s3.amazonaws.com/charles620016.hackpad.com_U4inYKbSPUp_p.431773_1440968152862_Selection_001.bmp) 從這張表就可以很清楚了解使用 NEON 指令集加速後所得到的效能增長, Version 4 只花了==原本 `4.6%` 的時間== 就完成彩色轉灰階處理。 ## 案例探討: 位元反轉 [revserse bit 原理和案例分析](https://hackmd.io/@sysprog/ByzoiggIb)