To determine the position of the k-th 1 bit in a 64-bit unsigned integer (from the least significant bit), you can implement a function that efficiently computes this using per-byte counts and a small precomputed table. While there isn't a built-in function in standard C++ to perform this exact operation, you can achieve very fast computation using bit manipulation techniques.
Here's how you can implement such a function:
-
Split the number into bytes: Break down your 64-bit number into eight 8-bit bytes.
-
Compute the popcount of each byte: Use a lookup table or efficient bit manipulation to count the number of 1 bits in each byte.
-
Compute cumulative counts: Determine the cumulative number of 1 bits up to each byte.
-
Identify the target byte: Find which byte contains the k-th 1 bit by comparing cumulative counts.
-
Find the position within the byte: Within the identified byte, find the position of the (k - cumulative count before this byte)-th 1 bit.
-
Combine the byte and bit positions: Calculate the overall bit position in the 64-bit number by combining the byte index and the bit position within the byte.
Here's the C++ code implementing this method:
#include <cstdint>
#include <climits>
const uint8_t pos_in_byte[256][8] = {
/* Precomputed positions of set bits for each possible byte value */
/* ... (initialize this table with positions of '1's in each byte value 0-255) ... */
};
uint64_t select64(uint64_t n, int k) {
if (n == 0) return UINT64_MAX; // Indicate that there are no set bits
// Split n into bytes
uint8_t bytes[8];
for (int i = 0; i < 8; ++i) {
bytes[i] = n & 0xFF;
n >>= 8;
}
// Compute popcounts and cumulative counts
int popcounts[8];
int cum_counts[8];
int total = 0;
for (int i = 0; i < 8; ++i) {
popcounts[i] = __builtin_popcount(bytes[i]);
total += popcounts[i];
cum_counts[i] = total;
}
// Check if k is within the total number of set bits
if (k >= total) {
return UINT64_MAX; // Indicate that k is out of range
}
// Find the byte where the k-th '1' bit lies
int i = 0;
while (cum_counts[i] <= k) {
i++;
}
int cum_before = (i == 0) ? 0 : cum_counts[i - 1];
int k_in_byte = k - cum_before;
uint8_t b = bytes[i];
// Find position of k_in_byte-th '1' in byte b
int pos_in_byte = pos_in_byte[b][k_in_byte];
// Compute the overall position
int position = i * 8 + pos_in_byte;
return position;
}
Note: You'll need to initialize the pos_in_byte table with the positions of set bits in each byte value from 0 to 255. This table will have a size of 256 x 8, which is feasible in terms of memory (approximately 2 KB).
This function effectively finds the position of the k-th set bit in a 64-bit integer with good performance, utilizing efficient built-in functions like __builtin_popcount and minimal looping.
There isn't a built-in function in standard C++ that directly finds the position of the k-th 1-bit in a 64-bit integer. However, you can implement an efficient function using bit manipulation and precomputed tables (as shown above) to achieve fast performance.