Mod of power 2 on bitwise operators?
来源:互联网 发布:手机能改淘宝密码吗 编辑:程序博客网 时间:2024/06/07 04:42
Bitwise Alternatives to Multiply, Divide, and Modulus: Faster?
Many programmers are aware of a special case where you can use a bitwise shift for multiplication or division when you’re multiplying or dividing by a power of two. For example, you can replace
i * 2
withi << 1
andi / 2
withi >> 1
. A lesser-known trick works for modulus. But are these alternatives actually any faster? Today's article puts them to the test!Here are today's competitors:
Here's a performance test that performs each of these operations a lot of times:
I ran this test app in the following environment:
- Flex SDK (MXMLC) 4.6.0.23201, compiling in release mode (no debugging or verbose stack traces)
- Release version of Flash Player 11.3.300.271
- 2.3 Ghz Intel Core i7
- Mac OS X 10.8.0
And here are the results I got:
Method Time Div: i / 4358Div: i >> 2224Mul: i * 4206Mul: i << 2255Mod: i % 4918Mod: i & 3254The above results validate the bitwise versions in two out of three tests: division and modulus. In the multiplication case, the normal version actually performs about 20% faster than the bitwise equivalent. On the other hand, division is nearly twice as fast with the bitwise shift and the bitwise modulus (really just an
&
) is more than three times faster! So if you're got a lot of divides or mods in your performance-critical code, swap them over to the bitwise versions!- 1) How does mod of power of 2 work on only lower order bits of a binary number (1011000111011010) ?
- 2) What is this number mod 2 to power 0, 2 to power 4 ?
- 3) What does power of 2 have to do with the modulo operator ? Does it hold a special property ?
- 4) Can someone give me an example ?
The instructor says "When you take something mod to power of 2 you just take its lower order bits". I was too afraid to ask what he meant =)
5 Answers
He meant that taking number mod 2^n
is equivalent to stripping off all but the n
lowest-order (right-most) bits of number
.
For example, if n == 2,
number number mod 400000001 0000000100000010 0000001000000011 0000001100000100 0000000000000101 0000000100000110 0000001000000111 0000001100001000 0000000000001001 00000001etc.
So in other words, number mod 4
is the same as number & 00000011
(where &
means bitwise-and)
Note that this works exactly the same in base-10: number mod 10
gives you the last digit of the number in base-10, number mod 100
gives you the last two digits, etc.
What he means is that :
x modulo y = (x & (y − 1))
When y is a power of 2.
Example:
0110010110 (406) modulo0001000000 (64) =0000010110 (22)^^^^<- ignore these bits
Using your example now :
1011000111011010 (45530) modulo0000000000000001 (2 power 0) =0000000000000000 (0)^^^^^^^^^^^^^^^^<- ignore these bits1011000111011010 (45530) modulo0000000000010000 (2 power 4) =0000000000001010 (10)^^^^^^^^^^^^<- ignore these bits
Consider when you take a number modulo 10. If you do that, you just get the last digit of the number.
334 % 10 = 4 12345 % 10 = 5
Likewise if you take a number modulo 100, you just get the last two digits.
334 % 100 = 34 12345 % 100 = 45
So you can get the modulo of a power of two by looking at its last digits in binary. That's the same as doing a bitwise and.
Modulo in general returns the remainder of a value after division. So x mod 4
, for example, returns 0, 1, 2 or 3 depending on x. These possible values can be represented using two bits in binary (00, 01, 10, 11) - another way to do x mod 4
is to simply set all the bits to zero in x except the last two ones.
Example:
x = 10101010110101110x mod 4 = 00000000000000010
Answering your specific questions:
- mod is a remainder operator. If applied to a series of numbers x in 0, 1, ..., then x mod n will be 0, 1, ..., n-1, 0, 1, ..., n-1, ad infinitum. When your modulus n is a power of 2, then x mod n will count up in binary from 0 to n-1, back to 0, to n-1, etc; for modulus n that looks like binary 01xxxxx, x mod n will cycle through every of those low-order bits xxxxx.
- binary 1011000111011010 mod 1 is 0 (mod 2^0 yields the last zero bits; everything mod 1 is zero). binary 1011000111011010 mod binary 10000 is 1010 (mod 2^4 yields the last four bits).
- Division and remainder of binary number by powers of two is particularly efficient because it's just shifting and masking; mathematically it's nothing special.
- Example: See answer to question 2.
- Mod of power 2 on bitwise operators?
- What are bitwise operators?
- The Bitwise Operators(Java)
- Day9-28.Bitwise operators
- An introduction to bitwise operators
- 位运算符 Bitwise Operators
- python: Bitwise Operators (位运算)
- Python 入门教程 16 ---- Introduction to Bitwise Operators
- Java的位运算(bitwise operators)
- Next Power of 2
- LeetCode[Bitwise]----Bitwise AND of Numbers Range
- usage of >>>、>> and << operators
- Summary of Operators
- Precedence of operators
- SPOJ MOD - Power Modulo Inverted
- How to power on/of the machine by KVM
- Leetcode 231 and 342. Power of 2, Power of 4
- power of 2 or not
- objective-c之block 总结
- 在Eclipse中打开工程中的drawable里面的PNG格式的图片报错,找不到应用程序
- java实现子字符串的BM算法
- kafka源码解析之十一KafkaApis
- Jmeter和Tsung之间的对比
- Mod of power 2 on bitwise operators?
- 怎么设置JLabel内容对齐方式
- QPushButton类
- [025]Java矩阵运算——jama
- 简单的leetcode(一)
- 图像分析:二值图像连通域标记2-基于轮廓的标记
- TypeScript 面向对象基础知识
- JScrollPane 自动跟进 自动到滚动到最底部
- Microsoft.Win32.OpenFileDialog使用例子