Scale numbers to be <= 255?

前端 未结 13 2237
生来不讨喜
生来不讨喜 2021-02-05 18:39

I have cells for whom the numeric value can be anything between 0 and Integer.MAX_VALUE. I would like to color code these cells correspondingly.

If the val

13条回答
  •  无人共我
    2021-02-05 19:29

    This works! r= value /8421504;

    8421504 is actually the 'magic' number, which equals MAX_VALUE/255. Thus, MAX_VALUE/8421504 = 255 (and some change, but small enough integer math will get rid of it.

    if you want one that doesn't have magic numbers in it, this should work (and of equal performance, since any good compiler will replace it with the actual value:

    r= value/ (Integer.MAX_VALUE/255);

    The nice part is, this will not require any floating-point values.

提交回复
热议问题