Skip to content

Instantly share code, notes, and snippets.

@xv
xv / calc_check_digit.py
Last active June 15, 2025 01:45
Python function to calculate the check digit of a UPC-12 barcode.
def calc_check_digit(digits):
digits = [int(i) for i in str(digits)][:-1]
odd_pos_list = digits[0::2] # 1st, 3rd, 5th, etc
even_pos_list = digits[1::2] # 2nd, 4th, 6th, etc
n = (sum(odd_pos_list) * 3) + sum(even_pos_list)
rmndr = n % 10
return (10 - rmndr) if rmndr > 0 else 0
@xv
xv / Win32Macros.cs
Last active January 7, 2024 01:23
Common Win32 API macros.
public static byte LOBYTE(ushort a) =>
(byte)(a & 0xFF);
public static byte HIBYTE(ushort a) =>
(byte)((a >> 8) & 0xFF);
public static ushort HIWORD(uint a) =>
(ushort)((a >> 16) & 0xFFFF);
public static ushort LOWORD(uint a) =>
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define CLAMP(N, LO, HI) (MIN(MAX((LO), (N)), (HI)))