A comprehensive library providing utilities for bit manipulation, binary conversions, and BCD (Binary Coded Decimal) operations in AMX NetLinx.
The BinaryUtils library provides low-level binary operations commonly needed when interfacing with hardware protocols, implementing data encoding schemes, or performing bitwise operations. It includes functions for bit rotation, bit extraction, binary-to-BCD conversion, and binary representation formatting.
- Bit Rotation: Rotate 32-bit values left or right
- Bit Extraction: Extract individual bits from values
- Binary Representation: Convert bytes to bit arrays or binary strings
- BCD Conversion: Bidirectional conversion between binary integers and BCD format
Include the library in your NetLinx project:
#include 'NAVFoundation.BinaryUtils.axi'Rotates bits of a 32-bit value to the left by the specified count. Bits that are rotated off the left end appear at the right end.
Parameters:
value(long): The value to rotatecount(long): Number of positions to rotate left
Returns: (long) The rotated value
Example:
stack_var long original
stack_var long rotated
original = $01 // Binary: 00000000 00000000 00000000 00000001
rotated = NAVBinaryRotateLeft(original, 4) // Binary: 00000000 00000000 00000000 00010000
// rotated = $10Note: Count should typically be between 1 and 31 for meaningful results.
Alias for NAVBinaryRotateLeft. Rotates bits of a 32-bit value to the left.
Parameters:
value(long): The value to rotatecount(long): Number of positions to rotate left
Returns: (long) The rotated value
See: NAVBinaryRotateLeft
Rotates bits of a 32-bit value to the right by the specified count. Bits that are rotated off the right end appear at the left end.
Parameters:
value(long): The value to rotatecount(long): Number of positions to rotate right
Returns: (long) The rotated value
Example:
stack_var long original
stack_var long rotated
original = $10 // Binary: 00000000 00000000 00000000 00010000
rotated = NAVBinaryRotateRight(original, 4) // Binary: 00000000 00000000 00000000 00000001
// rotated = $01Note: Count should typically be between 1 and 31 for meaningful results.
Alias for NAVBinaryRotateRight. Rotates bits of a 32-bit value to the right.
Parameters:
value(long): The value to rotatecount(long): Number of positions to rotate right
Returns: (long) The rotated value
See: NAVBinaryRotateRight
Extracts a single bit from a 32-bit value at the specified position.
Parameters:
value(long): The value to extract a bit frombit(long): The bit position to extract (0-31)
Returns: (long) 1 if the specified bit is set, 0 otherwise
Example:
stack_var long value
stack_var long bitValue
value = $05 // Binary: 00000000 00000000 00000000 00000101
bitValue = NAVBinaryGetBit(value, 0) // Returns 1 (rightmost bit)
bitValue = NAVBinaryGetBit(value, 1) // Returns 0 (second bit from right)
bitValue = NAVBinaryGetBit(value, 2) // Returns 1 (third bit from right)Note: Bit position 0 is the least significant (rightmost) bit.
Counts the number of leading zero bits in a 32-bit integer. This is useful for determining the position of the most significant bit or calculating the number of bits required to represent a value.
Parameters:
value(long): The value to analyze
Returns: (integer) Number of leading zeros (0-32)
Example:
stack_var long value
stack_var integer leadingZeros
value = $0000000F // Binary: 00000000 00000000 00000000 00001111
leadingZeros = NAVBinaryCountLeadingZeros(value) // Returns 28
value = $80000000 // Binary: 10000000 00000000 00000000 00000000
leadingZeros = NAVBinaryCountLeadingZeros(value) // Returns 0
value = $00000000 // Binary: 00000000 00000000 00000000 00000000
leadingZeros = NAVBinaryCountLeadingZeros(value) // Returns 32
value = $00FF0000 // Binary: 00000000 11111111 00000000 00000000
leadingZeros = NAVBinaryCountLeadingZeros(value) // Returns 8Use Cases:
- Determining bit width requirements for variable-length encoding
- Finding the position of the most significant bit
- Calculating logarithm base 2 (floor)
- Implementing priority encoders
- Optimizing compression algorithms
Converts a byte to an array of individual bit values. Each bit is represented as a numeric value (0 or 1) in the returned array.
Parameters:
value(char): The byte value to convert
Returns: (char[8]) Array of 8 bit values (0 or 1)
Example:
stack_var char value
stack_var char result[8]
value = $A5 // Binary: 10100101
result = NAVByteToBitArray(value)
// result = {1, 0, 1, 0, 0, 1, 0, 1}Use Cases:
- Analyzing individual bits of protocol bytes
- Implementing custom bit-level protocols
- Debugging binary data structures
Converts a byte to its binary representation as an 8-character string.
Parameters:
value(char): The byte value to convert
Returns: (char[8]) Binary representation as an 8-character string
Example:
stack_var char value
stack_var char result[8]
value = $A5 // Binary: 10100101
result = NAVByteToBinaryString(value)
// result = '10100101'Use Cases:
- Debugging and logging binary data
- Display binary values in user interfaces
- Protocol analysis and troubleshooting
BCD (Binary Coded Decimal) is a binary encoding where each decimal digit (0-9) is represented by 4 bits (one nibble). This encoding is commonly used in hardware devices like RTCs (Real-Time Clocks), numeric displays, and various embedded systems.
Converts a binary integer to BCD (Binary Coded Decimal) format using the double-dabble algorithm.
Parameters:
value(integer): The binary integer to convert (0-9999)
Returns: (long) BCD representation of the value
Example:
stack_var integer decimal
stack_var long bcd
decimal = 42
bcd = NAVBinaryToBcd(decimal) // Returns $42 (BCD format)
decimal = 1234
bcd = NAVBinaryToBcd(decimal) // Returns $1234 (BCD format)Use Cases:
- Sending decimal values to hardware that expects BCD encoding
- Implementing BCD-based protocols
- Interfacing with seven-segment displays
Note: This implements the double-dabble algorithm for BCD conversion.
Converts a BCD (Binary Coded Decimal) byte to its binary integer value. Each nibble (4 bits) of the input represents a decimal digit (0-9).
Parameters:
value(char): The BCD-encoded byte to convert (0x00-0x99)
Returns: (integer) Binary integer representation (0-99)
Example:
stack_var char bcdValue
stack_var integer decimal
bcdValue = $42 // BCD representation of 42
decimal = NAVBcdToBinary(bcdValue) // Returns 42
bcdValue = $99 // BCD representation of 99
decimal = NAVBcdToBinary(bcdValue) // Returns 99Use Cases:
- Reading BCD-encoded data from hardware (RTCs, displays, etc.)
- Parsing BCD protocol responses
- Converting BCD values for display or calculation
Note: Input values should only use digits 0-9 in each nibble (0x00-0x99).
Many hardware devices use BCD encoding for numeric values:
// Reading time from an RTC chip that returns BCD values
stack_var char bcdHour
stack_var integer hour
bcdHour = $13 // 1:00 PM in BCD
hour = NAVBcdToBinary(bcdHour) // Converts to 13
// Sending time to a device expecting BCD format
stack_var integer currentHour
stack_var char bcdOutput
currentHour = 15 // 3:00 PM
bcdOutput = NAVBinaryToBcd(currentHour) // Converts to $15 for transmission// Extracting flags from a status byte
stack_var char statusByte
stack_var long powerOn
stack_var long errorFlag
statusByte = $85 // Binary: 10000101
powerOn = NAVBinaryGetBit(statusByte, 0) // Returns 1
errorFlag = NAVBinaryGetBit(statusByte, 7) // Returns 1
// Rotating data for encryption or encoding
stack_var long data
stack_var long encoded
data = $12345678
encoded = NAVBinaryRotateLeft(data, 8) // Rotates by one byte// Visualizing binary data for debugging
stack_var char rxByte
stack_var char binaryStr[8]
rxByte = $A5
binaryStr = NAVByteToBinaryString(rxByte)
NAVErrorLog(NAV_LOG_LEVEL_DEBUG, "'Received byte: ',binaryStr")
// Output: "Received byte: 10100101"If you're updating from an older version of NAVFoundation.BinaryUtils, the following function names have changed:
| Old Function Name | New Function Name | Notes |
|---|---|---|
NAVCharToDecimalBinaryString() |
NAVByteToBitArray() |
Returns numeric bit array instead of string |
NAVCharToAsciiBinaryString() |
NAVByteToBinaryString() |
Returns ASCII string (behavior unchanged) |
NAVDecimalToBinary() |
NAVBinaryToBcd() |
Corrected naming to reflect BCD conversion |
New function:
NAVBcdToBinary()- Reverse BCD conversion for reading hardware values
Migration steps:
- Search your codebase for the old function names
- Replace with the new names according to the table above
- For BCD operations, verify you're using the correct direction:
- Use
NAVBinaryToBcd()when sending to hardware - Use
NAVBcdToBinary()when reading from hardware
- Use
NAVFoundation.Core.h.axi
This library includes comprehensive test coverage with 119 test cases covering all functions and edge cases. Tests are located in:
__tests__/include/binary-utils/
To run the test suite:
genlinx build .\__tests__\src\binary-utils.axsMIT License - Copyright (c) 2010-2026 Norgate AV