底下的Java程式會輸出什麼?
1 2 3 4 5 | public class Multicast { public static void main(String[] args) { System.out.println((int) (char) (byte) -1); } } |
結果不會是 -1。幸運的是有條簡單的規則來說明:「Sign extension is performed if the type of the original value is signed; zero extension if it is a char, regardless of the type to which it is being converted.」也就是 byte -1 === sign extension ==> char 65535 === zero extension ==> int 65535。
而 Python Built-in type 不多,筆者只試了底下兩行:
print(int(str(bytes(-1))))
print(int(chr(bytes(-1))))
輸出結果會是什麼呢?
沒有留言:
張貼留言