Luabit的位運作簡介

2021-07-25 18:32:18 字數 4530 閱讀 9537

lua提供了bit庫,可以對變數資料進行位運算,在某些應有場景,我們得確需要在lua中對資料進行位移,或是進行「與,或,非」,進製轉換等操作。

例如有這麼一種較典型的情況,我們用乙個32位的整數表示rgb顏色,32位整數,被分為4個部分,每個部分8bit, 8bit可表示的10進製數的範圍是0~255。

我們現在有一組rgb的顏色值: alpha通道=8, r=4, g=2, b=1

123

4567

891011

1213

14

下面是,二進位制表示:

alpha red green blue

00001000 00000100 00000010 00000001

第四組 第三組 第二組 第一組

下面是,十六進製制表示:

alpha red green blue

0x08 0x04 0x02 0x01

第四組 第三組 第二組 第一組

下面是,十進位制表示:

134480385

我們現在要作的是把整數"134480385"的「4組」「8bit」資料擷取出來。 思路和c語言二進位制運算是一樣,首先是「4組「每」8bit「的資料,都移動到」第一組的「位置,每一組不需要移位,然後是,第次移位後和0x000000ff這個數進行"and"運算,清空前24位資料,保留最後的8位bit資料。

需要分解的原始資料:

none

1

2

local

num=

bit.

tobit

(0x08040201

)print

(num

)

第一組blue資料的取得:

123

4

8        4        2        1

alpha red green blue

00001000 00000100 00000010 00000001

第四組 第三組 第二組 第一組

第一組的資料,不需要移位操作,直接將資料與"0x000000ff"進行」與「運算。

123

4567

8

8        4        2        1

alpha red green blue

00001000 00000100 00000010 00000001

and(與運算)

00000000 00000000 00000000 11111111

結果(1)

00000000 00000000 00000000 00000001

第四組 第三組 第二組 第一組

none

1

23

local

t_num

=num

local

blue

=bit

.band

(t_num

,0x000000ff

)print("

blue:"

..blue

)

第二組green資料的取得: 第二組green資料,需要先右移8位,然後,才能與"0x000000ff"進行」與「運算。

123

4567

8910

8        4        2        1

alpha red green blue

00001000 00000100 00000010 00000001

rshift(右移8位,左邊空位補零)

00000000 00001000 00000100 00000010

and(與運算)

00000000 00000000 00000000 11111111

結果(2)

00000000 00000000 00000000 00000010

第四組 第三組 第二組 第一組

none

1

23

local

t_num

=bit

.rshift

(num,8

)local

green

=bit

.band

(t_num

,0x000000ff

)print("

green:"

..green

)

第三組red資料的取得: 第三組red資料,需要先右移16位,然後,才能與"0x000000ff"進行」與「運算。

123

4567

8910

8        4        2        1

alpha red green blue

00001000 00000100 00000010 00000001

rshift(右移16位,左邊空位補零)

00000000 00000000 00001000 00000100

and(與運算)

00000000 00000000 00000000 11111111

結果(4)

00000000 00000000 00000000 00000100

第四組 第三組 第二組 第一組

第四組alpha資料的取得: 第四組alpha資料,需要先右移24位,然後,才能與"0x000000ff"進行」與「運算。

123

4567

8910

8        4        2        1

alpha red green blue

00001000 00000100 00000010 00000001

rshift(右移24位,左邊空位補零)

00000000 00000000 00000000 00001000

and(與運算)

00000000 00000000 00000000 11111111

結果(8)

00000000 00000000 00000000 00001000

第四組 第三組 第二組 第一組

最後,取得四組資料的值: alpha通道=8, r=4, g=2, b=1

none

123

4567

891011

1213

1415

1617

1819

2021

2223

local bit = require "bit"

local num = bit.tobit(0x08040201)

print(num)

--第一組blue資料的取得:

local t_num = num

local blue = bit.band(t_num, 0x000000ff)

print("blue:"..blue)

--第二組green資料的取得:

local t_num = bit.rshift(num, 8)

local green = bit.band(t_num, 0x000000ff)

print("green:"..green)

--第三組red資料的取得:

local t_num = bit.rshift(num, 16)

local red = bit.band(t_num, 0x000000ff)

print("red:"..red)

--第四組alpha資料的取得:

local t_num = bit.rshift(num, 24)

local alpha = bit.band(t_num, 0x000000ff)

print("alpha:"..alpha)

下面是luabit常用函式:

none

123

4567

891011

1213

14

彙編標誌位簡介

標誌 flag 用於反映指令執行結果或控制指令執行形式,是組合語言程式設計中必須特別注意的乙個方面。許多指令執行之後將影響有關的標誌位,同時不少指令的執行要利用某些標誌。因此我們在學習使用組合語言的過程中必須了解標誌位。進製標誌cf主要用來反映運算是否產生進製或借位。如果運算結果的最高位產生了乙個進...

php的運作機理

參考資料 php在編譯器zend中的運作 1.scanning lexing 將php 轉換為語言片段 tokens 2.parsing,將tokens轉換成簡單而有意義的表示式 3.compilation,將表示式編譯成opocdes 4.execution,順次執行opcodes,每次一條,從而...

64位平台簡介

目前最流行的兩種64bit微處理器架構 ia 64 intel 64 ia 64 由intel和hewlett packard公司聯合開發,被使用在 itanium和 itanium 2微處理器之上。intel 64 又稱em64t amd64 ia 32e x86 64 aa 64 x64 ham...