Skip to content
๐Ÿ“ Inclusive Range Counting

๐Ÿ“ Inclusive Range Counting

The difference between two numbers is exclusive โ€” it doesnโ€™t count the starting point.
To include both endpoints, you must add 1 if we took the negative difference or subtract 1 if we took the positive difference

๐Ÿงฉ Examples

Case 1: From 31 down to 24

[31, 30, 29, 28, 27, 26, 25, 24]
  • Difference: 31 - 24 = 7
  • โœ… Total count: 31 - 24 + 1 = 8

Case 2: Starting at 1, want 8 numbers

[1, 2, 3, 4, 5, 6, 7, 8]
  • Final number: 1 + 8 - 1 = 8
  • โœ… Range: [1:8] โ†’ total count = 8

๐Ÿง  Why This Matters in HDL and Indexing

โœ… Verilog slicing:

in[31:24]  // โœ… 8 bits: 31 - 24 + 1

โœ… Looping:

for (i = 0; i < 8; i++)  // โœ… 8 iterations

โœ… Array indexing:

mem[0:7]  // โœ… 8 elements
Last updated on