How to resolve the algorithm Bitwise operations step by step in the PureBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bitwise operations step by step in the PureBasic programming language
Table of Contents
Problem Statement
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bitwise operations step by step in the PureBasic programming language
Source code in the purebasic programming language
Procedure Bitwise(a, b)
Debug a & b ; And
Debug a | b ;Or
Debug a ! b ; XOr
Debug ~a ;Not
Debug a << b ; shift left
Debug a >> b ; arithmetic shift right
; Logical shift right and rotates are not available
; You can of use inline ASM to achieve this:
Define Temp
; logical shift right
!mov edx, dword [p.v_a]
!mov ecx, dword [p.v_b]
!shr edx, cl
!mov dword [p.v_Temp], edx
Debug Temp
; rotate left
!mov edx, dword [p.v_a]
!mov ecx, dword [p.v_b]
!rol edx, cl
!mov dword [p.v_Temp], edx
Debug Temp
; rotate right
!mov edx, dword [p.v_a]
!mov ecx, dword [p.v_b]
!ror edx, cl
!mov dword [p.v_Temp], edx
Debug Temp
EndProcedure
You may also check:How to resolve the algorithm Negative base numbers step by step in the C programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the Ada programming language
You may also check:How to resolve the algorithm AVL tree step by step in the Lua programming language
You may also check:How to resolve the algorithm Bitcoin/address validation step by step in the Factor programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Clipper programming language