@@ -30,12 +30,14 @@ Include the library in your NetLinx project:
3030Rotates 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.
3131
3232** Parameters:**
33+
3334- ` value ` (long): The value to rotate
3435- ` count ` (long): Number of positions to rotate left
3536
3637** Returns:** (long) The rotated value
3738
3839** Example:**
40+
3941``` netlinx
4042stack_var long original
4143stack_var long rotated
@@ -54,6 +56,7 @@ rotated = NAVBinaryRotateLeft(original, 4) // Binary: 00000000 00000000 0000000
5456Alias for ` NAVBinaryRotateLeft ` . Rotates bits of a 32-bit value to the left.
5557
5658** Parameters:**
59+
5760- ` value ` (long): The value to rotate
5861- ` count ` (long): Number of positions to rotate left
5962
@@ -68,12 +71,14 @@ Alias for `NAVBinaryRotateLeft`. Rotates bits of a 32-bit value to the left.
6871Rotates 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.
6972
7073** Parameters:**
74+
7175- ` value ` (long): The value to rotate
7276- ` count ` (long): Number of positions to rotate right
7377
7478** Returns:** (long) The rotated value
7579
7680** Example:**
81+
7782``` netlinx
7883stack_var long original
7984stack_var long rotated
@@ -92,6 +97,7 @@ rotated = NAVBinaryRotateRight(original, 4) // Binary: 00000000 00000000 000000
9297Alias for ` NAVBinaryRotateRight ` . Rotates bits of a 32-bit value to the right.
9398
9499** Parameters:**
100+
95101- ` value ` (long): The value to rotate
96102- ` count ` (long): Number of positions to rotate right
97103
@@ -108,12 +114,14 @@ Alias for `NAVBinaryRotateRight`. Rotates bits of a 32-bit value to the right.
108114Extracts a single bit from a 32-bit value at the specified position.
109115
110116** Parameters:**
117+
111118- ` value ` (long): The value to extract a bit from
112119- ` bit ` (long): The bit position to extract (0-31)
113120
114121** Returns:** (long) 1 if the specified bit is set, 0 otherwise
115122
116123** Example:**
124+
117125``` netlinx
118126stack_var long value
119127stack_var long bitValue
@@ -128,18 +136,59 @@ bitValue = NAVBinaryGetBit(value, 2) // Returns 1 (third bit from right)
128136
129137---
130138
139+ #### ` NAVBinaryCountLeadingZeros(long value) `
140+
141+ 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.
142+
143+ ** Parameters:**
144+
145+ - ` value ` (long): The value to analyze
146+
147+ ** Returns:** (integer) Number of leading zeros (0-32)
148+
149+ ** Example:**
150+
151+ ``` netlinx
152+ stack_var long value
153+ stack_var integer leadingZeros
154+
155+ value = $0000000F // Binary: 00000000 00000000 00000000 00001111
156+ leadingZeros = NAVBinaryCountLeadingZeros(value) // Returns 28
157+
158+ value = $80000000 // Binary: 10000000 00000000 00000000 00000000
159+ leadingZeros = NAVBinaryCountLeadingZeros(value) // Returns 0
160+
161+ value = $00000000 // Binary: 00000000 00000000 00000000 00000000
162+ leadingZeros = NAVBinaryCountLeadingZeros(value) // Returns 32
163+
164+ value = $00FF0000 // Binary: 00000000 11111111 00000000 00000000
165+ leadingZeros = NAVBinaryCountLeadingZeros(value) // Returns 8
166+ ```
167+
168+ ** Use Cases:**
169+
170+ - Determining bit width requirements for variable-length encoding
171+ - Finding the position of the most significant bit
172+ - Calculating logarithm base 2 (floor)
173+ - Implementing priority encoders
174+ - Optimizing compression algorithms
175+
176+ ---
177+
131178### Binary Representation Functions
132179
133180#### ` NAVByteToBitArray(char value) `
134181
135182Converts a byte to an array of individual bit values. Each bit is represented as a numeric value (0 or 1) in the returned array.
136183
137184** Parameters:**
185+
138186- ` value ` (char): The byte value to convert
139187
140188** Returns:** (char[ 8] ) Array of 8 bit values (0 or 1)
141189
142190** Example:**
191+
143192``` netlinx
144193stack_var char value
145194stack_var char result[8]
@@ -150,6 +199,7 @@ result = NAVByteToBitArray(value)
150199```
151200
152201** Use Cases:**
202+
153203- Analyzing individual bits of protocol bytes
154204- Implementing custom bit-level protocols
155205- Debugging binary data structures
@@ -161,11 +211,13 @@ result = NAVByteToBitArray(value)
161211Converts a byte to its binary representation as an 8-character string.
162212
163213** Parameters:**
214+
164215- ` value ` (char): The byte value to convert
165216
166217** Returns:** (char[ 8] ) Binary representation as an 8-character string
167218
168219** Example:**
220+
169221``` netlinx
170222stack_var char value
171223stack_var char result[8]
@@ -176,6 +228,7 @@ result = NAVByteToBinaryString(value)
176228```
177229
178230** Use Cases:**
231+
179232- Debugging and logging binary data
180233- Display binary values in user interfaces
181234- Protocol analysis and troubleshooting
@@ -191,11 +244,13 @@ BCD (Binary Coded Decimal) is a binary encoding where each decimal digit (0-9) i
191244Converts a binary integer to BCD (Binary Coded Decimal) format using the double-dabble algorithm.
192245
193246** Parameters:**
247+
194248- ` value ` (integer): The binary integer to convert (0-9999)
195249
196250** Returns:** (long) BCD representation of the value
197251
198252** Example:**
253+
199254``` netlinx
200255stack_var integer decimal
201256stack_var long bcd
@@ -208,6 +263,7 @@ bcd = NAVBinaryToBcd(decimal) // Returns $1234 (BCD format)
208263```
209264
210265** Use Cases:**
266+
211267- Sending decimal values to hardware that expects BCD encoding
212268- Implementing BCD-based protocols
213269- Interfacing with seven-segment displays
@@ -221,11 +277,13 @@ bcd = NAVBinaryToBcd(decimal) // Returns $1234 (BCD format)
221277Converts a BCD (Binary Coded Decimal) byte to its binary integer value. Each nibble (4 bits) of the input represents a decimal digit (0-9).
222278
223279** Parameters:**
280+
224281- ` value ` (char): The BCD-encoded byte to convert (0x00-0x99)
225282
226283** Returns:** (integer) Binary integer representation (0-99)
227284
228285** Example:**
286+
229287``` netlinx
230288stack_var char bcdValue
231289stack_var integer decimal
@@ -238,6 +296,7 @@ decimal = NAVBcdToBinary(bcdValue) // Returns 99
238296```
239297
240298** Use Cases:**
299+
241300- Reading BCD-encoded data from hardware (RTCs, displays, etc.)
242301- Parsing BCD protocol responses
243302- Converting BCD values for display or calculation
@@ -305,21 +364,23 @@ NAVErrorLog(NAV_LOG_LEVEL_DEBUG, "'Received byte: ',binaryStr")
305364
306365If you're updating from an older version of NAVFoundation.BinaryUtils, the following function names have changed:
307366
308- | Old Function Name | New Function Name | Notes |
309- | ------------------| -------------------| -------|
310- | ` NAVCharToDecimalBinaryString() ` | ` NAVByteToBitArray() ` | Returns numeric bit array instead of string |
311- | ` NAVCharToAsciiBinaryString() ` | ` NAVByteToBinaryString() ` | Returns ASCII string (behavior unchanged) |
312- | ` NAVDecimalToBinary() ` | ` NAVBinaryToBcd() ` | Corrected naming to reflect BCD conversion |
367+ | Old Function Name | New Function Name | Notes |
368+ | -------------------------------- | ------------------------- | ------------------------------------------- |
369+ | ` NAVCharToDecimalBinaryString() ` | ` NAVByteToBitArray() ` | Returns numeric bit array instead of string |
370+ | ` NAVCharToAsciiBinaryString() ` | ` NAVByteToBinaryString() ` | Returns ASCII string (behavior unchanged) |
371+ | ` NAVDecimalToBinary() ` | ` NAVBinaryToBcd() ` | Corrected naming to reflect BCD conversion |
313372
314373** New function:**
374+
315375- ` NAVBcdToBinary() ` - Reverse BCD conversion for reading hardware values
316376
317377** Migration steps:**
378+
3183791 . Search your codebase for the old function names
3193802 . Replace with the new names according to the table above
3203813 . For BCD operations, verify you're using the correct direction:
321- - Use ` NAVBinaryToBcd() ` when sending to hardware
322- - Use ` NAVBcdToBinary() ` when reading from hardware
382+ - Use ` NAVBinaryToBcd() ` when sending to hardware
383+ - Use ` NAVBcdToBinary() ` when reading from hardware
323384
324385## Dependencies
325386
0 commit comments