This commit is contained in:
Miloslav Ciz 2025-07-05 08:29:04 +02:00
parent 0d26cac3da
commit d60f3c941f
15 changed files with 1936 additions and 1926 deletions

View file

@ -36,7 +36,7 @@ Unless noted otherwise we suppose [C](c.md) syntax and semantics and integer [da
**clear (to 0) rightmost 1 bit of x**: `x & (x - 1)`
**conditionally add (subtract etc.) x and y based on condition c (c is 0 or 1)**: `x + ((0 - c) & y)`, this avoids branches AND ALSO multiplication by c, of course you may replace + by another operators.
**conditionally add (subtract, or etc.) x and y based on condition c (c is 0 or 1)**: `x + (y & (0 - c))` or `x + (y & ~(c - 1))`, this avoids branches AND ALSO multiplication by c, of course you may replace `+` by other operators.
**count 0 bits of x**: Count 1 bits and subtract from data type width.
@ -84,6 +84,8 @@ Unless noted otherwise we suppose [C](c.md) syntax and semantics and integer [da
**divide x by 5 (unsigned at least 16 bit, x < 256)**: `((x + 1) * 51) >> 8`, analogous to divide by 3.
**expand lowest bit (two's complement)** (i.e. 0 to all 0s and 1 to all 1s): `~(x - 1)` or `0 - x`
**get Nth bit of x**: `(x >> N) & 0x01`
**is x a power of 2?**: `x && ((x & (x - 1)) == 0)`
@ -171,4 +173,4 @@ TODO: the ugly hacks that use conversion to/from float?
- [De Morgan's laws](de_morgans_laws.md)
- [fast inverse square root](fast_inverse_sqrt.md)
- [optimization](optimization.md)
- [logic gate](logic_gate.md)
- [logic gate](logic_gate.md)