Skip to main content

15.4.14 Bit operation

15.4.14.1 BitAnd

Explanation

BitAnd is used to generate logical conjunction (and) for byte type data. See table below:

image761

Definition

Return value, data type: byte, the result returned by performing logical conjunction of two byte-type data.

BitAnd (BitData1, BitData2);

BitData1, data type: byte, the byte data 1 to be processed.

BitData2, data type: byte, the byte data 2 to be processed.

Example

Example 1

VAR byte data1 = 34;
VAR byte data2 = 38;
VAR byte byte3 = BitAnd(data1, data2); //34

Define the byte-type variable data1 and data2. assign them with the value of 34 and 38, respectively; perform logical conjunction on data1 and data2, the returned value of 34 is assigned to byte3.

15.4.14.2 BitCheck

Explanation

It is used to check whether a bit in a byte-type data is 1. If so, returns true, otherwise, false.

Definition

Return value, data type: bool, true indicates the bit is assigned to 1, false indicates the bit is assigned to 0.

BitCheck (BitData, BitPos);

BitData, data type: byte, the byte data to be processed.

BitPos, data type: int, the position of byte to be operated, ranging from 1 to 8.

Example

Example 1

VAR byte data1 = 130;
VAR bool b1 = BitCheck(data1, 8) //true;

Definite byte data1 and assign it with 130, check if the 8th bit of data1 is 1 and return true if so.

15.4.14.3 BitClear

Explanation

To set a certain bit of byte- or int-type data to 0. The bit starts from 1.

Definition

BitClear(BitData | IntData, BitPos);

BitData, data type: byte, the byte data to be processed.

IntData, data type: byte, the byte data to be processed.

BitPos, data type: int, the position of the bit to be operated, ranging from 1 to 8 for byte data and 1 to 32 for int data.

Example

Example 1

VAR byte data1 = 255;
BitClear data1 1 //254;
BitClear data1 2 //252;

Define byte-type variable data1 and assign it with 255, Perform BitClear on data1, set the first bit to 0, and 254 is returned, set the second bit to 0, and 252 is returned.

15.4.14.4 BitLSh

Explanation

It is used to perform logical left shift on byte-type data.

Definition

Return value, data type: byte, the byte data obtained by performing the left-shift operation.

BitLSh (BitData, ShiftSteps);

BitData, data type: byte, the byte data to be processed.

ShiftSteps, data type: int, the bits selected for the left shift, ranging from 1 to 8.

Example

Example 1

VAR int left_shift = 3;
VAR byte data1 = 38;
VAR byte data2;
data2 = BiLSh(data1, left_shift) //48;

Define byte-type variable data1, and assign it with 38, perform 3 bits left shift on data1, and 48 is returned.

15.4.14.5 BitNeg

Explanation

It is used to perform logical negation on byte-type data.

Definition

Return value, data type: byte, the byte data obtained by performing the logical negation.

BitNeg (BitData);

BitData, data type: byte, the byte data to be processed.

Example

Example 1

VAR byte data1 = 38;
VAR byte data2;
data2 = BitNeg(data1) //217;

Define byte-type variable data1, and assign it with 38, perform logical negation on data1, and 217 is returned.

15.4.14.6 BitOr

Explanation

It is used to perform logical disjunction (or) on byte-type data.

Definition

Return value, data type: byte, the byte data obtained by performing the logical disjunction.

BitOr (BitData1, BitData2);

BitData1, data type: byte, the byte data 1 to be processed.

BitData2, data type: byte, the byte data 2 to be processed.

Example

Example 1

VAR byte data1 = 39;
VAR byte data2 = 162;
VAR byte data3;
data3 = BitOr(data1, data2); //167

Define the byte-type variable data1 and data2, assign them with the value of 39 and 162, respectively; perform logical conjunction on data1 and data2, and 167 is returned.

15.4.14.7 BitRSh

Explanation

It is used to perform the logical right shift on byte-type data.

Definition

Return value, data type: byte, the byte data obtained by performing the right-shift operation.

BitLSh (BitData, ShiftSteps);

BitData, data type: byte, the byte data to be processed.

ShiftSteps, data type: int, the bits selected for the right shift, ranging from 1 to 8.

Example

Example 1

VAR int right_shift = 3;
VAR byte data1 = 38;
VAR byte data2;
data2 = BiRSh(data1, right_shift); //4

Define byte-type variable data1, and assign it with 38, perform 3 bits right shift on data1, and 4 is returned.

15.4.14.8 BitSet

Explanation

It is used to set a certain bit of byte- or int-type data to 1. The bit starts from 1.

Definition

BitSet (BitData | IntData, BitPos);

BitData, data type: byte, the byte data to be processed.

IntData, data type: byte, the byte data to be processed.

BitPos, data type: int, the position of the bit to be operated, ranging from 1 to 8 for byte data and 1 to 32 for int data.

Example

Example 1

VAR byte data1 = 0;
BitSet (data1, 1); //1
BitSet (data1, 2); //3

Define byte-type variable data1 and assign it with 255, Perform BitSet on data1, set the first bit to 1, and 1 is returned, set the second bit to 1, and 3 is returned.

15.4.14.9 BitXOr

Explanation

It is used to perform logical exclusive or on byte-type data.

Definition

Return value, data type: byte, the byte data obtained by performing the logical disjunction.

BitXOr (BitData1, BitData2);

BitData1, data type: byte, the byte data 1 to be processed.

BitData2, data type: byte, the byte data 2 to be processed.

Example

Example 1

VAR byte data1 = 39;
VAR byte data2 = 162;
VAR byte data3;
data3 = BitOr(data1, data2) ;//133

Define the byte-type variable data1 and data2, assign them with the value of 39 and 162, respectively; perform logical exclusive or on data1 and data2, and 133 is returned