Last active
May 30, 2025 08:50
-
-
Save tcantenot/323a181a7ca191b9ff9fb03dfc9f00ba to your computer and use it in GitHub Desktop.
Exponentially spaced set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on: https://x.com/ptrschmdtnlsn/status/1840765584398840233 | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <string.h> | |
constexpr uint32_t N = 7; | |
constexpr uint32_t M = 1u << N; | |
constexpr uint32_t MaxNumStates = 6; | |
void fmt_bits(char str[N+1], int32_t x) | |
{ | |
uint32_t u; | |
memcpy(&u, &x, sizeof(uint32_t)); | |
for(int i = 0; i < N; ++i) | |
{ | |
str[i] = u & (1u << i) ? '1' : '0'; | |
} | |
str[N] = '\0'; | |
} | |
int main() { | |
constexpr uint32_t MaxDuration = 1u << (MaxNumStates - 2); | |
uint32_t durations[M]; | |
for(int32_t i = 0; i < M; ++i) | |
{ | |
const int32_t m = i & -i; | |
const int32_t j = 2 * (i & -i); | |
char str_bits_i[N+1]; | |
fmt_bits(str_bits_i, i); | |
char str_bits_minus_i[N+1]; | |
fmt_bits(str_bits_minus_i, -i); | |
char str_bits_m[N+1]; | |
fmt_bits(str_bits_m, m); | |
printf("%2d: %s | %3d: %s | m: %s | j: %2d\n", i, str_bits_i, -i, str_bits_minus_i, str_bits_m, j); | |
durations[i] = j < MaxDuration ? j : MaxDuration; | |
} | |
for(int32_t i = 0; i < M * 2; ++i) | |
{ | |
printf("Frame %3d: 0 ", i); | |
for(int32_t j = 0, n = i <= M-1 ? i : M-1; j <= n; ++j) | |
{ | |
if(i < (j + durations[j])) | |
{ | |
printf("%3d ", j); | |
} | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: