Bitwise Data Operations

<< Click to Display Table of Contents >>

Navigation:  Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > Function library > The BIT Library >

Bitwise Data Operations

Bitwise Data Operations

mbul_mail bit.BitToData

mbul_mail bit.BitFromData

mbul_mail bit.BitNot

mbul_mail bit.BitAnd

mbul_mail bit.BitOr

mbul_mail bit.BitXor

mbul_mail bit.BitLshift

mbul_mail bit.BitRshift

mbul_mail bit.BitArshift

bit.BitToData

Sets on/off the specified bit of a number, and returns the modified number.

Function arguments are:

mbul   1 – number

mbul   2 – bit value (true or false)

mbul   3 – bit number (starting with 0)

Example

local d=1

d=bit.BitToData(d,true,1); --d=3

server.Message("d=",d);

bit.BitFromData

Returns a value of the specified bit of a number.

Function arguments are:

mbul   1 – number

mbul   2 – bit number (starting with 0)

Example

local bit=false;

local d=1

bit=bit.BitFromData(d,0); --bit=true

server.Message("bit=",bit);

bit.BitNot

Performs the operation "bitwise NOT"; returns a bitwise-inverted value of the argument.

Example

--a = 01101101

b=bit.BitNot(a);

--b=10010010

bit.BitAnd

Returns a result of the operation "bitwise AND" with arguments. Number of arguments may be greater than 2.

Example

--a = 01101101

--b = 11001001

c=bit.BitAnd(a,b);

--c = 01001001

bit.BitOr

Returns a result of the operation "bitwise OR" with arguments. Number of arguments may be greater than 2.

Example

--a = 00101101

--b = 10001001

c=bit.BitOr(a,b);

--c = 10101101

bit.BitXor

Returns a result of the operation "bitwise XOR" with arguments. Number of arguments may be greater than 2.

Example

--a = 00101101

--b = 10001001

c=bit.BitXor(a,b);

--c = 10100100

bit.BitLshift

Returns a result of the operation "shift to the left by specified number of bits" (cut bits are not transferred).

Arguments:

mbul   1 – number to be shifted

mbul   2 – shift as number of bits

Example

--a = 00101101

b=bit.BitLshift (a,3);

--b = 01101000

bit.BitRshift

Returns a result of the operation "shift to the right by specified number of bits" (cut bits are not transferred).

Arguments:

mbul   1 – number to be shifted

mbul   2 – shift as number of bits

Example

--a = 00101101

b=bit.BitRshift(a,3);

--b = 00000101

bit.BitArshift

Returns a result of the operation "arithmetic shift to the right by specified number of bits".

Arguments:

mbul   1 – number to be shifted

mbul   2 – shift as number of bits

Example

--a = 11111010

b=bit.BitArshift(a,1);

--b = 11111101