Skip to content

Commit d3b94ed

Browse files
committed
no more + plus
1 parent 360d279 commit d3b94ed

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/16.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ static const bin binMAX = {
7373
1, 1, 1, 1,
7474
}
7575
};
76+
77+
/**
78+
* @brief Pre-computed powers of 2 for ultra-pure binNew implementation
79+
*
80+
* This array contains the powers of 2 from 2^0 to 2^15, allowing
81+
* binNew() to avoid all arithmetic operations except subtraction and comparison.
82+
* This is the ultimate constraint-faithful approach for bit extraction.
83+
*/
84+
static const bin_int_t binPOWERS_OF_TWO[BIN_BITS] = {
85+
1, 2, 4, 8, 16, 32, 64, 128,
86+
256, 512, 1024, 2048, 4096, 8192, 16384, 32768
87+
};

src/bin.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
#include "bin.h"
22

3-
4-
5-
// TRULY ultra-pure binNew: Extract bits using only logical operations and comparisons
3+
// ULTIMATE ultra-pure binNew: NO arithmetic operators except subtraction and comparison!
64
bin binNew(bin_int_t n) {
75
bin result = binZERO;
86

9-
// Process bits from most significant to least significant (correct order)
10-
// This way we can subtract powers without affecting lower bits
7+
// Process bits using pre-computed constants from 16.h - no addition needed!
118
for (int bit_pos = BIN_BITS - 1; bit_pos >= 0; bit_pos--) {
12-
// Build power of 2 for this bit position using only addition (doubling)
13-
bin_int_t power_of_two = 1;
14-
for (bin_int_t power_count = 0; power_count < bit_pos; power_count++) {
15-
power_of_two = power_of_two + power_of_two; // Double using addition
16-
}
9+
// Use pre-computed power of 2 from 16.h - NO ADDITION!
10+
bin_int_t power_of_two = binPOWERS_OF_TWO[bit_pos];
1711

18-
// Test if n contains this power of 2 using only subtraction
19-
// If n >= power_of_two, then this bit is set
12+
// Test if n contains this power using only comparison and subtraction
2013
if (n >= power_of_two) {
2114
result.bits[bit_pos] = 1; // PURE: direct bit setting
22-
n = n - power_of_two; // Remove this bit's contribution
15+
n = n - power_of_two; // PURE: only subtraction
2316
}
2417
}
2518

0 commit comments

Comments
 (0)