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

一曲冷凌霜 提交于 2021-02-05 07:39:41

问题


I have two a cyclic integer, modulo 16, so they assume values between 0 and 15.

I need to compare two numbers to determine if n_1 is greater than n_0

n_1 > n_0

Obviously, this is not exactly defined, so I define n_1 to be greater than n_0 if it is less than 8 "numbers" ahead, otherwise, it is lesser than n_0 (if not equal).

I.e. if:

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.

How do I express this comparison programatically?

I am sure I am confusing the terminology above, so please feel free to correct my wording. :)


回答1:


I was thinking of a clock with 16 hours. The idea is basically to move n0 to the 0 position and move n1 the same amount of "ticks". Now you can simply check if n1 is greater or smaller dependent on if it is before 8 or after 8 o'clock.

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
}



回答2:


You may test it by finding the difference of n1 and n0 and test it whether it is between 1 and 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;
}



回答3:


You can do it without an explicit addition of 16 using this expression:

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

The idea is that the difference must be above 8 or -8 depending on the result of comparing a to b.

The result of applying this formula to numbers 0..15, inclusive, is as follows (asterisks represent points where the number from the horizontal line is less than the number from the vertical line; hex digits are used to represent numbers above 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               * * * * * * * *   



回答4:


I started with the simple part of the condition, and then mirrored it.

    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));


来源:https://stackoverflow.com/questions/47832475/check-if-cyclic-modulo-16-number-is-larger-than-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!