c#二进制位移的应用

需求:ABC三个物体,动态闪动。count=3
思路,index,结合for循环 1<<i位

int count = 3;//length
public float duration = 0.2f;//T
int pow = (int)Math.Pow(2.0, count);//8
...
        void Update()
        {
    
    
            int t = duration > 0f ? (int)(Time.time / duration) : 0;
            int newIndex = t - (t / pow) * pow;      //类似pow%t
            if (index != newIndex)
            {
    
    
                index = newIndex;
            }
        }
...
      for (int i = 0; i < count; i++)
      {
    
    
           if ((index & (1 << i)) != 0)
           {
    
    
              flares.Add(i);
           }
      }
...
//  index   |   1<<i
//    0         8421
//    0     |    fff
//    1     |    fft
//    2     |    ftf
//    3     |    ftt
//    4     |    tff
//    5     |    tft
//    6     |    ttf
//    7     |    ttt

猜你喜欢

转载自blog.csdn.net/kuilaurence/article/details/121901902