Bit Representation

Q1.整数的表示

  • most significant bit(the leftmost bit)

最高位是符号位。(There is no unsigned type in Java < 7)

  • 原码 补码 反码

      0: 00000000 00000000 00000000 00000000
      5: 00000000 00000000 00000000 00000101
      -5:
         10000000 00000000 00000000 00000101
         11111111 11111111 11111111 11111010(取反)-> 反码
         11111111 11111111 11111111 11111011(+1) -> 补码
    

    Integer.MAX_VALUE + 1 = Integer.MIN_VALUE

    Integer.MIN_VALUE - 1 = Integer.MAX_VALUE

  • Why 补码?

1.方便加法器容易扩展到正负数的加减法。

2.反码需要表示正负零,补码系统可多表示$$-2^(n-1)$$

Q2. ">>>" vs. ">>"

  • signed shift ">>" - arithmetical 算数右移

      1111 1000 -8 >> 1 (对于负数来说也相当于除以2,不过是向下取整)
      1111 1100 -4 >> 1
      1111 1110 -2 >> 1
      1111 1111 -1
    
  • unsigned shift ">>>" - logical 逻辑右移

Note: These is no "unsigned int" in Java!

    Two View of a int:
    1. 32 Bits, each of which is either 0 or 1
    2. arithmetic number

results matching ""

    No results matching ""