Skip to content

Instantly share code, notes, and snippets.

@ilansmith
ilansmith / count_bits.c
Last active January 6, 2019 13:49
Efficient count the number of bits in a 32bit bitmask
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define DO_PRINT 0
#define TEST_BITS(_n_, _do_print_) do { \
unsigned calculated = number_of_set_bits(_n_); \
unsigned actual = number_of_set_bits_naive(_n_); \
if (_do_print_) \
#include <stdio.h>
#if defined(DEBUG)
#define PRINT(_fmt_, ...) printf(_fmt_, __VA_ARGS__);
#else
#define PRINT(_fmt_, ...)
#endif
/* works in the range: [0x1, 0x7FFF FFFF FFFF FFFF] */
static size_t upper_power_of_two(size_t v)
@ilansmith
ilansmith / iface.c
Created December 29, 2022 14:20
List network devices
#include <net/if.h>
#include <stdio.h>
int main(void)
{
struct if_nameindex *if_nidxs, *intf;
if (!(if_nidxs = if_nameindex()))
goto exit;