[ 파워빌더.공통함수 - bit연산 ]

 

 

☞   

 

비트연산이 필요해서 찾아보니 파워빌더에 있더군요...

sru파일입니다...

 

 

$PBExportHeader$n_cst_numerical.sru
forward
global type n_cst_numerical from nonvisualobject
end type
end forward

global type n_cst_numerical from nonvisualobject autoinstantiate
end type

forward prototypes
public function string of_binary (long al_decimal)
public function long of_decimal (string as_binary)
public function boolean of_getbit (long al_decimal, unsignedinteger ai_bit)
public function long of_bitwiseand (long al_value1, long al_value2)
public function long of_bitwisenot (long al_value)
public function long of_bitwiseor (long al_value1, long al_value2)
public function long of_bitwisexor (long al_value1, long al_value2)
end prototypes

public function string of_binary (long al_decimal);integer li_remainder 
string ls_binary='' 

//Check   parameters 
If   IsNull(al_decimal)   or   al_decimal<   0   Then 
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 

//Build   binary   string 
ls_binary   =   string(li_remainder)   +   ls_binary 
Loop 
Return   ls_binary
end function

public function long of_decimal (string as_binary);integer   li_cnt 
long ll_len 
char lch_char[] 
long ll_decimal=0 

//Check   parameters 
If   IsNull(as_binary)   or   Len(as_binary)<=0   then 
long   ll_null 
SetNull(ll_null) 
Return   ll_null 
End   If 

//Get   the   length 
ll_len   =   Len(as_binary) 

//Move   string   into   arrach   of   characters 
lch_char   =   as_binary 

For   li_cnt   =   1   to   ll_len 
//Make   sure   only   0's   and   1's   are   present 
If   (Not   lch_char[li_cnt]='1')   AND   (Not   lch_char[li_cnt]='0')   Then 
Return   -1 
End   If 
//Build   the   decimal   equivalent 
ll_decimal   =   ll_decimal   +   (long(lch_char[li_cnt])   *   (2   ^   (ll_len   -   li_cnt))) 
Next 

Return   ll_decimal
end function

public function boolean of_getbit (long al_decimal, unsignedinteger ai_bit);Boolean   lb_null 

//Check   parameters 
If   IsNull(al_decimal)   or   IsNull(ai_bit)   then 
SetNull(lb_null) 
Return   lb_null 
End   If 

//Assumption   ai_bit   is   the   nth   bit   counting   right   to   left   with 
//the   leftmost   bit   being   bit   one. 
//al_decimal   is   a   binary   number   as   a   base   10   long. 
If   Int(Mod(al_decimal   /   (2   ^(ai_bit   -   1)),   2))   >   0   Then 
Return   True 
End   If 

Return   False
end 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] 

//   Check   for   nulls 
If   IsNull(al_Value1)   Or   IsNull(al_Value2)   Then 
SetNull(ll_Result) 
Return   ll_Result 
End   If 

//   Get   all   bits   for   both   values 
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 

//   And   them   together 
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

public function long of_bitwisenot (long al_value);Integer li_Cnt,   li_Count 
Long ll_Result 
string ls_Value,   ls_Result 

//   Check   for   nulls 
If   IsNull(al_Value)   Then 
SetNull(ll_Result) 
Return   ll_Result 
End   If 

//   return   a   binary   string   e.g.   100101 
ls_Value   =   of_binary(al_Value) 
li_Cnt   =   Len(ls_Value) 

//   change   0   to   1   and   1   to   0 
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   the   result   in   decimal   form   e.g.   57 
Return   of_decimal(ls_Result)
end function

public function long of_bitwiseor (long al_value1, long al_value2);Integer li_Cnt 
Long ll_Result 
Boolean lb_Value1[32],   lb_Value2[32] 

//   Check   for   nulls 
If   IsNull(al_Value1)   Or   IsNull(al_Value2)   Then 
SetNull(ll_Result) 
Return   ll_Result 
End   If 

//   Get   all   bits   for   both   values 
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 

//   Or   them   together 
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

public function long of_bitwisexor (long al_value1, long al_value2);Integer li_Cnt 
Long ll_Result 
Boolean lb_Value1[32],   lb_Value2[32] 

//   Check   for   nulls 
If   IsNull(al_Value1)   Or   IsNull(al_Value2)   Then 
SetNull(ll_Result) 
Return   ll_Result 
End   If 

//   Get   all   bits   for   both   values 
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 

//   Perfor   the   XOR 
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

on n_cst_numerical.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_cst_numerical.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

Posted by 농부지기
,