RosettaCodeData/Task/Bitwise-operations/Harbour/bitwise-operations.harbour

43 lines
1.2 KiB
Plaintext

PROCEDURE Main(...)
local n1 := 42, n2 := 2
local aPar := hb_AParams()
local nRot
if ! Empty( aPar )
n1 := Val( aPar[1] )
hb_Adel( aPar, 1, .T. )
if ! Empty( aPar )
n2 := Val( aPar[1] )
endif
endif
clear screen
? "Bitwise operations with two integers"
? "n1 =", hb_ntos(n1)
? "n2 =", hb_ntos(n2)
? "------------------------------------"
? "AND -->", hb_BitAnd( n1, n2 )
? "OR -->", hb_BitOr( n1, n2 )
? "XOR -->", hb_BitXor( n1, n2 )
? "NOT -->", hb_BitNot( n1 )
? "LSHIFT -->", hb_bitShift( n1, n2 )
? "RSHIFT -->", hb_bitShift( n1, -n2 )
? "RarSHIFT -->", hb_bitShift( n1, -n2 )
/* left/right rotation is not implemented, but we can use inline C-code to do it */
/* rotate n1 to the left by n2 bits */
nRot := hb_Inline( n1, n2 ) {
HB_UINT x = hb_parni( 1 ), s = hb_parni( 2 );
hb_retni( (x << s) | (x >> (-s & 31)) );
} // (x << s) | (x >> (32 - s));
? "Rotate left -->", nRot
/* rotate n1 to the right by n2 bits */
nRot := HB_INLINE( n1, n2 ){
HB_UINT x = hb_parni( 1 ), s = hb_parni( 2 );
hb_retni( (x >> s) | (x << (32 - s)) );
}
? "Rotate right -->", nRot
return