This commit is contained in:
Miloslav Ciz 2023-11-03 17:46:23 +01:00
parent 77b18c33d8
commit ca6a66554f
25 changed files with 100 additions and 21 deletions

View file

@ -1,6 +1,6 @@
# Bit Hack
Bit [hacks](hacking.md) or bit tricks are simple clever formulas for performing useful operations with [binary](binary.md) numbers. Some operations, such as checking if a number is power of two or reversing bits in a number, can be done very efficiently with these hacks, without using loops, [branching](branchless.md) and other undesirably slow operations, potentially increasing speed and/or decreasing size and/or memory usage of code -- this can help us [optimize](optimization.md). Many of these can be found on the [web](www.md) and there are also books such as *Hacker's Delight* which document such hacks.
Bit [hacks](hacking.md) (also bit tricks, bit [magic](magic.md), bit twiddling etc.) are simple clever formulas for performing useful operations with [binary](binary.md) numbers. Some operations, such as checking if a number is power of two or reversing bits in a number, can be done very efficiently with these hacks, without using loops, [branching](branchless.md) and other undesirably slow operations, potentially increasing speed and/or decreasing size and/or memory usage of code -- this can help us [optimize](optimization.md). Many of these can be found on the [web](www.md) and there are also books such as *Hacker's Delight* which document such hacks.
## Basics
@ -16,6 +16,8 @@ Basic bit manipulation techniques are common and part of general knowledge so th
{ Work in progress. I'm taking these from various sources such as the *Hacker's Delight* book or web and rewriting them a bit, always testing. Some of these are my own. ~drummyfish }
TODO: stuff from this gophersite: gopher://bitreich.org/0/thaumaturgy/bithacks
Unless noted otherwise we suppose [C](c.md) syntax and semantics and integer [data types](data_type.md). Keep in mind all potential dangers, for example it may sometimes be better to write an idiomatic code and let compiler do the optimization that's best for given platform, also of course readability will worsen etc. Nevertheless as a hacker you should know about these tricks, it's useful for low level code etc.
**2^N**: `1 << N`