{"id":19913582,"url":"https://github.com/ianbrayoni/bithacks","last_synced_at":"2025-10-30T04:36:41.447Z","repository":{"id":93799306,"uuid":"258448656","full_name":"ianbrayoni/bithacks","owner":"ianbrayoni","description":"Bit Twiddling Hacks By Sean Eron Anderson: code snippets in Python","archived":false,"fork":false,"pushed_at":"2023-10-20T05:38:35.000Z","size":38,"stargazers_count":17,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-12T21:40:59.904Z","etag":null,"topics":["bitmanipulation","python"],"latest_commit_sha":null,"homepage":"https://graphics.stanford.edu/~seander/bithacks.html","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ianbrayoni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-24T08:11:04.000Z","updated_at":"2024-11-12T09:47:03.000Z","dependencies_parsed_at":"2023-04-14T23:00:19.935Z","dependency_job_id":null,"html_url":"https://github.com/ianbrayoni/bithacks","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianbrayoni%2Fbithacks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianbrayoni%2Fbithacks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianbrayoni%2Fbithacks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianbrayoni%2Fbithacks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ianbrayoni","download_url":"https://codeload.github.com/ianbrayoni/bithacks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233546522,"owners_count":18692232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitmanipulation","python"],"created_at":"2024-11-12T21:33:26.254Z","updated_at":"2025-09-19T02:32:40.499Z","avatar_url":"https://github.com/ianbrayoni.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bit Twiddling Hacks\n\u003e This project provides code snippets of Sean Anderson's compilation of [bit manipulation tricks](https://graphics.stanford.edu/~seander/bithacks.html) in [Python](https://www.python.org/) to make it easy to follow for those who do not have experience in [C](https://en.wikipedia.org/wiki/C_(programming_language)).\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n- [Acknowledgement](#acknowledgement)\n- [License](#license)\n- [Contents](#contents)\n  - [About the operation counting methodology](#about-the-operation-counting-methodology)\n  - [Compute the sign of an integer](#compute-the-sign-of-an-integer)\n  - [Detect if two integers have opposite signs](#detect-if-two-integers-have-opposite-signs)\n  - [Compute the integer absolute value (abs) without branching](#compute-the-integer-absolute-value-abs-without-branching)\n  - [Compute the minimum (min) or maximum (max) of two integers without branching](#compute-the-minimum-min-or-maximum-max-of-two-integers-without-branching)\n  - [Determining if an integer is a power of 2](#determining-if-an-integer-is-a-power-of-2)\n  - [Sign extending](#sign-extending)\n    - [Sign extending from a constant bit-width](#sign-extending-from-a-constant-bit-width)\n    - [Sign extending from a variable bit-width](#sign-extending-from-a-variable-bit-width)\n    - [Sign extending from a variable bit-width in 3 operations](#sign-extending-from-a-variable-bit-width-in-3-operations)\n  - [Conditionally set or clear bits without branching](#conditionally-set-or-clear-bits-without-branching)\n  - [Conditionally negate a value without branching](#conditionally-negate-a-value-without-branching)\n  - [Merge bits from two values according to a mask](#merge-bits-from-two-values-according-to-a-mask)\n  - [Counting bits set](#counting-bits-set)\n    - [Counting bits set, naive way](#counting-bits-set-naive-way)\n    - [Counting bits set by lookup table](#counting-bits-set-by-lookup-table)\n    - [Counting bits set, Brian Kernighan's way](#counting-bits-set-brian-kernighans-way)\n    - [Counting bits set in 14, 24, or 32-bit words using 64-bit instructions](#counting-bits-set-in-14-24-or-32-bit-words-using-64-bit-instructions)\n    - [Counting bits set, in parallel](#counting-bits-set-in-parallel)\n    - [Count bits set (rank) from the most-significant bit upto a given position](#count-bits-set-rank-from-the-most-significant-bit-upto-a-given-position)\n    - [Select the bit position (from the most-significant bit) with the given count (rank)](#select-the-bit-position-from-the-most-significant-bit-with-the-given-count-rank)\n  - [Computing parity (1 if an odd number of bits set, 0 otherwise)](#computing-parity-1-if-an-odd-number-of-bits-set-0-otherwise)\n    - [Compute parity of a word the naive way](#compute-parity-of-a-word-the-naive-way)\n    - [Compute parity by lookup table](#compute-parity-by-lookup-table)\n    - [Compute parity of a byte using 64-bit multiply and modulus division](#compute-parity-of-a-byte-using-64-bit-multiply-and-modulus-division)\n    - [Compute parity of word with a multiply](#compute-parity-of-word-with-a-multiply)\n    - [Compute parity in parallel](#compute-parity-in-parallel)\n  - [Swapping Values](#swapping-values)\n    - [Swapping values with subtraction and addition](#swapping-values-with-subtraction-and-addition)\n    - [Swapping values with XOR](#swapping-values-with-xor)\n    - [Swapping individual bits with XOR](#swapping-individual-bits-with-xor)\n  - [Reversing bit sequences](#reversing-bit-sequences)\n    - [Reverse bits the obvious way](#reverse-bits-the-obvious-way)\n    - [Reverse bits in word by lookup table](#reverse-bits-in-word-by-lookup-table)\n    - [Reverse the bits in a byte with 3 operations (64-bit multiply and modulus division)](#reverse-the-bits-in-a-byte-with-3-operations-64-bit-multiply-and-modulus-division)\n    - [Reverse the bits in a byte with 4 operations (64-bit multiply, no division)](#reverse-the-bits-in-a-byte-with-4-operations-64-bit-multiply-no-division)\n    - [Reverse the bits in a byte with 7 operations (no 64-bit, only 32)](#reverse-the-bits-in-a-byte-with-7-operations-no-64-bit-only-32)\n    - [Reverse an N-bit quantity in parallel with 5 * lg(N) operations](#reverse-an-n-bit-quantity-in-parallel-with-5--lgn-operations)\n  - [Modulus division (aka computing remainders)](#modulus-division-aka-computing-remainders)\n    - [Computing modulus division by 1 \u003c\u003c s without a division operation (obvious)](#computing-modulus-division-by-1--s-without-a-division-operation-obvious)\n    - [Computing modulus division by (1 \u003c\u003c s) - 1 without a division operation](#computing-modulus-division-by-1--s---1-without-a-division-operation)\n    - [Computing modulus division by (1 \u003c\u003c s) - 1 in parallel without a division operation](#computing-modulus-division-by-1--s---1-in-parallel-without-a-division-operation)\n  - [Finding integer log base 2 of an integer (aka the position of the highest bit set)](#finding-integer-log-base-2-of-an-integer-aka-the-position-of-the-highest-bit-set)\n    - [Find the log base 2 of an integer with the MSB N set in O(N) operations (the obvious way)](#find-the-log-base-2-of-an-integer-with-the-msb-n-set-in-on-operations-the-obvious-way)\n    - [Find the integer log base 2 of an integer with an 64-bit IEEE float](#find-the-integer-log-base-2-of-an-integer-with-an-64-bit-ieee-float)\n    - [Find the log base 2 of an integer with a lookup table](#find-the-log-base-2-of-an-integer-with-a-lookup-table)\n    - [Find the log base 2 of an N-bit integer in O(lg(N)) operations](#find-the-log-base-2-of-an-n-bit-integer-in-olgn-operations)\n    - [Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup](#find-the-log-base-2-of-an-n-bit-integer-in-olgn-operations-with-multiply-and-lookup)\n  - [Find integer log base 10 of an integer](#find-integer-log-base-10-of-an-integer)\n  - [Find integer log base 10 of an integer the obvious way](#find-integer-log-base-10-of-an-integer-the-obvious-way)\n  - [Find integer log base 2 of a 32-bit IEEE float](#find-integer-log-base-2-of-a-32-bit-ieee-float)\n  - [Find integer log base 2 of the pow(2, r)-root of a 32-bit IEEE float (for unsigned integer r)](#find-integer-log-base-2-of-the-pow2-r-root-of-a-32-bit-ieee-float-for-unsigned-integer-r)\n  - [Counting consecutive trailing zero bits (or finding bit indices)](#counting-consecutive-trailing-zero-bits-or-finding-bit-indices)\n    - [Count the consecutive zero bits (trailing) on the right linearly](#count-the-consecutive-zero-bits-trailing-on-the-right-linearly)\n    - [Count the consecutive zero bits (trailing) on the right in parallel](#count-the-consecutive-zero-bits-trailing-on-the-right-in-parallel)\n    - [Count the consecutive zero bits (trailing) on the right by binary search](#count-the-consecutive-zero-bits-trailing-on-the-right-by-binary-search)\n    - [Count the consecutive zero bits (trailing) on the right by casting to a float](#count-the-consecutive-zero-bits-trailing-on-the-right-by-casting-to-a-float)\n    - [Count the consecutive zero bits (trailing) on the right with modulus division and lookup](#count-the-consecutive-zero-bits-trailing-on-the-right-with-modulus-division-and-lookup)\n    - [Count the consecutive zero bits (trailing) on the right with multiply and lookup](#count-the-consecutive-zero-bits-trailing-on-the-right-with-multiply-and-lookup)\n  - [Round up to the next highest power of 2 by float casting](#round-up-to-the-next-highest-power-of-2-by-float-casting)\n  - [Round up to the next highest power of 2](#round-up-to-the-next-highest-power-of-2)\n  - [Interleaving bits (aka computing Morton Numbers)](#interleaving-bits-aka-computing-morton-numbers)\n    - [Interleave bits the obvious way](#interleave-bits-the-obvious-way)\n    - [Interleave bits by table lookup](#interleave-bits-by-table-lookup)\n    - [Interleave bits with 64-bit multiply](#interleave-bits-with-64-bit-multiply)\n    - [Interleave bits by Binary Magic Numbers](#interleave-bits-by-binary-magic-numbers)\n  - [Testing for ranges of bytes in a word (and counting occurances found)](#testing-for-ranges-of-bytes-in-a-word-and-counting-occurances-found)\n    - [Determine if a word has a zero byte](#determine-if-a-word-has-a-zero-byte)\n    - [Determine if a word has a byte equal to n](#determine-if-a-word-has-a-byte-equal-to-n)\n    - [Determine if a word has byte less than n](#determine-if-a-word-has-byte-less-than-n)\n    - [Determine if a word has a byte greater than n](#determine-if-a-word-has-a-byte-greater-than-n)\n    - [Determine if a word has a byte between m and n](#determine-if-a-word-has-a-byte-between-m-and-n)\n    - [Compute the lexicographically next bit permutation](#compute-the-lexicographically-next-bit-permutation)\n- [Contributing](#contributing)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n# Acknowledgement\n\u003e Hats off to Sean Eron Anderson, seander@cs.stanford.edu, for creating https://graphics.stanford.edu/~seander/bithacks.html\n\nI **DO NOT** own any of this. The following contents comes directly from the above link. I will be happy to transfer the ownership of this repository to the original author, Sean Eron Anderson.\n\n I will not alter the explanations provided by Sean because they provide a lot of context. Where necessary, I will add a *NOTE* section as a quote, see below notation, or just as comments within the Python code.\n\n\u003e *example quote*\n\n# License\n[License Information](LICENSE.md)\n\n\n# Contents\n\n## About the operation counting methodology\nWhen totaling the number of operations for algorithms here, any Python operator is counted as one operation. Intermediate assignments, which need not be written to RAM, are not counted. Of course, this operation counting approach only serves as an approximation of the actual number of machine instructions and CPU time. All operations are assumed to take the same amount of time, which is not true in reality, but CPUs have been heading increasingly in this direction over time. There are many nuances that determine how fast a system will run a given sample of code, such as cache sizes, memory bandwidths, instruction sets, etc. In the end, benchmarking is the best way to determine whether one method is really faster than another, so consider the techniques below as possibilities to test on your target architecture.\n\n## Compute the sign of an integer\n\u003e we want to find the sign of `v` and hold the result in `sign`\n```python\n# obvious way, -1 if negative, 0 if positive\n\u003e\u003e\u003e sign = -(v \u003c 0)\n```\n`CHAR_BIT` is the number of bits per byte (normally 8) but is platform dependent.\n\n```python\n\u003e\u003e\u003e import sys\n\u003e\u003e\u003e CHAR_BIT = sys.int_info.bits_per_digit\n\u003e\u003e\u003e SIZE_INT = sys.int_info.sizeof_digit\n# Given an integer v, returns -1 iff v \u003c 0, else 0\n\u003e\u003e\u003e sign = v \u003e\u003e (SIZE_INT * CHAR_BIT - 1);\n```\nThe above evaluates to `sign = v \u003e\u003e 31` for 32-bit integers. This is one operation faster than the obvious way, `sign = -(v \u003c 0)`. This trick works because when signed integers are shifted right, the value of the far left bit is copied to the other bits. The far left bit is 1 when the value is negative and 0 otherwise; all 1 bits gives -1. Unfortunately, this behavior is architecture-specific.\n\nAlternatively, if you prefer the result be either -1 or +1, then use:\n\n```python\n# Given an integer v, returns -1 iff v \u003c 0, else +1\n\u003e\u003e\u003e sign = 1 | (v \u003e\u003e (SIZE_INT * CHAR_BIT - 1))\n```\nOn the other hand, if you prefer the result be either -1, 0, or +1, then use:\n\n```python\n# -1, 0, or +1\n\u003e\u003e\u003e sign = (v \u003e 0) - (v \u003c 0)\n```\n*Caveat*: On March 7, 2003, Angus Duggan pointed out that the 1989 ANSI C specification leaves the result of signed right-shift implementation-defined, so on some systems this hack might not work. For greater portability, Toby Speight suggested on September 28, 2005 that CHAR_BIT be used here and throughout rather than assuming bytes were 8 bits long. Angus recommended the more portable versions above, involving casting on March 4, 2006. Rohit Garg suggested the version for non-negative integers on September 12, 2009.\n\n## Detect if two integers have opposite signs\n\n```python\n# True iff x and y have opposite signs\n\u003e\u003e\u003e (x ^ y) \u003c 0\n```\n\nManfred Weis suggested I add this entry on November 26, 2009.\n\n## Compute the integer absolute value (abs) without branching\n\u003e we want to find the absolute value of `v` and hold the result in `r`\n\n```python\n\u003e\u003e\u003e import sys\n\u003e\u003e\u003e CHAR_BIT = sys.int_info.bits_per_digit\n\u003e\u003e\u003e SIZE_INT = sys.int_info.sizeof_digit\n\u003e\u003e\u003e mask = v \u003e\u003e (SIZE_INT * (CHAR_BIT - 1))\n\u003e\u003e\u003e r = (v + mask) ^ mask\n```\n\nPatented variation:\n```python\n\u003e\u003e\u003e import sys\n\u003e\u003e\u003e CHAR_BIT = sys.int_info.bits_per_digit\n\u003e\u003e\u003e mask = v \u003e\u003e (CHAR_BIT - 1)\n\u003e\u003e\u003e r = (v ^ mask) - mask\n```\n\nSome CPUs don't have an integer absolute value instruction (or the compiler fails to use them). On machines where branching is expensive, the above expression can be faster than the obvious approach, `r = (v \u003c 0) ? -(unsigned)v : v`, even though the number of operations is the same.\n\nOn March 7, 2003, Angus Duggan pointed out that the 1989 ANSI C specification leaves the result of signed right-shift implementation-defined, so on some systems this hack might not work. I've read that ANSI C does not require values to be represented as two's complement, so it may not work for that reason as well (on a diminishingly small number of old machines that still use one's complement).\n\nOn March 14, 2004, Keith H. Duggar sent me the patented variation above; it is superior to the one I initially came up with, `r=(+1|(v\u003e\u003e(sizeof(int)*CHAR_BIT-1)))*v`, because a multiply is not used. Unfortunately, this method has been patented in the USA on June 6, 2000 by Vladimir Yu Volkonsky and assigned to Sun Microsystems. On August 13, 2006, Yuriy Kaminskiy told me that the patent is likely invalid because the method was published well before the patent was even filed, such as in How to Optimize for the Pentium Processor by Agner Fog, dated November, 9, 1996. Yuriy also mentioned that this document was translated to Russian in 1997, which Vladimir could have read. Moreover, the Internet Archive also has an old link to it. On January 30, 2007, Peter Kankowski shared with me an abs version he discovered that was inspired by Microsoft's Visual C++ compiler output. It is featured here as the primary solution. On December 6, 2007, Hai Jin complained that the result was signed, so when computing the abs of the most negative value, it was still negative. On April 15, 2008 Andrew Shapira pointed out that the obvious approach could overflow, as it lacked an (unsigned) cast then; for maximum portability he suggested `(v \u003c 0) ? (1 + ((unsigned)(-1-v))) : (unsigned)v`. But citing the ISO C99 spec on July 9, 2008, Vincent Lefèvre convinced me to remove it becasue even on non-2s-complement machines -(unsigned)v will do the right thing. The evaluation of -(unsigned)v first converts the negative value of v to an unsigned by adding `2**N`, yielding a 2s complement representation of v's value that I'll call U. Then, U is negated, giving the desired result, `-U = 0 - U = 2**N - U = 2**N - (v+2**N) = -v = abs(v)`.\n\n\n## Compute the minimum (min) or maximum (max) of two integers without branching\n\u003e we want to find the minimum/maximum of `x` and `y` and hold the result in `r`\n\n```python\n# min(x, y)\n\u003e\u003e\u003e r = y ^ ((x ^ y) \u0026 - (x \u003c y))\n```\nOn some rare machines where branching is very expensive and no condition move instructions exist, the above expression might be faster than the obvious approach, `r = (x \u003c y) ? x : y`, even though it involves two more instructions. (Typically, the obvious approach is best, though.) It works because if `x \u003c y`, then `-(x \u003c y)` will be all ones, so `r = y ^ (x ^ y) \u0026 ~0 = y ^ x ^ y = x`. Otherwise, if `x \u003e= y`, then `-(x \u003c y)` will be all zeros, so `r = y ^ ((x ^ y) \u0026 0) = y`. On some machines, evaluating `(x \u003c y)` as 0 or 1 requires a branch instruction, so there may be no advantage.\n\nTo find the maximum, use:\n```python\n# max(x, y)\n\u003e\u003e\u003e r = x ^ ((x ^ y) \u0026 -(x \u003c y))\n```\n\n**Quick and dirty versions**\n\nIf you know that `INT_MIN \u003c= x - y \u003c= INT_MAX`, then you can use the following, which are faster because (x - y) only needs to be evaluated once.\n\n\u003e *Please note that Integers in Python are unbounded i.e the maximum integer representable is a fraction of the available memory. The constant `sys.maxsize` can be used to find the word-size; specifically, it's the maximum value integer that can be stored in the word, e.g., `2**63 - 1` on a 64-bit machine*.\n\n```python\n\u003e\u003e\u003e import sys\n\u003e\u003e\u003e CHAR_BIT = sys.int_info.bits_per_digit\n\u003e\u003e\u003e SIZE_INT = sys.int_info.sizeof_digit\n# min(x, y)\n\u003e\u003e\u003e r = y + ((x - y) \u0026 ((x - y) \u003e\u003e (SIZE_INT * CHAR_BIT - 1)))\n\n# max(x, y)\n\u003e\u003e\u003e r =  x - ((x - y) \u0026 ((x - y) \u003e\u003e (SIZE_INT * CHAR_BIT - 1)))\n```\n\nNote that the 1989 ANSI C specification doesn't specify the result of signed right-shift, so these aren't portable. If exceptions are thrown on overflows, then the values of x and y should be unsigned or cast to unsigned for the subtractions to avoid unnecessarily throwing an exception, however the right-shift needs a signed operand to produce all one bits when negative, so cast to signed there. On March 7, 2003, Angus Duggan pointed out the right-shift portability issue. On May 3, 2005, Randal E. Bryant alerted me to the need for the precondition, `INT_MIN \u003c= x - y \u003c= INT_MAX`, and suggested the non-quick and dirty version as a fix. Both of these issues concern only the quick and dirty version. Nigel Horspoon observed on July 6, 2005 that gcc produced the same code on a Pentium as the obvious solution because of how it evaluates `(x \u003c y)`. On July 9, 2008 Vincent Lefèvre pointed out the potential for overflow exceptions with subtractions in `r = y + ((x - y) \u0026 -(x \u003c y))`, which was the previous version. Timothy B. Terriberry suggested using xor rather than add and subract to avoid casting and the risk of overflows on June 2, 2009.\n\n## Determining if an integer is a power of 2\n\n\u003e we want to see if `v` is a power of 2, `f` holds the boolean result.\n\n```python\n\u003e\u003e\u003e f = (v \u0026 (v - 1)) == 0\n```\nNote that 0 is incorrectly considered a power of 2 here. To remedy this, use:\n\n```python\n\u003e\u003e\u003e f = v and not(v \u0026 (v - 1))\n```\n## Sign extending\n### Sign extending from a constant bit-width\n### Sign extending from a variable bit-width\n### Sign extending from a variable bit-width in 3 operations\n## Conditionally set or clear bits without branching\n## Conditionally negate a value without branching\n## Merge bits from two values according to a mask\n## Counting bits set\n### Counting bits set, naive way\n### Counting bits set by lookup table\n### Counting bits set, Brian Kernighan's way\n### Counting bits set in 14, 24, or 32-bit words using 64-bit instructions\n### Counting bits set, in parallel\n### Count bits set (rank) from the most-significant bit upto a given position\n### Select the bit position (from the most-significant bit) with the given count (rank)\n## Computing parity (1 if an odd number of bits set, 0 otherwise)\n### Compute parity of a word the naive way\n### Compute parity by lookup table\n### Compute parity of a byte using 64-bit multiply and modulus division\n### Compute parity of word with a multiply\n### Compute parity in parallel\n## Swapping Values\n### Swapping values with subtraction and addition\n### Swapping values with XOR\n### Swapping individual bits with XOR\n## Reversing bit sequences\n### Reverse bits the obvious way\n### Reverse bits in word by lookup table\n### Reverse the bits in a byte with 3 operations (64-bit multiply and modulus division)\n### Reverse the bits in a byte with 4 operations (64-bit multiply, no division)\n### Reverse the bits in a byte with 7 operations (no 64-bit, only 32)\n### Reverse an N-bit quantity in parallel with 5 * lg(N) operations\n## Modulus division (aka computing remainders)\n### Computing modulus division by 1 \u003c\u003c s without a division operation (obvious)\n### Computing modulus division by (1 \u003c\u003c s) - 1 without a division operation\n### Computing modulus division by (1 \u003c\u003c s) - 1 in parallel without a division operation\n## Finding integer log base 2 of an integer (aka the position of the highest bit set)\n### Find the log base 2 of an integer with the MSB N set in O(N) operations (the obvious way)\n### Find the integer log base 2 of an integer with an 64-bit IEEE float\n### Find the log base 2 of an integer with a lookup table\n### Find the log base 2 of an N-bit integer in O(lg(N)) operations\n### Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup\n## Find integer log base 10 of an integer\n## Find integer log base 10 of an integer the obvious way\n## Find integer log base 2 of a 32-bit IEEE float\n## Find integer log base 2 of the pow(2, r)-root of a 32-bit IEEE float (for unsigned integer r)\n## Counting consecutive trailing zero bits (or finding bit indices)\n### Count the consecutive zero bits (trailing) on the right linearly\n### Count the consecutive zero bits (trailing) on the right in parallel\n### Count the consecutive zero bits (trailing) on the right by binary search\n### Count the consecutive zero bits (trailing) on the right by casting to a float\n### Count the consecutive zero bits (trailing) on the right with modulus division and lookup\n### Count the consecutive zero bits (trailing) on the right with multiply and lookup\n## Round up to the next highest power of 2 by float casting\n## Round up to the next highest power of 2\n## Interleaving bits (aka computing Morton Numbers)\n### Interleave bits the obvious way\n### Interleave bits by table lookup\n### Interleave bits with 64-bit multiply\n### Interleave bits by Binary Magic Numbers\n## Testing for ranges of bytes in a word (and counting occurances found)\n### Determine if a word has a zero byte\n### Determine if a word has a byte equal to n\n### Determine if a word has byte less than n\n### Determine if a word has a byte greater than n\n### Determine if a word has a byte between m and n\n### Compute the lexicographically next bit permutation\n\n# Contributing\nPlease take a minute to go over the [Contribution Guidelines](CONTRIBUTING.md).\n\nContributions are welcome!\n* [Issue reports](https://github.com/ianbrayoni/bithacks/issues)\n* [Pull Requests](https://github.com/ianbrayoni/bithacks/pulls)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianbrayoni%2Fbithacks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fianbrayoni%2Fbithacks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianbrayoni%2Fbithacks/lists"}