<< Click to Display Table of Contents >> Navigation: 3. Script Language > BIT. - Work with Bits > BIT. - Work with Bits |
MiniRobotLanguage (MRL)
BIT. Command
Get, Set Clear or Toggle Bits and make Logic Operations with Bits
Intention
Performing Single-Bit Operations.
Single-bit operations enable querying and manipulating individual bits.
This can be instrumental in analyzing outcomes of other operations or even in creating custom encryption algorithms.
Let’s first understand the parameters needed.
For bit operations, parameters can be specified in two forms:
1. By Value - For example, specifying the number '16' as text would require two bytes. The first byte represents '1' and the second byte represents '6'.
2. As Binary - Alternatively, specifying the number using ASCII code '16' takes up only one byte.
1. Specify Parameter as VALUE
' To specify the Parameter "as VALUE" we can just write it as a number, using the VAN.-Command.
' 9 is 1001 in Binary
VAN.$$P01=9
' We want extract Bit "1" which is "1" in Result
VAN.$$P02=1
BIT.Get|v$$P01|$$P02|$$RES
' We change the Result to a readable Value using the ASC.-Command. This will only work with 8-bit results
' The result will be "1" as CHR-value (Binary). To make it readable we use the ASC.-Command
ASC.$$RES|$$ERG
MBX.Result is $$ERG
This will give the result: "1", because the first bit in "1001" (dez. 9) is a "1".
In both cases, we did not specify P4 (Size-Parameter) so the default size of 8-bit was used.
2. Specify Parameter as BINARY
' To specify the Parameter "as Binary" we use the CHR. Command if the Parameter is just 8 bit (default).
' 9 is 1001 in Binary
CHR.9|$$P01
' We want extract Bit "1" which is "1" in Result
VAN.$$P02=1
BIT.Get|b$$P01|$$P02|$$RES
' We change the Result to a readable Value using the ASC.-Command. This will only work with 8-bit results
' The result will be "1" as CHR-value (Binary). To make it readable we use the ASC.-Command
ASC.$$RES|$$ERG
MBX.Result is $$ERG
3. Specify multi-Byte Parameters "as Binary" or "as Value"
' Use the "ToBin" or the "ToVal" Option to convert the Parameters
VAN.$$P01=9
' $$P02 will contain the result
BIT.ToBin|$$P01|$$P02|2
' $$P02 will contain the result
BIT.ToVal|$$P01|$$P02|2
4. Specify multi-Byte Parameters using the "Mask" Option.
IMPORTANT:
5. Using the 64-bit Options / Why are the 64-bit operations unusable?
While there is a 64-Bit Mask Option implemented, and also other 64-bit Operations, we found during testing that the
SPR is currently unable to show exact results in many cases.
The problem happens, if a 64-bit number will evaluate to a number in Exponential writing during variable resolution.
For example "6.144567 E+18".
In such case the result number will be enough precise for all "normal" calculations, but the real "Bits" inside can differ from the expected result.
As you do not know in which cases this rounding will happen, most 64-bit operations will be useless at this time.
The problem appears when the SPR looks what is inside of a numeric variable and replaces it with the number that is inside.
Currently this is an decimal number and decimal numbers are not 100% precise at all times. And this is when the problem appears.
As an alternative, we have evaluated to change the variables of the SPR to NOT output decimal numbers but Hexadecimal numbers like
&HAAAAAAAAAAAAAAAAFFFFFFFFFFFFFFFF, in such case the output would be unreadable for most people.
So we have not done this, instead 64-bit operations with bits are there but you should currently not use them because the results may be wrong.
For details on how to prefix Hexadecimal, Octal or Binary numbers, see
3.5 Hexadezimal, Decimal and Binary and more
Syntax
BIT.P1[|P2][|P3][|P4]
Parameter Explanation
P1 - Subcommand
P2 - Input Parameter
P3 -- Px Parameter depending on the Subcommand
Example
'*****************************************************
' EXAMPLE 1:
'*****************************************************
VAN.$$LEA=1
VAN.$$P01=67
VAN.$$P02=2
BIT.Show|v$$P01|$$ERA|$$LEA
BIT.Toggle|$$P01|$$P02|$$P03|$$LEA
BIT.Show|v$$P03|$$ERC|$$LEA
MBX.$$P01 Toggle $$P02 is $$P03$crlf$Bin: $$ERA Toogle $$P02 -> $$ERC
BIT.Toggle|$$P03|$$P02|$$P04|$$LEA
BIT.Show|v$$P04|$$ERD|$$LEA
MBX.$$P03 Toggle $$P02 is $$P04$crlf$Bin: $$ERC Toogle $$P02 -> $$ERD
ENR.
Remarks
Please always use the proper prefix for non-decimal numbers.
More details on this, see here:
3.5 Hexadezimal, Decimal and Binary and more
Limitations:
Why are the 64-bit operations unusable and give wrong results?
While there is a 64-Bit Mask (8-Byte Option) implemented, and also other 64-bit Operations, we found during testing that the
SPR is currently unable to show exact results in many cases.
The problem happens, if a 64-bit number will evaluate to a number in Exponential writing during variable resolution.
For example "6.144567 E+18".
In such case the result number will be enough precise for all "normal" calculations, but the real "Bits" inside can differ from the expected result.
As you do not know in which cases this rounding will happen, most 64-bit operations will be useless at this time.
The problem appears when the SPR looks what is inside of a numeric variable and replaces it with the number that is inside.
Currently this is an decimal number and decimal numbers are not 100% precise at all times. And this is when the problem appears.
As an alternative, we have evaluated to change the variables of the SPR to NOT output decimal numbers but Hexadecimal numbers like
&HAAAAAAAAAAAAAAAAFFFFFFFFFFFFFFFF, in such case the output would be unreadable for most people.
So we have not done this, instead 64-bit operations with bits are there but
you should currently not use them because the results may be wrong.
' Maximum 60 bit Precision
VAN.$$P01=9223372036854775807
VAN.$$LEA=8
BIT.Show|$$P01|$$ERG|$$LEA
LEN.$$ERG|$$LEN
NVV.$$LEN=64
GTO.ero
EIF.
NVS.$$ERG=0111111111111111111111111111111111111111111111111111111111111000
GTO.ero
EIF.
' The last 4 Bits (right side) are not precide
VAN.$$P01=-9223372036854775807
VAN.$$LEA=8
BIT.Show|$$P01|$$ERG|$$LEA
LEN.$$ERG|$$LEN
NVV.$$LEN=64
GTO.ero
EIF.
NVS.$$ERG=1000000000000000000000000000000000000000000000000000000000001000
GTO.ero
EIF.
See also:
•3.5 Hexadezimal, Decimal and Binary and more
•VTH. - Variable to Hexadecimal
•CAL. - mathematical CALculation
•VAR. - Variable Set Value/Clear
•! Smart Package Robot 's BIT. - Operations
•BIT.16to8 - Split 1 Word into 2 Bytes
•BIT.16to32 - concatenate 2 Words to 32 Bit DWord
•BIT.32to16 - Split 1 DWord into 2 Words
•BIT.8to16 - concatenate 2 Bytes to 16 Bit Word
•BIT.AND - Use logical AND with Bits
•BIT.LShift - Shift Bits to Left, insert 0
•BIT.NOT - Use logical NOT with Bits
•BIT.OR - Use logical OR with Bits
•BIT.RotateLeft - Rotate Bits Left
•BIT.RotateRight - Rotate Bits Right
•BIT.RShift - Shift Bits to Right, insert 0
•BIT.Show - Show Bits in Binary-Numbers
•BIT.Store - Set Bit to 1 or 0
•BIT.ToBin - change Parameter to Binary-Format
•BIT.ToVal - change Parameter to Value-Format
•BIT.XOR - Use logical XOR with Bits
•BIT.Get - check if Bit is set