如何生成位移方程?
最好在 C# 中完成此操作。
假设我有一个整数 1024。 我将能够生成这些方程:
4096 >> 2 = 1024
65536 >> 6 = 1024
64 << 4 = 1024
等等...
有任何线索、提示、指南或想法吗?
编辑:好的,简单来说,我想要的是,例如...嘿,我给你一个整数 1024,现在给我一个可能的位移列表方程总是返回 1024 的值。
好吧,从头开始。看来我的问题不是很简洁和清楚。我会再试一次。
我想要的是基于数值生成可能的位移方程列表。例如,如果我的值为 1024,我将如何生成始终返回值 1024 的可能方程列表?
示例方程:
4096 >> 2 = 1024
65536 >> 6 = 1024
64 << 4 = 1024
以类似的方式,如果我要求你给我一些额外的方程,可以给我 5,你会回答:
3 + 2 = 5
10 - 5 = 5
4 + 1 = 5
我还是太模糊了吗?我对此表示歉意。
Preferably for this to be done in C#.
Supposedly, I have an integer of 1024.
I will be able to generate these equations:
4096 >> 2 = 1024
65536 >> 6 = 1024
64 << 4 = 1024
and so on...
Any clues or tips or guides or ideas?
Edit: Ok, in simple terms, what I want is, for example...Hey, I'm giving you an integer of 1024, now give me a list of possible bit-shift equations that will always return the value of 1024.
Ok, scratch that. It seems my question wasn't very concise and clear. I'll try again.
What I want, is to generate a list of possible bit-shift equations based on a numerical value. For example, if I have a value of 1024, how would I generate a list of possible equations that would always return the value of 1024?
Sample Equations:
4096 >> 2 = 1024
65536 >> 6 = 1024
64 << 4 = 1024
In a similar way, if I asked you to give me some additional equations that would give me 5, you would response:
3 + 2 = 5
10 - 5 = 5
4 + 1 = 5
Am I still too vague? I apologize for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以反转每个方程,从而“生成”可能的方程:
因此,
因此生成 1024 的所有右/左移位,而不会由于变量的上溢或下溢而丢失位,然后反转相应的方程。
You may reverse each equation and thus "generate" possible equations:
and therefore
Thus generate all right/left shifts of 1024 without loosing bits due to overflow or underflow of your variable and then invert the corresponding equation.
只需添加一个额外的“>”或“<”:
http://www.blackwasp.co.uk/CSharpShiftOperators.aspx
Just add an extra '>' or '<':
http://www.blackwasp.co.uk/CSharpShiftOperators.aspx
您是否在问为什么这些关系存在?左移 1 位相当于乘以 2。所以
512 << 1 = 512 * 2 = 1024。右移 1 就是除以 2。右移 2 就是乘/除 4,乘以 n 就是 2^n。所以
1 << 10 = 1 * 2^10 = 1024。要了解原因,请以二进制形式写出数字:让我们以 7 为例:
如果您已经知道所有这些,我很抱歉,但您可能想让问题不那么模糊。
Are you asking why these relationships exist? Shifting bits left by 1 bit is equivalent to multiplying by 2. So
512 << 1 = 512 * 2 = 1024
. Shifting right by 1 is dividing by 2. Shifting by 2 is multiplying/dividing by 4, by n is 2^n. So1 << 10 = 1 * 2^10 = 1024
. To see why, write the number out in binary: let's take 7 as an example:If you already knew all this, I apologize, but you might want to make the question less vague.