Larger integer to smaller unsigned type conversion in C

前端 未结 2 1918
天命终不由人
天命终不由人 2021-01-03 02:05

I was goint through k & r. I was having problem in understanding following lines on page 197(section A6)

Integral conversions: any integer is co

2条回答
  •  太阳男子
    2021-01-03 02:13

    It means only low value bits will be count and high order bits will be discarded.

    For example:

    01111111 11111111 11110000 00001111
    

    when converted to a 16 bit unsigned short will be:

    11110000 00001111
    

    This is effectively mathematically expressed in:

    target_value = value % (target_type_max+1)           ( % = modulus operator )
    

提交回复
热议问题