检查循环(模 16)数是否大于另一个?

Check if cyclic (modulo 16) number is larger than another?

我有两个以 16 为模的循环整数,因此它们的值介于 0 和 15 之间。

我需要比较两个数字以确定 n_1 是否大于 n_0

n_1 > n_0

很明显,这个定义不准确,所以我定义n_1大于n_0如果前面小于8"numbers",否则小于n_0(如果不相等)。

即如果:

n_0 = 0
if n_1 is between 1 and 8 (both inclusive)
then n_1 is greater than n_0.

n_0 = 5
if n_1 is between 6 and 15 (both inclusive)
then n_1 is greater than n_0.

n_0 = 12
if n_1 is between 13 and 15 (both inclusive)
    or between 0 and 4 (both inclusive)
then n_1 is greater than n_0.

如何以编程方式表达这种比较?

我确定我混淆了上面的术语,所以请随时纠正我的措辞。 :)

我在想一个有 16 小时的时钟。这个想法基本上是将 n0 移动到 0 位置,并将 n1 移动相同数量的 "ticks"。现在您可以简单地检查 n1 是大于还是小于取决于它是在 8 点钟之前还是在 8 点钟之后。

public int compare (int n0, int n1){
    int ticksToZero = 16 - n0;
    if(n0 == n1)
        return 0;
    else if((n1 + ticksToZero) % 16 <= 8)
        return -1; //n0 is smaller than n1
    else
        return 1; //n0 is larger than n1
}

我先从条件简单的部分开始,然后镜像。

    function smaller(n_0, n_1) {
      n = 16;   
      n_0 = n_0 % n;
      n_1 = n_1 % n;
    
      if(n_0 == n_1)
          return 0;
      else
          return (n_0 < n_1 && n_1 <= n_0 + 8) || (n_1 < n_0 &&  n_0 >= n_1 + 8);
    }
    console.log(0);
    console.log(smaller(0,1));
    console.log(smaller(0,8));
    console.log(smaller(0,9));
    console.log(5);
    console.log(smaller(5,6));
    console.log(smaller(5,15));
    console.log(smaller(5,16));
    console.log(12);
    console.log(smaller(12,13));
    console.log(smaller(12,14));
    console.log(smaller(12,15))
    console.log(smaller(12,0));
    console.log(smaller(12,1))
    console.log(smaller(12,2))
    console.log(smaller(12,3))
    console.log(smaller(12,4));
    console.log(smaller(12,5));
    console.log(smaller(12,6));
    console.log(smaller(12,7));
    console.log(smaller(12,8));
    console.log(smaller(12,9));
    console.log(smaller(12,10));
    console.log(smaller(12,11));

你可以通过找出n1n0的不同来测试它是否在1和8之间。

#include <iostream>
using namespace std;

bool Test(int n0, int n1) {
    int n = (n1 - n0 + 16) % 16;
    return n && n <= 8;
}

int main() {
    cout << Test(0, 0) << endl;
    cout << Test(0, 1) << endl;
    cout << Test(0, 8) << endl;
    cout << Test(0, 9) << endl;
    cout << Test(0, 15) << endl;
    cout << endl;

    cout << Test(5, 0) << endl;
    cout << Test(5, 4) << endl;
    cout << Test(5, 5) << endl;
    cout << Test(5, 6) << endl;
    cout << Test(5, 13) << endl;
    cout << Test(5, 15) << endl;
    cout << endl;

    cout << Test(12, 0) << endl;
    cout << Test(12, 3) << endl;
    cout << Test(12, 4) << endl;
    cout << Test(12, 5) << endl;
    cout << Test(12, 12) << endl;
    cout << Test(12, 15) << endl;

    return 0;
}

您可以使用此表达式在不显式添加 16 的情况下执行此操作:

(b - a) >= (a <= b ? 8 : -8);

这个想法是,根据比较 ab 的结果,差异必须大于 8 或 -8。

将此公式应用于数字 0..15(含)的结果如下(星号表示水平线上的数字小于垂直线上的数字的点;十六进制数字用于表示9 以上的数字;demo)

  0 1 2 3 4 5 6 7 8 9 A B C D E F
0                 * * * * * * * * 
1 *                 * * * * * * * 
2 * *                 * * * * * * 
3 * * *                 * * * * * 
4 * * * *                 * * * * 
5 * * * * *                 * * * 
6 * * * * * *                 * * 
7 * * * * * * *                 * 
8 * * * * * * * *                 
9   * * * * * * * *               
A     * * * * * * * *             
B       * * * * * * * *           
C         * * * * * * * *         
D           * * * * * * * *       
E             * * * * * * * *     
F               * * * * * * * *