English 中文(简体)
对立面(使用公式)(目标-c)
原标题:opposite of enum (using formula) (objective-c)

我有4个零3个,是一个大体(目标),我想用一个公式来对待。 因此,如果将0列入公式,就会返回2,如果加入2,则返回0(与1和3相同)。

原因是(而且与方案拟定有关),我想在一席之地正好相反,而不必作任何改动。 但是,这意味着任何公式(如果可能的话)都必须不成体系,这样,其效率要比在转换时使用效率更高。

最佳回答

公式非常简单:

n = n ^ 2;

参照价值0至3,以及排他性或变迁方式:

n       ^10
---------------
0 = 00 : 10 = 2
1 = 01 : 11 = 3
2 = 10 : 00 = 0
3 = 11 : 01 = 1

测试(C#):

for (int n = 0; n <= 3; n++) {
  Console.WriteLine("{0} : {1}", n, n ^ 2);
}

产出:

0 : 2
1 : 3
2 : 0
3 : 1
问题回答
int newValue = 3 - originalValue;

或许可以投身于你身上。

Edit: Sorry, this swaps 0 <=> 3 and 1 <=> 2,我认为这是相反的。

“0 <”;=> 2和1 <=> 3

int newValue = (originalValue + 2) % 4;




相关问题
Maths in LaTex table of contents

I am trying to add a table of contents for my LaTex document. The issue I am having is that this line: subsubsection{The expectation of (X^2)} Causes an error in the file that contains the ...

Math Overflow -- Handling Large Numbers

I am having a problem handling large numbers. I need to calculate the log of a very large number. The number is the product of a series of numbers. For example: log(2x3x66x435x444) though my actual ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Subsequent weighted algorithm for comparison

I have two rows of numbers ... 1) 2 2 1 0 0 1 2) 1.5 1 0 .5 1 2 Each column is compared to each other. Lower values are better. For example Column 1, row 2 s value (1.5) is more ...

热门标签