Comando util de git:
Deshacer último commit sin perder cambios (Git · 1 línea)
git reset --soft HEAD~1
SWAP sin variable temporal (macro)
#define SWAP(a, b) ((a)^=(b),(b)^=(a),(a)^=(b))
Tamaño de array estático (macro)
#define ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))
MIN y MAX seguros sin double-evaluation (macro GCC)
#define MAX(a,b) ({typeof(a) _a=(a); typeof(b) _b=(b); _a>_b?_a:_b;})
#define MIN(a,b) ({typeof(a) _a=(a); typeof(b) _b=(b); _a<_b?_a:_b;})
Es potencia de 2? (1 línea)
int is_pow2(unsigned int n) { return n && !(n & (n - 1)); }
Contar bits en 1 — truco de Kernighan
int popcount(unsigned int n) {
int c = 0;
for (; n; c++) n &= n - 1;
return c;
}
Siguiente potencia de 2
unsigned next_pow2(unsigned n) {
n--;
n|=n>>1; n|=n>>2; n|=n>>4; n|=n>>8; n|=n>>16;
return n+1;
}
container_of — el corazón del kernel Linux
#define container_of(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
Leer archivo entero a memoria
fseek(f, 0, SEEK_END);
long len = ftell(f);
rewind(f);
char *buf = malloc(len + 1);
fread(buf, 1, len, f);
buf[len] = '\0';
Fast inverse square root — Quake III (1999)
float Q_rsqrt(float n) {
long i; float x2 = n * 0.5f, y = n;
i = *(long *) &y;
i = 0x5f3759df - (i >> 1);
y = *(float *) &i;
y = y * (1.5f - (x2 * y * y));
return y;
}
Alinear a potencia de 2 (macro)
#define ALIGN(x, n) (((x)+(n)-1) & ~((n)-1))
// ALIGN(13, 8) → 16
// ALIGN(17, 8) → 24
Sign sin branch
int sign(int v) { return (v > 0) - (v < 0); }
LIKELY / UNLIKELY — kernel Linux
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
Static assert en compile time (pre-C11)
#define STATIC_ASSERT(cond) typedef char _sa_[(cond)?1:-1]
SAFE_FREE — liberar y nullear
#define SAFE_FREE(p) do { free(p); (p)=NULL; } while(0)
Hash djb2 — Dan Bernstein
unsigned long djb2(const char *s) {
unsigned long h = 5381; int c;
while ((c = *s++)) h = h * 33 ^ c;
return h;
}
GCD — Euclides (300 a.C.)
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
Endian swap 32 bits
uint32_t bswap32(uint32_t x) {
return ((x&0xFF000000)>>24)|((x&0x00FF0000)>>8)
|((x&0x0000FF00)<<8) |((x&0x000000FF)<<24);
}
Duff’s Device — Tom Duff, 1983
void copy(char *to, char *from, int n) {
int c = (n + 7) / 8;
switch (n % 8) {
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--c > 0);
}
}
Obtener el bit N de un entero
#define BIT_GET(x, n) (((x) >> (n)) & 1)
#define BIT_SET(x, n) ((x) |= (1 << (n)))
#define BIT_CLR(x, n) ((x) &= ~(1 << (n)))
#define BIT_FLIP(x, n) ((x) ^= (1 << (n)))
Clamp — limitar un valor entre min y max
#define CLAMP(x, lo, hi) \
((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x))
Abs sin branch (enteros)
int abs_branchless(int x) {
int mask = x >> 31;
return (x ^ mask) - mask;
}
Rotar bits a la izquierda (rotate left)
uint32_t rotl32(uint32_t x, int n) {
return (x << n) | (x >> (32 - n));
}
Detectar overflow en suma sin UB
int add_safe(int a, int b, int *result) {
if ((b > 0 && a > INT_MAX - b) ||
(b < 0 && a < INT_MIN - b)) return 0; // overflow
*result = a + b;
return 1;
}
Fibonacci en O(log n) — exponenciación de matriz 2x2
// multiplica matrices 2x2 de Fibonacci
void mat_mul(long long a[2][2], long long b[2][2]) {
long long c[2][2] = {
{a[0][0]*b[0][0]+a[0][1]*b[1][0],
a[0][0]*b[0][1]+a[0][1]*b[1][1]},
{a[1][0]*b[0][0]+a[1][1]*b[1][0],
a[1][0]*b[0][1]+a[1][1]*b[1][1]}
};
memcpy(a, c, sizeof(c));
}
long long fib(int n) {
long long r[2][2]={{1,0},{0,1}};
long long m[2][2]={{1,1},{1,0}};
for (; n; n>>=1) { if(n&1) mat_mul(r,m); mat_mul(m,m); }
return r[0][1];
}
Arena allocator — el allocator más simple posible
typedef struct { char *buf; size_t cap, cur; } Arena;
void *arena_alloc(Arena *a, size_t size) {
size = (size + 7) & ~7; // alinear a 8
if (a->cur + size > a->cap) return NULL;
void *p = a->buf + a->cur;
a->cur += size;
return p;
}
// liberar todo: a.cur = 0; ← una sola línea
Tiny ring buffer (buffer circular)
#define RING_SIZE 256 // debe ser potencia de 2
typedef struct { int buf[RING_SIZE]; int r, w; } Ring;
void ring_push(Ring *r, int v) { r->buf[r->w++ & (RING_SIZE-1)] = v; }
int ring_pop (Ring *r) { return r->buf[r->r++ & (RING_SIZE-1)]; }
int ring_empty(Ring *r) { return r->r == r->w; }
Generar número random sin stdlib (xorshift32)
uint32_t xorshift32(uint32_t *state) {
uint32_t x = *state;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return *state = x;
}
// uso: uint32_t s = 12345; xorshift32(&s);
strcmp en una línea
int streq(const char *a, const char *b) {
return !strcmp(a, b);
}
// o con macro:
#define STREQ(a,b) (strcmp((a),(b)) == 0)
Imprimir bits de cualquier entero
void print_bits(unsigned int x) {
for (int i = sizeof(x)*8-1; i >= 0; i--)
putchar('0' + ((x >> i) & 1));
putchar('\n');
}
Tabla de lookup en vez de if-else
// en vez de: if(op==0) add; else if(op==1) sub; ...
typedef int (*op_fn)(int, int);
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
op_fn ops[] = { add, sub, mul };
int result = ops[op](x, y); // dispatch en O(1)
Medición de tiempo de alta precisión
#include <time.h>
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
// ... código a medir ...
clock_gettime(CLOCK_MONOTONIC, &t1);
double ms = (t1.tv_sec - t0.tv_sec)*1e3
+ (t1.tv_nsec - t0.tv_nsec)/1e6;
printf("%.3f ms\n", ms);
Son 40+ snippets en total. Los más poderosos en mi opinión: container_of, el arena allocator, el xorshift32 y la tabla de dispatch con punteros a funciones. Ese último reemplaza cadenas enteras de if-else con una indexación O(1).