[ruby-cvs:72775] shyouhei:r65675 (trunk): ~(unsigned char) is not unsigned char

shyouhei at ruby-lang.org shyouhei at ruby-lang.org
Mon Nov 12 10:08:35 JST 2018


shyouhei	2018-11-12 10:08:35 +0900 (Mon, 12 Nov 2018)

  New Revision: 65675

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65675

  Log:
    ~(unsigned char) is not unsigned char
    
    The unary ~ operator excercises integer promotion of the operand
    _before_ actually applying bitwise complement (cf: ISO/IEC 9899:1990
    section 6.3.3.3).  Which means `~buf[i]` is in fact
    `(int)~(int)buf[i]`.
    
    The problem is, when buf[i] is 0xFF:
    
          buf[i]        0xFF
     (int)buf[i] 0x0000_00FF
    ~(int)buf[i] 0xFFFF_FF00 This is -256, out of unsigned char range.
    
    The proposed fix is to change the char signed.  By doing so,
    
                       buf[i]        0xFF
          (signed char)buf[i]        0xFF
     (int)(signed char)buf[i] 0xFFFF_FFFF
    ~(int)(signed char)buf[i] 0x0000_0000 This is 0, does not overflow.

  Modified files:
    trunk/bignum.c


More information about the ruby-cvs mailing list