
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.




enum SHL, SAR, SHR, ROL, ROR
function bitop(atom a, integer b, integer op)
atom res
    #ilASM{
        [32]
            mov eax,[a]
            call :%pLoadMint
            mov ecx,[b]
            mov edx,[op]
            cmp dl,SHL
            jne @f
                shl eax,cl
                jmp :storeres
          @@:
            cmp dl,SAR
            jne @f
                sar eax,cl
                jmp :storeres
          @@:
            cmp dl,SHR
            jne @f
                shr eax,cl
                jmp :storeres
          @@:
            cmp dl,ROL
            jne @f
                rol eax,cl
                jmp :storeres
          @@:
            cmp dl,ROR
            jne @f
                ror eax,cl
                jmp :storeres
          @@:
            int3
          ::storeres
            lea edi,[res]
            call :%pStoreMint
        [64]
            mov rax,[a]
            mov rcx,[b]
            mov edx,[op]
            cmp dl,SHL
            jne @f
                shl rax,cl
                jmp :storeres
          @@:
            cmp dl,SAR
            jne @f
                sar rax,cl
                jmp :storeres
          @@:
            cmp dl,SHR
            jne @f
                shr rax,cl
                jmp :storeres
          @@:
            cmp dl,ROL
            jne @f
                rol rax,cl
                jmp :storeres
          @@:
            cmp dl,ROR
            jne @f
                ror eax,cl
                jmp :storeres
          @@:
            int3
          ::storeres
            lea rdi,[res]
            call :%pStoreMint
          }
    return res
end function
 
procedure bitwise(atom a, atom b)
    printf(1,"and_bits(%b,%b) = %032b\n",{a,b,and_bits(a,b)})
    printf(1," or_bits(%b,%b) = %032b\n",{a,b, or_bits(a,b)})
    printf(1,"xor_bits(%b,%b) = %032b\n",{a,b,xor_bits(a,b)})
    printf(1,"not_bits(%b)     = %032b\n",{a,not_bits(a)})
    printf(1,"     shl(%b,%b) = %032b\n",{a,b,bitop(a,b,SHL)})
    printf(1,"     sar(%b,%b) = %032b\n",{a,b,bitop(a,b,SAR)})
    printf(1,"     shr(%b,%b) = %032b\n",{a,b,bitop(a,b,SHR)})
    printf(1,"     rol(%b,%b) = %032b\n",{a,b,bitop(a,b,ROL)})
    printf(1,"     ror(%b,%b) = %032b\n",{a,b,bitop(a,b,ROR)})
end procedure
 
bitwise(0x800000FE,7)
