
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses




Task

Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate.

All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer.

If any operation is not available in your language, note it.




 
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
 
 