Skip to content

Instantly share code, notes, and snippets.

@cyberkm
cyberkm / gist:4477b20acb38418a37e12f3eb7c5c21c
Created December 15, 2018 09:19
Population count algorithm hack
x * 0x0101010101010101ull
is equal to (x + (x<<8) + (x<<16) + ... + (x<<56))
@cyberkm
cyberkm / gist:a7f881b126dc852c41ce0ff4b96149bb
Created December 15, 2018 09:14
Compute area of polygon
Source: https://www.johndcook.com/blog/2018/09/26/polygon-area/
Shoelace formula:
x1 y2 + x2 y3 + … xn y1 – y1 x2 – y2 x3 – … – yn x1
Python implementation
def area(x, y):
n = len(x)
s = 0.0