├── .travis.yml ├── LICENSE ├── README.md └── test.sh /.travis.yml: -------------------------------------------------------------------------------- 1 | script: ./test.sh 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Sanket Dasgupta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bit Hacks 2 | 3 | A collection of bit manipulation techniques. 4 | 5 | *The examples are done in Python but should work with most languages 6 | with hardly any modification.* 7 | 8 | ## Multiply a number by 2 9 | 10 | ```python 11 | >>> 5 << 1 12 | 10 13 | 14 | ``` 15 | 16 | ## Divide a number by 2 17 | 18 | ```python 19 | >>> 6 >> 1 20 | 3 21 | 22 | ``` 23 | 24 | ## Multiply by mth power of 2 25 | 26 | ```python 27 | >>> n = 5 28 | >>> n << 3 29 | 40 30 | 31 | ``` 32 | 33 | ## Divide by mth power of 2 34 | 35 | ```python 36 | >>> n = 40 37 | >>> n >> 3 38 | 5 39 | 40 | ``` 41 | 42 | ## Exchange two values 43 | 44 | ```python 45 | >>> x = 3 46 | >>> y = 5 47 | 48 | >>> x ^= y 49 | >>> y ^= x 50 | >>> x ^= y 51 | 52 | >>> x 53 | 5 54 | >>> y 55 | 3 56 | 57 | ``` 58 | 59 | ## Check if an integer is even or odd 60 | 61 | ```python 62 | >>> n = 6 63 | >>> n & 1 == 0 64 | True 65 | 66 | >>> n = 5 67 | >>> n & 1 == 0 68 | False 69 | 70 | ``` 71 | 72 | ## Check Equality of two numbers 73 | 74 | ```python 75 | >>> x = 42 76 | >>> y = 42 77 | >>> not x ^ y 78 | True 79 | 80 | ``` 81 | 82 | ## Check if a number is a power of two 83 | 84 | ```python 85 | >>> n = 8 86 | >>> n & (n-1) == 0 87 | True 88 | 89 | >>> n = 6 90 | >>> n & (n-1) == 0 91 | False 92 | 93 | ``` 94 | 95 | Note that 0 is incorrectly considered a power of 2 here. To remedy this, use: 96 | 97 | ```python 98 | >>> n = 8 99 | >>> n and not (n & (n - 1)) 100 | True 101 | 102 | ``` 103 | 104 | ## Check whether the n-th bit is set of an integer 105 | 106 | ```python 107 | >>> n = 5 108 | >>> n & (1 << 2) > 0 # 2nd bit is set in 101 (zero-indexed). 109 | True 110 | 111 | >>> n = 2 112 | >>> n & (1 << 2) > 0 # 2nd bit is not set 010 113 | False 114 | 115 | ``` 116 | 117 | ## Minimum and Maximum of two numbers 118 | 119 | ```python 120 | >>> x = 5 121 | >>> y = 2 122 | >>> y ^ ((x ^ y) & -(x < y)) # minimum 123 | 2 124 | >>> x ^ ((x ^ y) & -(x < y)) # maximum 125 | 5 126 | 127 | ``` 128 | 129 | ## Convert integer to binary number 130 | 131 | ```python 132 | >>> def binary(s): 133 | ... return str(s) if s <= 1 else binary(s >> 1) + str(s & 1) 134 | >>> binary(28) 135 | '11100' 136 | 137 | ``` 138 | 139 | ## Resources 140 | 141 | * [awesome-bits](https://github.com/keon/awesome-bits) 142 | * [A Bit of Fun: Fun with Bits - Topcoder](https://www.topcoder.com/community/data-science/data-science-tutorials/a-bit-of-fun-fun-with-bits/) 143 | * [Bit Manipulation - HackerEarth](https://www.hackerearth.com/practice/notes/bit-manipulation/) 144 | * [Bit Twiddling Hacks - Stanford](https://graphics.stanford.edu/~seander/bithacks.html) 145 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | python -m doctest -v README.md 3 | --------------------------------------------------------------------------------