检查循环数(模16)是否大于另一个? [英] Check if cyclic (modulo 16) number is larger than another?

查看:96
本文介绍了检查循环数(模16)是否大于另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个循环整数,取模16,因此它们取0到15之间的值.

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

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

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

n_1 > n_0

很明显,这没有被完全定义,因此我将 n_1 定义为大于 n_0 (如果前面小于8个数字"),否则,它会小于比 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).

即如果:

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. :)

推荐答案

我当时想使用16小时的时钟.这个想法基本上是将n0移动到0位置,并将n1移动相同数量的滴答声".现在,您可以简单地检查n1是更大还是更小,具体取决于它是在8点之前还是在8点之后.

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
}

这篇关于检查循环数(模16)是否大于另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆