16.3.5. Values in binary and hexadecimal notation and logical operations
When operating various binary formats, the so-called "magical numbers", that is constants or bit masks, are used quite frequently. Such magical number can be converted into decimal numbers, but such conversion increases risk of errors and is less obvious. To simplify operation with such magical number, there are global context functions: NumberFromHexString() and NumberFromBinaryString().
Example:
OlderBitMask = NumberFromBinaryString("0b10000000"); ParameterSet = NumberFromHexString("0x79E");
The system also enables logical operations with binary data buffer. The following operations are supported:
- Logical "And" (the WriteBitwiseAnd() method)
- Logical "Or" (the WriteBitwiseOr() method)
- Logical "exclusive Or" (the WriteBitwiseExclusiveOr() method)
- Logical "and not" (a reverse operation to logical "And", the WriteBitwiseAndNot() method)
- Inversion (the Invert() method)
Using this methods you can perform various logical operations with data, including verification of specific bits setting. These methods operate with binary data buffers. So, the operation bitness is limited by the buffer size. For the example of how to use these methods, see the next section.
However, logical operations are increasingly frequently required to operate numbers with limited bitness. The platform provides a set of methods that simplifies manipulation of specific bits in numbers with limited bitness:
- Functions for bits and masks: CheckBit(), CheckByBitMask(), and SetBit().
- Bitwise logical operations: BitwiseAnd(), BitwiseOr(), BitwiseNot(), BitwiseAndNot(), and BitwiseXor().
- Shifting operations: BitwiseShiftRight() and BitwiseShiftLeft().
When operating the above-mentioned functions, remember:
- All operands are considered 32-bit unsigned integers. An attempt to other value as a parameter is considered invalid value of the parameter with exception generation.
- The bit number is an integer from 0 (the youngest bit) to 31 (the oldest bit).
- When shifting, the free digits are filled with 0.