bitwise not operator

前端 未结 10 991
广开言路
广开言路 2021-01-30 16:24

Why bitwise operation (~0); prints -1 ? In binary , not 0 should be 1 . why ?

10条回答
  •  无人及你
    2021-01-30 17:03

    In standard binary encoding, 0 is all 0s, ~ is bitwise NOT. All 1s is (most often) -1 for signed integer types. So for a signed byte type:

    0xFF = -1    // 1111 1111
    0xFE = -2    // 1111 1110
    ...
    0xF0 = -128  // 1000 0000
    0x7F = 127   // 0111 1111
    0x7E = 126   // 0111 1110
    ...
    0x01 = 1     // 0000 0001
    0x00 = 0     // 0000 0000
    

提交回复
热议问题