Skip to content

Instantly share code, notes, and snippets.

View nikita-edel's full-sized avatar

Nikita Edel nikita-edel

  • Potsdam, Brandenburg, Germany
View GitHub Profile
@nikita-edel
nikita-edel / bit_width.h
Last active June 30, 2026 20:45
Calculate bit width of any arithmetic type in C (c23)
#ifndef NSL_BIT_WIDTH_H
#define NSL_BIT_WIDTH_H
// most efficient calcualtion of bit width of any size of integer (in c23)
// O(log2(n)) where n is the number of bits of the type,
// the below bound is clangs limit meaning for any integer its evaluated in 23 steps,
// (constant time) (computed dynamically, walking down and collecting each entry)
// NOTE: sadly, there is nothing you can do on bitfields
// NOTE: _Decimal not included, long double gets evaluated as 128bit, but well you cant really
@nikita-edel
nikita-edel / get_caps.lua
Last active July 7, 2026 13:21
Check for CAPS state in Neovim on Windows, Linux or MacOS, repo nikita-edel/capsdetect.nvim
local OS = vim.loop.os_uname().sysname
local get_caps_fn = nil
if OS == "Linux" then
get_caps_fn = require("linux")
elseif OS == "Darwin" then
get_caps_fn = require("macos")
elseif vim.fn.has("win32") == 1 then
get_caps_fn = require("windows")
else
error("capsdetect: unsupported OS", 0)
@nikita-edel
nikita-edel / meta_swap.h
Created June 26, 2026 21:58
Swap any 2 variables of same type in C (std=c23)
#ifndef META_SWAP_H
#define META_SWAP_H
// if any of them is const, than there is a warning (discarding const)
// if 2 different types, than dereferencing an incomplete type fails to compile
// same thing for void*
#define META_SWAP(p1, p2) \
((void)_Generic((p1), \
void*: (*(struct meta_swap_incompatible*)0), \
typeof((p2)): meta_sw((p1), (p2), (char[sizeof(*(p1))]){}, \