Converting from signed char to unsigned char and back again?

后端 未结 5 897
清酒与你
清酒与你 2020-11-28 19:22

I\'m working with JNI and have an array of type jbyte, where jbyte is represented as an signed char i.e. ranging from -128 to 127. The jbytes represent image pixels. For ima

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 20:07

    Yes this is safe.

    The c language uses a feature called integer promotion to increase the number of bits in a value before performing calculations. Therefore your CLAMP255 macro will operate at integer (probably 32 bit) precision. The result is assigned to a jbyte, which reduces the integer precision back to 8 bits fit in to the jbyte.

提交回复
热议问题