如何生成位移方程?

发布于 2024-12-22 02:05:57 字数 746 浏览 0 评论 0原文

最好在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和我恋爱吧 2024-12-29 02:05:57

您可以反转每个方程,从而“生成”可能的方程:

1024 >> 4 == 64 

因此,

64 << 4 == 1024

因此生成 1024 的所有右/左移位,而不会由于变量的上溢或下溢而丢失位,然后反转相应的方程。

You may reverse each equation and thus "generate" possible equations:

1024 >> 4 == 64 

and therefore

64 << 4 == 1024

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.

吻泪 2024-12-29 02:05:57

只需添加一个额外的“>”或“<”:

uint value1= 4096 >> 2;
uint value2 = 65536 >> 6;
uint value3 = 64 << 4;

http://www.blackwasp.co.uk/CSharpShiftOperators.aspx

Just add an extra '>' or '<':

uint value1= 4096 >> 2;
uint value2 = 65536 >> 6;
uint value3 = 64 << 4;

http://www.blackwasp.co.uk/CSharpShiftOperators.aspx

天煞孤星 2024-12-29 02:05:57

您是否在问为什么这些关系存在?左移 1 位相当于乘以 2。所以 512 << 1 = 512 * 2 = 1024。右移 1 就是除以 2。右移 2 就是乘/除 4,乘以 n 就是 2^n。所以 1 << 10 = 1 * 2^10 = 1024。要了解原因,请以二进制形式写出数字:让我们以 7 为例:

7 -> 0000 0111b
7 << 1 -> 0000 1110b = 14
7 << 3 -> 0011 1000b = 56

如果您已经知道所有这些,我很抱歉,但您可能想让问题不那么模糊。

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. So 1 << 10 = 1 * 2^10 = 1024. To see why, write the number out in binary: let's take 7 as an example:

7 -> 0000 0111b
7 << 1 -> 0000 1110b = 14
7 << 3 -> 0011 1000b = 56

If you already knew all this, I apologize, but you might want to make the question less vague.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文