How to perform bit operation and binary and decimal conversion in PB (excerpt)

xiaoxiao2021-03-06  39

Original: PFC

This article intends to combine the PFC_N_CST_NUMERICAL objects provided in the PFC, explaining how to implement bit operations in the PB script and the binary and decimal mutual conversion.

(1) Writing a decimal conversion into binary functions:

Public function string of_binary (long al_decimal); Integer Li_RemaInder

String ls_binary = ''

IF isnull (al_decimal) or al_decimal <0

String ls_null

Setnull (ls_null)

Return ls_null

END IF

IF al_decimal = 0 THEN

Return '0'

END IF

Do Until Al_Decimal = 0

Li_RemaInder = mod (al_Decimal, 2)

Al_Decimal = al_decimal / 2

Ls_binary = string (li_remainder) ls_binary

Loop

Return ls_binary

END FUNCTION

(2) Writing binary conversion into a 10-entry function:

Public function long of_decimal (string as_binary); integer li_cnt

Long Ll_len

Char lch_char []

Long ll_decimal = 0

IF isnull (as_binary) or LEN (as_binary) <= 0 THEN

Long LL_NULL

SetNULL (LL_NULL)

Return LL_NULL

END IF

LL_LEN = LEN (as_binary)

LCH_CHAR = as_binary

For Li_CNT = 1 to LL_LEN

IF (not lch_char [li_cnt] = '1') and (not lch_char [li_cnt] = '0') THEN

Return -1

END IF

LL_DECIMAL = LL_DECIMAL (long (lch_char [li_cnt]) * (2 ^ (LL_LEN - Li_CNT)))))

NEXT

Return Ll_Decimal

END FUNCTION

(3) Write a public function to acquire BIT bit data in place:

Public Function Boolean of_Getbit (long al_decimal, unsignedinteger ai_bit); Boolean LB_NULL

IF isnull (al_Decimal) or isnull (ai_bit) THEN

SetNull (lb_null)

Return LB_NULL

END IF

IF Int (MOD (AL_DECIMAL / (2 ^ (ai_bit - 1)), 2)> 0 THEN

Return True

END IF

Return False

END FUNCTION

(4) Write the AND and operation function:

Public Function Long of_bitWiseand (long al_value1, long al_value2); integer li_cnt

Long Ll_Result

Boolean LB_Value1 [32], LB_VALUE2 [32]

IF isnull (al_value1) or isnull (al_value2) Thensetnull (LL_RESULT)

Return LL_RESULT

END IF

For Li_CNT = 1 to 32

LB_Value1 [li_cnt] = of_getbit (al_value1, li_cnt)

LB_Value2 [li_cnt] = of_getbit (al_value2, li_cnt)

NEXT

For Li_CNT = 1 to 32

IF lb_value1 [li_cnt] and lb_value2 [li_cnt] Then

LL_RESULT = LL_RESULT (2 ^ (li_cnt - 1)))

END IF

NEXT

Return LL_RESULT

END FUNCTION

(5) Write or operate functions:

Public function long of_bitwiseor (long al_value1, long al_value2); integer li_cnt

Long Ll_Result

Boolean LB_Value1 [32], LB_VALUE2 [32]

IF isnull (al_value1) or isnull (al_value2) THEN

SetNull (LL_RESULT)

Return LL_RESULT

END IF

For Li_CNT = 1 to 32

LB_Value1 [li_cnt] = of_getbit (al_value1, li_cnt)

LB_Value2 [li_cnt] = of_getbit (al_value2, li_cnt)

NEXT

For Li_CNT = 1 to 32

IF lb_value1 [li_cnt] or lb_value2 [li_cnt] THEN

LL_RESULT = LL_RESULT (2 ^ (li_cnt - 1)))

END IF

NEXT

Return LL_RESULT

END FUNCTION

(6) Write a XOR train:

Public function long of_bitwisexor (long al_value1, long al_value2); integer li_cnt

Long Ll_Result

Boolean LB_Value1 [32], LB_VALUE2 [32]

IF isnull (al_value1) or isnull (al_value2) THEN

SetNull (LL_RESULT)

Return LL_RESULT

END IF

For Li_CNT = 1 to 32

LB_Value1 [li_cnt] = of_getbit (al_value1, li_cnt)

LB_Value2 [li_cnt] = of_getbit (al_value2, li_cnt)

NEXT

For Li_CNT = 1 to 32

IF (lb_value1 [li_cnt] and not lb_value2 [li_cnt]) or &

(Not lb_value1 [li_cnt] and lb_value2 [li_cnt]) THEN

LL_RESULT = LL_RESULT (2 ^ (li_cnt - 1)))

END IF

NEXT

Return LL_RESULT

END FUNCTION

(7) Write the Not No Operation function:

Public Function long of_bitwisenot (long al_value); Integer li_cnt, li_countlong ll_result

String Ls_Value, Ls_RESULT

IF isnull (Al_Value) THEN

SetNull (LL_RESULT)

Return LL_RESULT

END IF

LS_Value = of_binary (al_value)

LI_CNT = LEN (Ls_Value)

For li_count = 1 to li_cnt

IF MID (Ls_Value, li_count, 1) = '0' THEN

LS_RESULT = LS_RESULT '1'

Else

LS_RESULT = LS_RESULT '0'

END IF

End for

RETURN OF_DECIMAL (Ls_RESULT)

END FUNCTION

(8) Call the above function:

// convert the decimal number 10 into binary 1010

Ls_binary = of_binary (10)

// convert the binary 1010 into a decimal number 10

LL_DECIMAL = of_DECIMAL ("1010")

// Execute and operate (55 && 44) = 36

LL_RET = of_bitwiseand (55, 44) // Return 36

/ / Execute Not No Operation (! 55) = 8

LL_RET = of_bitwisenot (55) // Return 8

// Execute or operate (55 || 44) = 63

LL_RET = of_bitwiseor (55, 44) // Return 63

// Execute XOR different or operation (55 xor 44) = 27

LL_RET = of_bitwisexor (55, 44) // Returns 27

This end is over this article.

转载请注明原文地址:https://www.9cbs.com/read-73308.html

New Post(0)