如何将位转换为位移位值

发布于 2025-01-29 08:42:28 字数 949 浏览 2 评论 0原文

我有一个难度选择器作为枚举设置(无= 0,易于= 1<< 0,medium = 1<< 1,hard = 1<<< 2,Expert = 1<< 3)。除此之外,我还有一系列要分配给这些困难的点值。因此,数组的索引。 [0,100,133,166,200]。

很棘手,是这个。我想抓住数组的索引,这相当于难度的位移动。因此,无= 0(0000) - > index = 0。简单= 1(0001) - >索引=1。中= 2(0010) - >索引= 2。硬= 4(0100) - >索引= 3。专家= 8(1000) - >索引= 4。

我最初尝试做平方根,因为我认为它是两个的力量,但很快就意识到它实际上并没有提高到两个,这只是两个的基础。所以那永远不会起作用。

我也知道我可以通过forloop获得此值,例如,我从8(1000)开始,并在我向右移动时保留一个计数器,并保持该值直到达到0

int difficulty = (int)LevelSetup.difficulty;
int difficultyIndex = 0;
while(difficulty != 0)
{
    difficultyIndex++;
    difficulty = difficulty >> 1;
}
currScorePerQuestion = ScorePerQuestion[difficultyIndex];

。。计数器= 0; Val = 8。|移位|计数器= 1; val = 4; | Shift |计数器= 2; val = 2; | Shift |计数器= 3; val = 1; | Shift |计数器= 4; val = 0; |结束|我们以4个价值结尾。

这似乎是真的很混乱和过度杀伤,尤其是如果您想达到64位并且有很多指示。我只知道有某种算法可以用来简单地转换这些算法。我只是在努力提出那个方程式。

任何帮助都非常感谢,谢谢!

I have a difficulty selector set as an enum (None=0, Easy = 1<<0, Medium = 1<<1, Hard = 1<<2, Expert = 1<<3). Along with this, I have an array of point values I want to assign to these difficulties. So the array has indexes as so. [0, 100, 133, 166, 200].

The tricky bit, is this. I want to grab the index of the array, that is equivalent to the bit shift of the difficulty. So None = 0 (0000)-> Index = 0. Easy = 1 (0001)-> Index = 1. Medium = 2 (0010)-> Index = 2. Hard = 4 (0100) -> Index = 3. Expert = 8 (1000) -> Index = 4.

I tried doing the Square root originally, as I thought that it was powers of two, but quickly realized that it's actually not RAISED to two, it's just a base of two. So that would never work.

I also know I can get this value via a forloop, where I start at 8 (1000) for instance, and keep a counter as I shift right, and keep that going until it hits 0.

int difficulty = (int)LevelSetup.difficulty;
int difficultyIndex = 0;
while(difficulty != 0)
{
    difficultyIndex++;
    difficulty = difficulty >> 1;
}
currScorePerQuestion = ScorePerQuestion[difficultyIndex];

IE. Counter = 0; val = 8. | SHIFT | Counter = 1; val = 4; |SHIFT| Counter = 2; val = 2; |SHIFT| Counter = 3; val = 1; |SHIFT| Counter = 4; val = 0; |END| and we end with a value of 4.

The problem with this is that it seems really messy and overkill, especially if you wanted to go up to 64 bits and have lots of indicies. I just know that there is some kind of algorithm that I could use to convert these very simply. I am just struggling to come up with what that equation is exactly.

Any help is super appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一花一树开 2025-02-05 08:42:28

问我的朋友之后。他们想到了,给了我这个解决方案。

作为二进制的工作,通过将2提高到第n个力量。我们总是有2个基础,将其提高到了位。因此2^4是8,与二进制中的1000相同。

然后,使用对数的属性。您可以使用基本2的日志,该日志与我们的基本2幂匹配,并记录其值的日志以获得指数。 IE log2(2^3)= 3。和log2(2^7)= 7。

幸运的是,二进制完全匹配了此模式,因此(1000)的一点掩码为8,等于2^3,所以log2(8)=&gt; 3。

要将一些转换为索引,即(1000)是第四位,因此我们想要4个索引。
log Base 2 of 8-&gt; Math.log2(8)= 3。然后,要达到我们的0个基于0的索引,我们只添加1。

这使我们具有以下算法:

int difficulty = (int)LevelSetup.difficulty;
currScorePerQuestion = ScorePerQuestion[Math.Log2(difficulty)+1];

After asking my friends. They came up with and gave me this solution.

As binary works by raising 2 to the nth power. We always have a base of 2, raised to the number that the bit is. So 2^4 is 8 which is the same as 1000 in binary.

Then, using the properties of Logarithms. You can use Log of base 2, which matches our base 2 powers, and take the log of it's value to get the exponential. ie Log2(2^3) = 3. And Log2(2^7) = 7.

luckily for us, binary matches this pattern completely, so a bit mask of (1000) is 8, which is equal to 2^3, so Log2(8) => 3.

To convert a bit into an index, ie (1000) is the 4th bit, so we want an index of 4.
Log base 2 of 8 -> Math.Log2(8) = 3. Then to get up to our 0 based index, we just add 1.

This leaves us with the following algorithm:

int difficulty = (int)LevelSetup.difficulty;
currScorePerQuestion = ScorePerQuestion[Math.Log2(difficulty)+1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文