ALPHA: breaking changes expected. What that means
NanoC

Bitset sieve

A sieve of Eratosthenes over a bit array using function-like defines.

NanoC
+ <stdio.h>
+ <stdint.h>
# Sieve of Eratosthenes on a bitset: preprocessor-style macros and fixed-width types.
$ BITS = 128
$ GET(a, i) = (((a)[(i) / 32] >> ((i) % 32)) & 1u)
$ SET(a, i) = ((a)[(i) / 32] |= 1u << ((i) % 32))
f main()
  composite: uint32_t[BITS / 32] = {0}
  for i := 2; i * i < BITS; i++
    if !GET(composite, i)
      for j: int = i * i; j < BITS; j += i
        SET(composite, j)
  count := 0
  for i := 2; i < BITS; i++
    if !GET(composite, i)
      printf("%d ", i)
      count++
  printf("\n%d primes below %d\n", count, BITS)
  < 0
C
#include <stdio.h>
#include <stdint.h>
#define BITS 128
#define GET(a, i) (((a)[(i) / 32] >> ((i) % 32)) & 1u)
#define SET(a, i) ((a)[(i) / 32] |= 1u << ((i) % 32))

int main(void) {
    uint32_t composite[BITS / 32] = {0};
    for (int i = 2; i * i < BITS; i++) {
        if (!GET(composite, i)) {
            for (int j = i * i; j < BITS; j += i) {
                SET(composite, j);
            }
        }
    }
    int count = 0;
    for (int i = 2; i < BITS; i++) {
        if (!GET(composite, i)) {
            printf("%d ", i);
            count++;
        }
    }
    printf("\n%d primes below %d\n", count, BITS);
    return 0;
}

Output

stdout
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 
31 primes below 128

What it shows

defines, bit operations

Edit in the playgroundSource on GitHub